Comparison

util-src/poll.c @ 9313:b95ef295c66d

util.poll: Import poll library with epoll and select support
author Kim Alvefur <zash@zash.se>
date Wed, 16 May 2018 23:56:34 +0200
child 9317:d8496858c809
comparison
equal deleted inserted replaced
9312:6cab07323274 9313:b95ef295c66d
1
2 /*
3 * Lua polling library
4 * Copyright (C) 2017-2018 Kim Alvefur
5 *
6 * This project is MIT licensed. Please see the
7 * COPYING file in the source package for more information.
8 *
9 */
10
11 #include <unistd.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #ifdef __linux__
16 #define USE_EPOLL
17 #endif
18
19 #ifdef USE_EPOLL
20 #include <sys/epoll.h>
21 #ifndef MAX_EVENTS
22 #define MAX_EVENTS 10
23 #endif
24 #else
25 #include <sys/select.h>
26 #endif
27
28 #include <lualib.h>
29 #include <lauxlib.h>
30
31 #ifdef USE_EPOLL
32 #define STATE_MT "util.poll.epoll"
33 #else
34 #define STATE_MT "util.poll.select"
35 #endif
36
37 /*
38 * Structure to keep state for each type of API
39 */
40 typedef struct Lpoll_state {
41 int processed;
42 #ifdef USE_EPOLL
43 int epoll_fd;
44 struct epoll_event events[MAX_EVENTS];
45 #else
46 fd_set wantread;
47 fd_set wantwrite;
48 fd_set readable;
49 fd_set writable;
50 fd_set all;
51 fd_set err;
52 #endif
53 } Lpoll_state;
54
55 /*
56 * Add an FD to be watched
57 */
58 int Ladd(lua_State *L) {
59 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
60 int fd = luaL_checkinteger(L, 2);
61
62 int wantread = lua_toboolean(L, 3);
63 int wantwrite = lua_toboolean(L, 4);
64
65 #ifdef USE_EPOLL
66 struct epoll_event event;
67 event.data.fd = fd;
68 event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
69
70 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
71
72 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
73
74 if(ret < 0) {
75 ret = errno;
76 lua_pushnil(L);
77 lua_pushstring(L, strerror(ret));
78 lua_pushinteger(L, ret);
79 return 3;
80 }
81
82 lua_pushboolean(L, 1);
83 return 1;
84
85 #else
86
87 if(FD_ISSET(fd, &state->all)) {
88 lua_pushnil(L);
89 lua_pushstring(L, strerror(EEXIST));
90 lua_pushinteger(L, EEXIST);
91 }
92
93 FD_CLR(fd, &state->readable);
94 FD_CLR(fd, &state->writable);
95 FD_CLR(fd, &state->err);
96
97 FD_SET(fd, &state->all);
98
99 if(wantread) {
100 FD_SET(fd, &state->wantread);
101 }
102 else {
103 FD_CLR(fd, &state->wantread);
104 }
105
106 if(wantwrite) {
107 FD_SET(fd, &state->wantwrite);
108 }
109 else {
110 FD_CLR(fd, &state->wantwrite);
111 }
112
113 lua_pushboolean(L, 1);
114 return 1;
115 #endif
116 }
117
118 /*
119 * Set events to watch for, readable and/or writable
120 */
121 int Lset(lua_State *L) {
122 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
123 int fd = luaL_checkinteger(L, 2);
124
125 #ifdef USE_EPOLL
126
127 int wantread = lua_toboolean(L, 3);
128 int wantwrite = lua_toboolean(L, 4);
129
130 struct epoll_event event;
131 event.data.fd = fd;
132 event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
133
134 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
135
136 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event);
137
138 if(ret == 0) {
139 lua_pushboolean(L, 1);
140 return 1;
141 }
142 else {
143 ret = errno;
144 lua_pushnil(L);
145 lua_pushstring(L, strerror(ret));
146 lua_pushinteger(L, ret);
147 return 3;
148 }
149
150 #else
151
152 if(!FD_ISSET(fd, &state->all)) {
153 lua_pushnil(L);
154 lua_pushstring(L, strerror(ENOENT));
155 lua_pushinteger(L, ENOENT);
156 }
157
158 if(!lua_isnoneornil(L, 3)) {
159 if(lua_toboolean(L, 3)) {
160 FD_SET(fd, &state->wantread);
161 }
162 else {
163 FD_CLR(fd, &state->wantread);
164 }
165 }
166
167 if(!lua_isnoneornil(L, 4)) {
168 if(lua_toboolean(L, 4)) {
169 FD_SET(fd, &state->wantwrite);
170 }
171 else {
172 FD_CLR(fd, &state->wantwrite);
173 }
174 }
175
176 lua_pushboolean(L, 1);
177 return 1;
178 #endif
179 }
180
181 /*
182 * Remove FDs
183 */
184 int Ldel(lua_State *L) {
185 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
186 int fd = luaL_checkinteger(L, 2);
187
188 #ifdef USE_EPOLL
189
190 struct epoll_event event;
191 event.data.fd = fd;
192
193 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event);
194
195 if(ret == 0) {
196 lua_pushboolean(L, 1);
197 return 1;
198 }
199 else {
200 ret = errno;
201 lua_pushnil(L);
202 lua_pushstring(L, strerror(ret));
203 lua_pushinteger(L, ret);
204 return 3;
205 }
206
207 #else
208
209 if(!FD_ISSET(fd, &state->all)) {
210 lua_pushnil(L);
211 lua_pushstring(L, strerror(ENOENT));
212 lua_pushinteger(L, ENOENT);
213 }
214
215 FD_CLR(fd, &state->wantread);
216 FD_CLR(fd, &state->wantwrite);
217 FD_CLR(fd, &state->readable);
218 FD_CLR(fd, &state->writable);
219 FD_CLR(fd, &state->all);
220 FD_CLR(fd, &state->err);
221
222 lua_pushboolean(L, 1);
223 return 1;
224 #endif
225 }
226
227
228 /*
229 * Check previously manipulated event state for FDs ready for reading or writing
230 */
231 inline int Lpushevent(lua_State *L, struct Lpoll_state *state) {
232 #ifdef USE_EPOLL
233
234 if(state->processed > 0) {
235 state->processed--;
236 struct epoll_event event = state->events[state->processed];
237 lua_pushinteger(L, event.data.fd);
238 lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR));
239 lua_pushboolean(L, event.events & EPOLLOUT);
240 return 3;
241 }
242
243 #else
244
245 for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) {
246 if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) {
247 lua_pushinteger(L, fd);
248 lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err));
249 lua_pushboolean(L, FD_ISSET(fd, &state->writable));
250 FD_CLR(fd, &state->readable);
251 FD_CLR(fd, &state->writable);
252 FD_CLR(fd, &state->err);
253 state->processed = fd;
254 return 3;
255 }
256 }
257
258 #endif
259 return 0;
260 }
261
262 /*
263 * Wait for event
264 */
265 int Lwait(lua_State *L) {
266 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
267
268 int ret = Lpushevent(L, state);
269
270 if(ret != 0) {
271 return ret;
272 }
273
274 lua_Number timeout = luaL_checknumber(L, 2);
275 luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
276
277 #ifdef USE_EPOLL
278 ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
279 #else
280 /*
281 * select(2) mutates the fd_sets passed to it so in order to not
282 * have to recreate it manually every time a copy is made.
283 */
284 memcpy(&state->readable, &state->wantread, sizeof(fd_set));
285 memcpy(&state->writable, &state->wantwrite, sizeof(fd_set));
286 memcpy(&state->err, &state->all, sizeof(fd_set));
287
288 struct timeval tv;
289 tv.tv_sec = (time_t)timeout;
290 tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000;
291
292 ret = select(FD_SETSIZE, &state->readable, &state->writable, NULL, &tv);
293 #endif
294
295 if(ret == 0) {
296 lua_pushnil(L);
297 lua_pushstring(L, "timeout");
298 return 2;
299 }
300 else if(ret < 0) {
301 ret = errno;
302 lua_pushnil(L);
303 lua_pushstring(L, strerror(ret));
304 lua_pushinteger(L, ret);
305 return 3;
306 }
307
308 /*
309 * Search for the first ready FD and return it
310 */
311 #ifdef USE_EPOLL
312 state->processed = ret;
313 #else
314 state->processed = -1;
315 #endif
316 return Lpushevent(L, state);
317 }
318
319 #ifdef USE_EPOLL
320 /*
321 * Return Epoll FD
322 */
323 int Lgetfd(lua_State *L) {
324 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
325 lua_pushinteger(L, state->epoll_fd);
326 return 1;
327 }
328
329 /*
330 * Close epoll FD
331 */
332 int Lgc(lua_State *L) {
333 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
334
335 if(close(state->epoll_fd) == 0) {
336 state->epoll_fd = -1;
337 }
338 else {
339 lua_pushstring(L, strerror(errno));
340 lua_error(L);
341 }
342
343 return 0;
344 }
345 #endif
346
347 /*
348 * String representation
349 */
350 int Ltos(lua_State *L) {
351 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
352 lua_pushfstring(L, "%s: %p", STATE_MT, state);
353 return 1;
354 }
355
356 /*
357 * Create a new context
358 */
359 int Lnew(lua_State *L) {
360 /* Allocate state */
361 Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
362 luaL_setmetatable(L, STATE_MT);
363
364 /* Initialize state */
365 #ifdef USE_EPOLL
366 state->epoll_fd = epoll_create1(0);
367 state->processed = 0;
368 #else
369 FD_ZERO(&state->wantread);
370 FD_ZERO(&state->wantwrite);
371 FD_ZERO(&state->readable);
372 FD_ZERO(&state->writable);
373 state->processed = FD_SETSIZE;
374 #endif
375
376 return 1;
377 }
378
379 /*
380 * Open library
381 */
382 int luaopen_util_poll(lua_State *L) {
383 luaL_checkversion(L);
384
385 luaL_newmetatable(L, STATE_MT);
386 {
387
388 lua_pushliteral(L, STATE_MT);
389 lua_setfield(L, -2, "__name");
390
391 lua_pushcfunction(L, Ltos);
392 lua_setfield(L, -2, "__tostring");
393
394 lua_createtable(L, 0, 2);
395 {
396 lua_pushcfunction(L, Ladd);
397 lua_setfield(L, -2, "add");
398 lua_pushcfunction(L, Lset);
399 lua_setfield(L, -2, "set");
400 lua_pushcfunction(L, Ldel);
401 lua_setfield(L, -2, "del");
402 lua_pushcfunction(L, Lwait);
403 lua_setfield(L, -2, "wait");
404 #ifdef USE_EPOLL
405 lua_pushcfunction(L, Lgetfd);
406 lua_setfield(L, -2, "getfd");
407 #endif
408 }
409 lua_setfield(L, -2, "__index");
410
411 #ifdef USE_EPOLL
412 lua_pushcfunction(L, Lgc);
413 lua_setfield(L, -2, "__gc");
414 #endif
415 }
416
417 lua_createtable(L, 0, 1);
418 {
419 lua_pushcfunction(L, Lnew);
420 lua_setfield(L, -2, "new");
421 }
422 return 1;
423 }
424