forked from brimworks/lua-ev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop_lua_ev.c
302 lines (260 loc) · 7.94 KB
/
loop_lua_ev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* Create a table for ev.Loop that gives access to the constructor for
* loop objects and the "default" event loop object instance.
*
* [-0, +1, ?]
*/
static int luaopen_ev_loop(lua_State *L) {
lua_pop(L, create_loop_mt(L));
lua_createtable(L, 0, 1);
lua_pushcfunction(L, loop_new);
lua_setfield(L, -2, "new");
*loop_alloc(L) = UNINITIALIZED_DEFAULT_LOOP;
lua_setfield(L, -2, "default");
return 1;
}
/**
* Create the loop metatable in the registry.
*
* [-0, +1, ?]
*/
static int create_loop_mt(lua_State *L) {
static luaL_Reg fns[] = {
{ "is_default", loop_is_default },
{ "count", loop_iteration }, /* old API */
{ "iteration", loop_iteration },
{ "depth", loop_depth },
{ "now", loop_now },
{ "update_now", loop_update_now },
{ "loop", loop_loop },
{ "unloop", loop_unloop },
{ "backend", loop_backend },
{ "fork", loop_fork },
{ "__gc", loop_delete },
{ NULL, NULL }
};
luaL_newmetatable(L, LOOP_MT);
luaL_setfuncs(L, fns, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
return 1;
}
/**
* Create a table intended as the loop object, sets the metatable,
* registers it, creates the evlua_loop struct appropriately, and sets
* the userdata fenv to an empty table. This table is used to store
* references to active watchers so the garbage collector does not
* prematurely collect the watchers.
*
* [-0, +1, v]
*/
static struct ev_loop** loop_alloc(lua_State *L) {
struct ev_loop** loop = (struct ev_loop**)
obj_new(L, sizeof(struct ev_loop*), LOOP_MT);
return loop;
}
/**
* Validates that loop_i is a loop object and checks if it is the
* special UNINITIALIZED_DEFAULT_LOOP token, and if so it initializes
* the default loop. If everything checks out fine, then a pointer to
* the ev_loop object is returned.
*/
static struct ev_loop** check_loop_and_init(lua_State *L, int loop_i) {
struct ev_loop** loop_r = check_loop(L, loop_i);
if ( UNINITIALIZED_DEFAULT_LOOP == *loop_r ) {
*loop_r = ev_default_loop(EVFLAG_AUTO);
if ( NULL == *loop_r ) {
luaL_error(L,
"libev init failed, perhaps LIBEV_FLAGS environment variable "
" is causing it to select a bad backend?");
}
register_obj(L, loop_i, *loop_r);
}
return loop_r;
}
/**
* Create a new non-default loop instance.
*
* [-0, +1, ?]
*/
static int loop_new(lua_State *L) {
struct ev_loop** loop_r = loop_alloc(L);
unsigned int flags = lua_isnumber(L, 1) ?
lua_tointeger(L, 1) : EVFLAG_AUTO;
*loop_r = ev_loop_new(flags);
register_obj(L, -1, *loop_r);
return 1;
}
/**
* Delete a loop instance. Default event loop is ignored.
*/
static int loop_delete(lua_State *L) {
struct ev_loop* loop = *check_loop(L, 1);
if ( UNINITIALIZED_DEFAULT_LOOP == loop ||
ev_is_default_loop(loop) ) return 0;
ev_loop_destroy(loop);
return 0;
}
/**
* Must be called aftert start()ing a watcher. This is necessary so
* that the watcher is not prematurely garbage collected, and if the
* watcher is "marked as a daemon", then ev_unref() is called so that
* this watcher does not prevent the event loop from terminating.
*
* is_daemon may be -1 to specify that if this watcher is already
* registered in the event loop, then use the current is_daemon value,
* otherwise set is_daemon to false.
*
* [-0, +0, m]
*/
static void loop_start_watcher(lua_State* L, int loop_i, int watcher_i, int is_daemon) {
int current_is_daemon = -1;
loop_i = lua_absindex(L, loop_i);
watcher_i = lua_absindex(L, watcher_i);
/* Check that watcher isn't already registered: */
lua_getuservalue(L, loop_i);
lua_pushvalue(L, watcher_i);
lua_rawget(L, -2);
if ( ! lua_isnil(L, -1) ) {
current_is_daemon = lua_toboolean(L, -1);
}
lua_pop(L, 1);
if ( is_daemon == -1 ) {
/* Set is_daemon properly for -1 case. */
if ( current_is_daemon == -1 )
is_daemon = 0;
else
is_daemon = current_is_daemon;
}
/* Currently not initialized, or daemon status change? */
if ( -1 == current_is_daemon ||
current_is_daemon ^ is_daemon )
{
lua_pushvalue(L, watcher_i);
lua_pushboolean(L, is_daemon);
lua_rawset(L, -3);
lua_pop(L, 1);
if ( ! is_daemon && -1 == current_is_daemon ) {
/* Do nothing, we are initializing a non-daemon */
} else if ( is_daemon ) {
/* unref() so that we are a "daemon" */
ev_unref(*check_loop_and_init(L, loop_i));
} else {
/* ref() so that we are no longer a "daemon" */
ev_ref(*check_loop_and_init(L, loop_i));
}
}
}
/**
* Must be called aftert stop()ing a watcher, or after a watcher is
* automatically stopped (such as a non-repeating timer expiring).
* This is necessary so that the watcher is not prematurely garbage
* collected, and if the watcher is "marked as a daemon", then
* ev_ref() is called in order to "undo" what was done in
* loop_add_watcher().
*
* [-0, +0, m]
*/
static void loop_stop_watcher(lua_State* L, int loop_i, int watcher_i) {
loop_i = lua_absindex(L, loop_i);
watcher_i = lua_absindex(L, watcher_i);
lua_getuservalue(L, loop_i);
lua_pushvalue(L, watcher_i);
lua_rawget(L, -2);
if ( lua_toboolean(L, -1) ) ev_ref(*check_loop_and_init(L, loop_i));
lua_pop(L, 1);
lua_pushvalue(L, watcher_i);
lua_pushnil(L);
lua_rawset(L, -3);
lua_pop(L, 1);
}
/**
* Check if this is the default event loop.
*/
static int loop_is_default(lua_State *L) {
struct ev_loop* loop = *check_loop(L, 1);
lua_pushboolean(L,
UNINITIALIZED_DEFAULT_LOOP == loop ||
ev_is_default_loop(loop) );
return 1;
}
/**
* How many times have we iterated though the event loop?
*/
static int loop_iteration(lua_State *L) {
struct ev_loop* loop = *check_loop(L, 1);
lua_pushinteger(L,
UNINITIALIZED_DEFAULT_LOOP == loop ?
0 : ev_loop_count(loop));
return 1;
}
/**
* How many times have we iterated though the event loop?
*/
static int loop_depth(lua_State *L) {
struct ev_loop* loop = *check_loop(L, 1);
lua_pushinteger(L,
UNINITIALIZED_DEFAULT_LOOP == loop ?
0 : ev_loop_depth(loop));
return 1;
}
/**
* The current event loop time.
*/
static int loop_now(lua_State *L) {
lua_pushnumber(L, ev_now(*check_loop_and_init(L, 1)));
return 1;
}
/**
* Sync the event loop time with "real" time and return the "real"
* time.
*/
static int loop_update_now(lua_State *L) {
struct ev_loop* loop = *check_loop_and_init(L, 1);
ev_now_update(loop);
lua_pushnumber(L, ev_now(loop));
return 1;
}
/**
* Actually do the event loop.
*/
static int loop_loop(lua_State *L) {
struct ev_loop *loop = *check_loop_and_init(L, 1);
void *old_userdata = ev_userdata(loop);
ev_set_userdata(loop, L);
ev_loop(loop, 0);
ev_set_userdata(loop, old_userdata);
return 0;
}
/**
* "Quit" out of the event loop.
*/
static int loop_unloop(lua_State *L) {
ev_unloop(*check_loop_and_init(L, 1), EVUNLOOP_ALL);
return 0;
}
/**
* Determine which backend is implementing the event loop.
*
* [-0, +1, m]
*/
static int loop_backend(lua_State *L) {
lua_pushinteger(L, ev_backend(*check_loop_and_init(L, 1)));
return 1;
}
/**
* Make it safe to resume an event loop after the fork(2) system call.
*
* [-0, +0, m]
*/
static int loop_fork(lua_State *L) {
struct ev_loop* loop = *check_loop(L, 1);
if ( UNINITIALIZED_DEFAULT_LOOP == loop ) {
// Do nothing!
} else if ( ev_is_default_loop(loop) ) {
ev_default_fork();
} else {
ev_loop_fork(loop);
}
return 0;
}