Software /
code /
prosody
Annotate
util-src/poll.c @ 12314:898554323338
util.poll: Restructure to make adding additional system APIs easier
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 23 Feb 2022 20:30:22 +0100 |
parent | 10921:6eb5d2bb11af |
child | 12315:cf2086a1bd45 |
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 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
17 #else |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
18 #define USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 #include <sys/epoll.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 #ifndef MAX_EVENTS |
9450
b890ceb1c24f
util.poll: Increase max epoll events per call
Kim Alvefur <zash@zash.se>
parents:
9448
diff
changeset
|
24 #define MAX_EVENTS 64 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
26 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
27 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 #include <sys/select.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 #include <lualib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 #include <lauxlib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 #ifdef USE_EPOLL |
9440
1ddecde4ce98
util.poll: Tweak metatable name field
Kim Alvefur <zash@zash.se>
parents:
9437
diff
changeset
|
35 #define STATE_MT "util.poll<epoll>" |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
36 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
37 #ifdef USE_SELECT |
9440
1ddecde4ce98
util.poll: Tweak metatable name field
Kim Alvefur <zash@zash.se>
parents:
9437
diff
changeset
|
38 #define STATE_MT "util.poll<select>" |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
9318 | 41 #if (LUA_VERSION_NUM == 501) |
42 #define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2) | |
43 #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
|
44 #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
|
45 #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
|
46 #endif |
9318 | 47 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 * 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
|
50 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 typedef struct Lpoll_state { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 int processed; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 int epoll_fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 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
|
56 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
57 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 fd_set wantread; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 fd_set wantwrite; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 fd_set readable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 fd_set writable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 fd_set all; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 fd_set err; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 } Lpoll_state; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 * 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
|
69 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 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
|
75 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
|
76 |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
77 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
|
78 luaL_pushfail(L); |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
79 lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
80 lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
81 return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
82 } |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
83 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 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
|
88 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 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
|
92 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 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
|
95 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
104 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
105 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
107 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
|
108 luaL_pushfail(L); |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
109 lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
110 lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
111 return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
112 } |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
113 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 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
|
115 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 lua_pushstring(L, strerror(EEXIST)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 lua_pushinteger(L, EEXIST); |
9446
6b4d28eb19cf
util.poll: Fix missing return for adding duplicate FD
Kim Alvefur <zash@zash.se>
parents:
9440
diff
changeset
|
118 return 3; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 FD_SET(fd, &state->all); |
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 if(wantread) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 FD_SET(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 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 } |
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 if(wantwrite) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 FD_SET(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 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 * 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
|
148 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
149 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
|
150 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
|
151 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
|
152 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 #ifdef USE_EPOLL |
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 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
|
156 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
|
157 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 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
|
161 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 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
|
165 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 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
|
172 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
178 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
179 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 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
|
182 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 lua_pushinteger(L, ENOENT); |
10096
46a7792fdac5
util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents:
9954
diff
changeset
|
185 return 3; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 if(!lua_isnoneornil(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 if(lua_toboolean(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 FD_SET(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 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 if(!lua_isnoneornil(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 if(lua_toboolean(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 FD_SET(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 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 * Remove FDs |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 #ifdef USE_EPOLL |
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 struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 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
|
224 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 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
|
231 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
237 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
238 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 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
|
241 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 lua_pushinteger(L, ENOENT); |
10096
46a7792fdac5
util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents:
9954
diff
changeset
|
244 return 3; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 FD_CLR(fd, &state->all); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 #endif |
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 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 * 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
|
262 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
263 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
|
264 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 if(state->processed > 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 state->processed--; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 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
|
269 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
|
270 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
|
271 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
|
272 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
275 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
276 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 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
|
279 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
|
280 lua_pushinteger(L, fd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 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
|
282 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
|
283 FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 state->processed = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 } |
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 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 return 0; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 * Wait for event |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
298 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
|
299 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
|
300 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 int ret = Lpushevent(L, state); |
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 if(ret != 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 return ret; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 } |
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 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
|
308 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
|
309 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
311 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
|
312 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
313 #ifdef USE_SELECT |
9313
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 * 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
|
316 * 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
|
317 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 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
|
319 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
|
320 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
|
321 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 struct timeval tv; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
323 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
|
324 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
|
325 |
9437
b202aa1e2d7b
util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents:
9318
diff
changeset
|
326 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
|
327 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 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
|
330 /* Is this an error? */ |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 lua_pushstring(L, "timeout"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 return 2; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 } |
9508
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
335 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
|
336 /* Is this an error? */ |
9508
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
337 lua_pushnil(L); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
338 lua_pushstring(L, "signal"); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
339 return 2; |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
340 } |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 else if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 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
|
343 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 } |
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 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 * 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
|
351 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 state->processed = ret; |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
354 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
355 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 state->processed = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 return Lpushevent(L, state); |
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 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 * Return Epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
365 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
|
366 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
|
367 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
|
368 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 * Close epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
374 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
|
375 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
|
376 |
9478 | 377 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
|
378 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
|
379 } |
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9450
diff
changeset
|
380 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 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
|
382 state->epoll_fd = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
383 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 lua_pushstring(L, strerror(errno)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 lua_error(L); |
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 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 return 0; |
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 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
394 * String representation |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
396 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
|
397 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
|
398 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
|
399 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
400 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
401 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
402 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
403 * Create a new context |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
404 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
405 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
|
406 /* Allocate state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
407 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
|
408 luaL_setmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
409 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
410 /* Initialize state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 #ifdef USE_EPOLL |
9476
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
412 state->epoll_fd = -1; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
413 state->processed = 0; |
9476
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
414 |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
415 int epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
416 |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
417 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
|
418 luaL_pushfail(L); |
9476
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
419 lua_pushstring(L, strerror(errno)); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
420 lua_pushinteger(L, errno); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
421 return 3; |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
422 } |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
423 |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
424 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
|
425 #endif |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
426 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
427 FD_ZERO(&state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 FD_ZERO(&state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 FD_ZERO(&state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 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
|
431 FD_ZERO(&state->all); |
352e8b75c1ae
util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents:
9447
diff
changeset
|
432 FD_ZERO(&state->err); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 state->processed = FD_SETSIZE; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
434 #endif |
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 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 } |
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 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
440 * Open library |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 int luaopen_util_poll(lua_State *L) { |
9318 | 443 #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
|
444 luaL_checkversion(L); |
9318 | 445 #endif |
9313
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 luaL_newmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 { |
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_pushliteral(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 lua_setfield(L, -2, "__name"); |
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_pushcfunction(L, Ltos); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 lua_setfield(L, -2, "__tostring"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
456 lua_createtable(L, 0, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
457 { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
458 lua_pushcfunction(L, Ladd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
459 lua_setfield(L, -2, "add"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 lua_pushcfunction(L, Lset); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 lua_setfield(L, -2, "set"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 lua_pushcfunction(L, Ldel); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 lua_setfield(L, -2, "del"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 lua_pushcfunction(L, Lwait); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
465 lua_setfield(L, -2, "wait"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
466 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
467 lua_pushcfunction(L, Lgetfd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
468 lua_setfield(L, -2, "getfd"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
469 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
470 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 lua_setfield(L, -2, "__index"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
473 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
474 lua_pushcfunction(L, Lgc); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
475 lua_setfield(L, -2, "__gc"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 #endif |
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 |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
479 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
|
480 { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
481 lua_pushcfunction(L, Lnew); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 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
|
483 |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
484 #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
|
485 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
|
486 |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
487 push_errno(EEXIST); |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
488 push_errno(ENOENT); |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9478
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 |