-
Notifications
You must be signed in to change notification settings - Fork 55
/
lua_ev.h
228 lines (194 loc) · 8.11 KB
/
lua_ev.h
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
/**
* This is a private header file. If you use it outside of this
* package, then you will be surprised by any changes made.
*
* Documentation for functions declared here is with the function
* implementation.
*/
/**
* Compatibility defines
*/
#if EV_VERSION_MAJOR < 3 || (EV_VERSION_MAJOR == 3 && EV_VERSION_MINOR < 7)
# warning lua-ev requires version 3.7 or newer of libev
#endif
#if LUA_VERSION_NUM <= 501
/** Backwards compatibility shims: */
#define lua_absindex(L, i) \
((i) > 0 || (i) <= LUA_REGISTRYINDEX ? \
(i) : lua_gettop(L) + (i) + 1)
#define lua_setuservalue(L, i) lua_setfenv((L), (i))
#define lua_getuservalue(L, i) lua_getfenv((L), (i))
/* NOTE: this only works if nups == 0! */
#define luaL_setfuncs(L, fns, nups) luaL_register((L), NULL, (fns))
#endif
/**
* Define the names used for the metatables.
*/
#define LOOP_MT "ev{loop}"
#define IO_MT "ev{io}"
#define ASYNC_MT "ev{async}"
#define TIMER_MT "ev{timer}"
#define SIGNAL_MT "ev{signal}"
#define IDLE_MT "ev{idle}"
#define CHILD_MT "ev{child}"
#define STAT_MT "ev{stat}"
/**
* Special token to represent the uninitialized default loop. This is
* so we can defer initializing the default loop for as long as
* possible.
*/
#define UNINITIALIZED_DEFAULT_LOOP (struct ev_loop*)1
/**
* The location in the fenv of the watcher that contains the callback
* function.
*/
#define WATCHER_FN 1
/**
* The location in the fenv of the shadow table.
*/
#define WATCHER_SHADOW 2
/**
* Various "check" functions simply call luaL_checkudata() and do the
* appropriate casting, with the exception of check_watcher which is
* implemented as a C function.
*/
/**
* If there is any chance that the loop is not initialized yet, then
* please use check_loop_and_init() instead! Also note that the
* loop userdata is a pointer, so don't forget to de-reference the
* result.
*/
#define check_loop(L, narg) \
((struct ev_loop**) luaL_checkudata((L), (narg), LOOP_MT))
#define check_timer(L, narg) \
((struct ev_timer*) luaL_checkudata((L), (narg), TIMER_MT))
#define check_io(L, narg) \
((struct ev_io*) luaL_checkudata((L), (narg), IO_MT))
#define check_async(L, narg) \
((struct ev_async*) luaL_checkudata((L), (narg), ASYNC_MT))
#define check_signal(L, narg) \
((struct ev_signal*) luaL_checkudata((L), (narg), SIGNAL_MT))
#define check_idle(L, narg) \
((struct ev_idle*) luaL_checkudata((L), (narg), IDLE_MT))
#define check_child(L, narg) \
((struct ev_child*) luaL_checkudata((L), (narg), CHILD_MT))
#define check_stat(L, narg) \
((struct ev_stat*) luaL_checkudata((L), (narg), STAT_MT))
/**
* Generic functions:
*/
static int version(lua_State *L);
static int traceback(lua_State *L);
/**
* Loop functions:
*/
static int luaopen_ev_loop(lua_State *L);
static int create_loop_mt(lua_State *L);
static struct ev_loop** loop_alloc(lua_State *L);
static struct ev_loop** check_loop_and_init(lua_State *L, int loop_i);
static int loop_new(lua_State *L);
static int loop_delete(lua_State *L);
static void loop_start_watcher(lua_State* L, int loop_i, int watcher_i, int is_daemon);
static void loop_stop_watcher(lua_State* L, int loop_i, int watcher_i);
static int loop_is_default(lua_State *L);
static int loop_iteration(lua_State *L);
static int loop_depth(lua_State *L);
static int loop_now(lua_State *L);
static int loop_update_now(lua_State *L);
static int loop_loop(lua_State *L);
static int loop_unloop(lua_State *L);
static int loop_backend(lua_State *L);
static int loop_fork(lua_State *L);
/**
* Object functions:
*/
static void create_obj_registry(lua_State *L);
static int obj_count(lua_State *L);
static void* obj_new(lua_State* L, size_t size, const char* tname);
static int obj_newindex(lua_State *L);
static int obj_index(lua_State *L);
static int push_objs(lua_State* L, void** objs);
/**
* Watcher functions:
*/
static int add_watcher_mt(lua_State *L);
static int watcher_is_active(lua_State *L);
static int watcher_is_pending(lua_State *L);
static int watcher_clear_pending(lua_State *L);
static void* watcher_new(lua_State* L, size_t size, const char* lua_type);
static int watcher_callback(lua_State *L);
static int watcher_priority(lua_State *L);
static void watcher_cb(struct ev_loop *loop, void *watcher, int revents);
static struct ev_watcher* check_watcher(lua_State *L, int watcher_i);
/**
* Timer functions:
*/
static int luaopen_ev_timer(lua_State *L);
static int create_timer_mt(lua_State *L);
static int timer_new(lua_State* L);
static void timer_cb(struct ev_loop* loop, ev_timer* timer, int revents);
static int timer_again(lua_State *L);
static int timer_stop(lua_State *L);
static int timer_start(lua_State *L);
static int timer_clear_pending(lua_State *L);
/**
* IO functions:
*/
static int luaopen_ev_io(lua_State *L);
static int create_io_mt(lua_State *L);
static int io_new(lua_State* L);
static void io_cb(struct ev_loop* loop, ev_io* io, int revents);
static int io_stop(lua_State *L);
static int io_start(lua_State *L);
static int io_getfd(lua_State *L);
/**
* Async functions:
*/
static int luaopen_ev_async(lua_State *L);
static int create_async_mt(lua_State *L);
static int async_new(lua_State* L);
static void async_cb(struct ev_loop* loop, ev_async* async, int revents);
static int async_send(lua_State *L);
static int async_stop(lua_State *L);
static int async_start(lua_State *L);
/**
* Signal functions:
*/
static int luaopen_ev_signal(lua_State *L);
static int create_signal_mt(lua_State *L);
static int signal_new(lua_State* L);
static void signal_cb(struct ev_loop* loop, ev_signal* sig, int revents);
static int signal_stop(lua_State *L);
static int signal_start(lua_State *L);
/**
* Idle functions:
*/
static int luaopen_ev_idle(lua_State *L);
static int create_idle_mt(lua_State *L);
static int idle_new(lua_State* L);
static void idle_cb(struct ev_loop* loop, ev_idle* idle, int revents);
static int idle_stop(lua_State *L);
static int idle_start(lua_State *L);
/**
* Child functions:
*/
static int luaopen_ev_child(lua_State *L);
static int create_child_mt(lua_State *L);
static int child_new(lua_State* L);
static void child_cb(struct ev_loop* loop, ev_child* sig, int revents);
static int child_stop(lua_State *L);
static int child_start(lua_State *L);
static int child_getpid(lua_State *L);
static int child_getrpid(lua_State *L);
static int child_getstatus(lua_State *L);
/**
* Stat functions:
*/
static int luaopen_ev_stat(lua_State *L);
static int create_stat_mt(lua_State *L);
static int stat_new(lua_State* L);
static void stat_cb(struct ev_loop* loop, ev_stat* sig, int revents);
static int stat_stop(lua_State *L);
static int stat_start(lua_State *L);
static int stat_start(lua_State *L);
static int stat_getdata(lua_State *L);