Annotate

util-src/poll.c @ 12315:cf2086a1bd45

util.poll: Expose API (epoll or select) used Could he handy to know for debugging or decisions
author Kim Alvefur <zash@zash.se>
date Sun, 27 Feb 2022 14:36:43 +0100
parent 12314:898554323338
child 12316:6bb2f660f689
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 * Lua polling library
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 * Copyright (C) 2017-2018 Kim Alvefur
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 *
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 * This project is MIT licensed. Please see the
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 * COPYING file in the source package for more information.
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 *
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 #include <unistd.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 #include <string.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 #include <errno.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
15 #if defined(__linux__)
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 #define USE_EPOLL
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
17 #define POLL_BACKEND "epoll"
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
18 #else
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
19 #define USE_SELECT
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
20 #define POLL_BACKEND "select"
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 #include <sys/epoll.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 #ifndef MAX_EVENTS
9450
b890ceb1c24f util.poll: Increase max epoll events per call
Kim Alvefur <zash@zash.se>
parents: 9448
diff changeset
26 #define MAX_EVENTS 64
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
28 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
29 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 #include <sys/select.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 #include <lualib.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 #include <lauxlib.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
36 #define STATE_MT "util.poll<" POLL_BACKEND ">"
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
9318
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
38 #if (LUA_VERSION_NUM == 501)
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
39 #define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
40 #endif
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
41 #if (LUA_VERSION_NUM < 504)
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
42 #define luaL_pushfail lua_pushnil
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
43 #endif
9318
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
44
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 * Structure to keep state for each type of API
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 typedef struct Lpoll_state {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 int processed;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 int epoll_fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 struct epoll_event events[MAX_EVENTS];
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
53 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
54 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 fd_set wantread;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 fd_set wantwrite;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 fd_set readable;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 fd_set writable;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 fd_set all;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 fd_set err;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 } Lpoll_state;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 * Add an FD to be watched
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
67 static int Ladd(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 int fd = luaL_checkinteger(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 int wantread = lua_toboolean(L, 3);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 int wantwrite = lua_toboolean(L, 4);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
74 if(fd < 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
75 luaL_pushfail(L);
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
76 lua_pushstring(L, strerror(EBADF));
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
77 lua_pushinteger(L, EBADF);
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
78 return 3;
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
79 }
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
80
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
92 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
101 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
102 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
104 if(fd > FD_SETSIZE) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
105 luaL_pushfail(L);
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
106 lua_pushstring(L, strerror(EBADF));
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
107 lua_pushinteger(L, EBADF);
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
108 return 3;
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
109 }
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
110
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 if(FD_ISSET(fd, &state->all)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
112 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 lua_pushstring(L, strerror(EEXIST));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 lua_pushinteger(L, EEXIST);
9446
6b4d28eb19cf util.poll: Fix missing return for adding duplicate FD
Kim Alvefur <zash@zash.se>
parents: 9440
diff changeset
115 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 FD_SET(fd, &state->all);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 if(wantread) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 if(wantwrite) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 * Set events to watch for, readable and/or writable
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
146 static int Lset(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 int fd = luaL_checkinteger(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 int wantread = lua_toboolean(L, 3);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 int wantwrite = lua_toboolean(L, 4);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
169 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
175 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
176 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 if(!FD_ISSET(fd, &state->all)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
179 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 lua_pushinteger(L, ENOENT);
10096
46a7792fdac5 util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents: 9954
diff changeset
182 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 if(!lua_isnoneornil(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 if(lua_toboolean(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 if(!lua_isnoneornil(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 if(lua_toboolean(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 * Remove FDs
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
211 static int Ldel(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 int fd = luaL_checkinteger(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
228 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
234 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
235 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 if(!FD_ISSET(fd, &state->all)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
238 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 lua_pushinteger(L, ENOENT);
10096
46a7792fdac5 util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents: 9954
diff changeset
241 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 FD_CLR(fd, &state->all);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 * Check previously manipulated event state for FDs ready for reading or writing
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
260 static int Lpushevent(lua_State *L, struct Lpoll_state *state) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 if(state->processed > 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 state->processed--;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 struct epoll_event event = state->events[state->processed];
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 lua_pushinteger(L, event.data.fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 lua_pushboolean(L, event.events & EPOLLOUT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
272 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
273 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 lua_pushinteger(L, fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 lua_pushboolean(L, FD_ISSET(fd, &state->writable));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 state->processed = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 return 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 * Wait for event
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
295 static int Lwait(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 int ret = Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 if(ret != 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 return ret;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 lua_Number timeout = luaL_checknumber(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
309 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
310 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 * select(2) mutates the fd_sets passed to it so in order to not
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 * have to recreate it manually every time a copy is made.
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 memcpy(&state->readable, &state->wantread, sizeof(fd_set));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 memcpy(&state->writable, &state->wantwrite, sizeof(fd_set));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 memcpy(&state->err, &state->all, sizeof(fd_set));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 struct timeval tv;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 tv.tv_sec = (time_t)timeout;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322
9437
b202aa1e2d7b util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
323 ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 if(ret == 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
327 /* Is this an error? */
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 lua_pushstring(L, "timeout");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 return 2;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 }
9508
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
332 else if(ret < 0 && errno == EINTR) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
333 /* Is this an error? */
9508
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
334 lua_pushnil(L);
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
335 lua_pushstring(L, "signal");
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
336 return 2;
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
337 }
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 else if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
340 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 * Search for the first ready FD and return it
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 state->processed = ret;
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
351 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
352 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 state->processed = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 return Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 * Return Epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
362 static int Lgetfd(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 lua_pushinteger(L, state->epoll_fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 * Close epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
371 static int Lgc(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373
9478
bd178ed0459b util.poll: Fix inverted logic
Kim Alvefur <zash@zash.se>
parents: 9476
diff changeset
374 if(state->epoll_fd == -1) {
9475
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
375 return 0;
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
376 }
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
377
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 if(close(state->epoll_fd) == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 state->epoll_fd = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 lua_pushstring(L, strerror(errno));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 lua_error(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 return 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 * String representation
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
393 static int Ltos(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 lua_pushfstring(L, "%s: %p", STATE_MT, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 * Create a new context
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
402 static int Lnew(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 /* Allocate state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404 Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 luaL_setmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 /* Initialize state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 #ifdef USE_EPOLL
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
409 state->epoll_fd = -1;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 state->processed = 0;
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
411
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
412 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
413
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
414 if(epoll_fd <= 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
415 luaL_pushfail(L);
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
416 lua_pushstring(L, strerror(errno));
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
417 lua_pushinteger(L, errno);
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
418 return 3;
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
419 }
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
420
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
421 state->epoll_fd = epoll_fd;
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
422 #endif
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
423 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 FD_ZERO(&state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 FD_ZERO(&state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 FD_ZERO(&state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 FD_ZERO(&state->writable);
9448
352e8b75c1ae util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents: 9447
diff changeset
428 FD_ZERO(&state->all);
352e8b75c1ae util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents: 9447
diff changeset
429 FD_ZERO(&state->err);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 state->processed = FD_SETSIZE;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 * Open library
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 int luaopen_util_poll(lua_State *L) {
9318
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
440 #if (LUA_VERSION_NUM > 501)
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
441 luaL_checkversion(L);
9318
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
442 #endif
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
443
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 luaL_newmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
446
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 lua_pushliteral(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
448 lua_setfield(L, -2, "__name");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
449
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
450 lua_pushcfunction(L, Ltos);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
451 lua_setfield(L, -2, "__tostring");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
452
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
453 lua_createtable(L, 0, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455 lua_pushcfunction(L, Ladd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
456 lua_setfield(L, -2, "add");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457 lua_pushcfunction(L, Lset);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 lua_setfield(L, -2, "set");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 lua_pushcfunction(L, Ldel);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 lua_setfield(L, -2, "del");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461 lua_pushcfunction(L, Lwait);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
462 lua_setfield(L, -2, "wait");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
463 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
464 lua_pushcfunction(L, Lgetfd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
465 lua_setfield(L, -2, "getfd");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
466 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
467 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
468 lua_setfield(L, -2, "__index");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
469
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
470 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 lua_pushcfunction(L, Lgc);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
472 lua_setfield(L, -2, "__gc");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
475
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
476 lua_createtable(L, 0, 3);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
477 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
478 lua_pushcfunction(L, Lnew);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
479 lua_setfield(L, -2, "new");
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
480
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
481 #define push_errno(named_error) lua_pushinteger(L, named_error);\
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
482 lua_setfield(L, -2, #named_error);
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
483
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
484 push_errno(EEXIST);
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
485 push_errno(ENOENT);
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
486
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
487 lua_pushliteral(L, POLL_BACKEND);
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
488 lua_setfield(L, -2, "api");
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
489
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
490 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
491 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
492 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
493