comparison util-src/poll.c @ 10411:db2a06b9ff98

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Sat, 16 Nov 2019 16:52:31 +0100
parents 46a7792fdac5
children 6eb5d2bb11af
comparison
equal deleted inserted replaced
10410:659b577f280c 10411:db2a06b9ff98
57 } Lpoll_state; 57 } Lpoll_state;
58 58
59 /* 59 /*
60 * Add an FD to be watched 60 * Add an FD to be watched
61 */ 61 */
62 int Ladd(lua_State *L) { 62 static int Ladd(lua_State *L) {
63 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 63 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
64 int fd = luaL_checkinteger(L, 2); 64 int fd = luaL_checkinteger(L, 2);
65 65
66 int wantread = lua_toboolean(L, 3); 66 int wantread = lua_toboolean(L, 3);
67 int wantwrite = lua_toboolean(L, 4); 67 int wantwrite = lua_toboolean(L, 4);
135 } 135 }
136 136
137 /* 137 /*
138 * Set events to watch for, readable and/or writable 138 * Set events to watch for, readable and/or writable
139 */ 139 */
140 int Lset(lua_State *L) { 140 static int Lset(lua_State *L) {
141 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 141 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
142 int fd = luaL_checkinteger(L, 2); 142 int fd = luaL_checkinteger(L, 2);
143 143
144 #ifdef USE_EPOLL 144 #ifdef USE_EPOLL
145 145
170 170
171 if(!FD_ISSET(fd, &state->all)) { 171 if(!FD_ISSET(fd, &state->all)) {
172 lua_pushnil(L); 172 lua_pushnil(L);
173 lua_pushstring(L, strerror(ENOENT)); 173 lua_pushstring(L, strerror(ENOENT));
174 lua_pushinteger(L, ENOENT); 174 lua_pushinteger(L, ENOENT);
175 return 3;
175 } 176 }
176 177
177 if(!lua_isnoneornil(L, 3)) { 178 if(!lua_isnoneornil(L, 3)) {
178 if(lua_toboolean(L, 3)) { 179 if(lua_toboolean(L, 3)) {
179 FD_SET(fd, &state->wantread); 180 FD_SET(fd, &state->wantread);
198 } 199 }
199 200
200 /* 201 /*
201 * Remove FDs 202 * Remove FDs
202 */ 203 */
203 int Ldel(lua_State *L) { 204 static int Ldel(lua_State *L) {
204 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 205 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
205 int fd = luaL_checkinteger(L, 2); 206 int fd = luaL_checkinteger(L, 2);
206 207
207 #ifdef USE_EPOLL 208 #ifdef USE_EPOLL
208 209
227 228
228 if(!FD_ISSET(fd, &state->all)) { 229 if(!FD_ISSET(fd, &state->all)) {
229 lua_pushnil(L); 230 lua_pushnil(L);
230 lua_pushstring(L, strerror(ENOENT)); 231 lua_pushstring(L, strerror(ENOENT));
231 lua_pushinteger(L, ENOENT); 232 lua_pushinteger(L, ENOENT);
233 return 3;
232 } 234 }
233 235
234 FD_CLR(fd, &state->wantread); 236 FD_CLR(fd, &state->wantread);
235 FD_CLR(fd, &state->wantwrite); 237 FD_CLR(fd, &state->wantwrite);
236 FD_CLR(fd, &state->readable); 238 FD_CLR(fd, &state->readable);
245 247
246 248
247 /* 249 /*
248 * Check previously manipulated event state for FDs ready for reading or writing 250 * Check previously manipulated event state for FDs ready for reading or writing
249 */ 251 */
250 int Lpushevent(lua_State *L, struct Lpoll_state *state) { 252 static int Lpushevent(lua_State *L, struct Lpoll_state *state) {
251 #ifdef USE_EPOLL 253 #ifdef USE_EPOLL
252 254
253 if(state->processed > 0) { 255 if(state->processed > 0) {
254 state->processed--; 256 state->processed--;
255 struct epoll_event event = state->events[state->processed]; 257 struct epoll_event event = state->events[state->processed];
279 } 281 }
280 282
281 /* 283 /*
282 * Wait for event 284 * Wait for event
283 */ 285 */
284 int Lwait(lua_State *L) { 286 static int Lwait(lua_State *L) {
285 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 287 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
286 288
287 int ret = Lpushevent(L, state); 289 int ret = Lpushevent(L, state);
288 290
289 if(ret != 0) { 291 if(ret != 0) {
342 344
343 #ifdef USE_EPOLL 345 #ifdef USE_EPOLL
344 /* 346 /*
345 * Return Epoll FD 347 * Return Epoll FD
346 */ 348 */
347 int Lgetfd(lua_State *L) { 349 static int Lgetfd(lua_State *L) {
348 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 350 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
349 lua_pushinteger(L, state->epoll_fd); 351 lua_pushinteger(L, state->epoll_fd);
350 return 1; 352 return 1;
351 } 353 }
352 354
353 /* 355 /*
354 * Close epoll FD 356 * Close epoll FD
355 */ 357 */
356 int Lgc(lua_State *L) { 358 static int Lgc(lua_State *L) {
357 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 359 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
358 360
359 if(state->epoll_fd == -1) { 361 if(state->epoll_fd == -1) {
360 return 0; 362 return 0;
361 } 363 }
373 #endif 375 #endif
374 376
375 /* 377 /*
376 * String representation 378 * String representation
377 */ 379 */
378 int Ltos(lua_State *L) { 380 static int Ltos(lua_State *L) {
379 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT); 381 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
380 lua_pushfstring(L, "%s: %p", STATE_MT, state); 382 lua_pushfstring(L, "%s: %p", STATE_MT, state);
381 return 1; 383 return 1;
382 } 384 }
383 385
384 /* 386 /*
385 * Create a new context 387 * Create a new context
386 */ 388 */
387 int Lnew(lua_State *L) { 389 static int Lnew(lua_State *L) {
388 /* Allocate state */ 390 /* Allocate state */
389 Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state)); 391 Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
390 luaL_setmetatable(L, STATE_MT); 392 luaL_setmetatable(L, STATE_MT);
391 393
392 /* Initialize state */ 394 /* Initialize state */