author | Kim Alvefur <zash@zash.se> |
Wed, 23 Feb 2022 20:31:03 +0100 | |
changeset 12320 | 6bb2f660f689 |
parent 12319 | cf2086a1bd45 |
child 12579 | 1f6f05a98fcd |
permissions | -rw-r--r-- |
9316
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 |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
4 |
* Copyright (C) 2017-2022 Kim Alvefur |
9316
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 |
|
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
15 |
#if defined(__linux__) |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
#define USE_EPOLL |
12319
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12318
diff
changeset
|
17 |
#define POLL_BACKEND "epoll" |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
18 |
#elif defined(__unix__) |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
19 |
#define USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
20 |
#define POLL_BACKEND "poll" |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
21 |
#else |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
22 |
#define USE_SELECT |
12319
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12318
diff
changeset
|
23 |
#define POLL_BACKEND "select" |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
#include <sys/epoll.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
#ifndef MAX_EVENTS |
9453
b890ceb1c24f
util.poll: Increase max epoll events per call
Kim Alvefur <zash@zash.se>
parents:
9451
diff
changeset
|
29 |
#define MAX_EVENTS 64 |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
31 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
32 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
33 |
#include <poll.h> |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
34 |
#ifndef MAX_EVENTS |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
35 |
#define MAX_EVENTS 10000 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
36 |
#endif |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
37 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
38 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
#include <sys/select.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
#include <lualib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 |
#include <lauxlib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
|
12319
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12318
diff
changeset
|
45 |
#define STATE_MT "util.poll<" POLL_BACKEND ">" |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
|
9321 | 47 |
#if (LUA_VERSION_NUM == 501) |
48 |
#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2) |
|
49 |
#endif |
|
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
50 |
#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:
10100
diff
changeset
|
51 |
#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:
10100
diff
changeset
|
52 |
#endif |
9321 | 53 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 |
* 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
|
56 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
typedef struct Lpoll_state { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 |
int processed; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
int epoll_fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
struct epoll_event events[MAX_EVENTS]; |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
62 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
63 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
64 |
nfds_t count; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
65 |
struct pollfd events[MAX_EVENTS]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
66 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
67 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 |
fd_set wantread; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
fd_set wantwrite; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 |
fd_set readable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 |
fd_set writable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 |
fd_set all; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 |
fd_set err; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 |
} Lpoll_state; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
* 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
|
79 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
80 |
static int Ladd(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
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
|
82 |
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
|
83 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
|
9450
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
87 |
if(fd < 0) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
88 |
luaL_pushfail(L); |
9450
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
89 |
lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
90 |
lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
91 |
return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
92 |
} |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
93 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 |
event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 |
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
|
98 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 |
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
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 |
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
|
102 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 |
if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 |
ret = errno; |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
105 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
|
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
114 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
115 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
116 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
117 |
for(nfds_t i = 0; i < state->count; i++) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
118 |
if(state->events[i].fd == fd) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
119 |
luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
120 |
lua_pushstring(L, strerror(EEXIST)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
121 |
lua_pushinteger(L, EEXIST); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
122 |
return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
123 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
124 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
125 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
126 |
if(state->count >= MAX_EVENTS) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
127 |
luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
128 |
lua_pushstring(L, strerror(EMFILE)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
129 |
lua_pushinteger(L, EMFILE); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
130 |
return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
131 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
132 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
133 |
state->events[state->count].fd = fd; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
134 |
state->events[state->count].events = (wantread ? POLLIN : 0) | (wantwrite ? POLLOUT : 0); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
135 |
state->events[state->count].revents = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
136 |
state->count++; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
137 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
138 |
lua_pushboolean(L, 1); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
139 |
return 1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
140 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
141 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 |
|
9450
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
143 |
if(fd > FD_SETSIZE) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
144 |
luaL_pushfail(L); |
9450
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
145 |
lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
146 |
lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
147 |
return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
148 |
} |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9449
diff
changeset
|
149 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 |
if(FD_ISSET(fd, &state->all)) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
151 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 |
lua_pushstring(L, strerror(EEXIST)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 |
lua_pushinteger(L, EEXIST); |
9449
6b4d28eb19cf
util.poll: Fix missing return for adding duplicate FD
Kim Alvefur <zash@zash.se>
parents:
9443
diff
changeset
|
154 |
return 3; |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 |
FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 |
FD_CLR(fd, &state->err); |
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 |
FD_SET(fd, &state->all); |
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(wantread) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 |
FD_SET(fd, &state->wantread); |
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 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 |
FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 |
} |
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 |
if(wantwrite) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 |
FD_SET(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 |
FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 |
} |
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 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 |
#endif |
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 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 |
* 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
|
184 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
185 |
static int Lset(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 |
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
|
187 |
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
|
188 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 |
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
|
192 |
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
|
193 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 |
struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 |
event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 |
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
|
197 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 |
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
|
201 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 |
if(ret == 0) { |
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 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 |
ret = errno; |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
208 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 |
|
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
214 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
215 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
216 |
int wantread = lua_toboolean(L, 3); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
217 |
int wantwrite = lua_toboolean(L, 4); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
218 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
219 |
for(nfds_t i = 0; i < state->count; i++) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
220 |
struct pollfd *event = &state->events[i]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
221 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
222 |
if(event->fd == fd) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
223 |
event->events = (wantread ? POLLIN : 0) | (wantwrite ? POLLOUT : 0); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
224 |
lua_pushboolean(L, 1); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
225 |
return 1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
226 |
} else if(event->fd == -1) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
227 |
break; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
228 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
229 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
230 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
231 |
luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
232 |
lua_pushstring(L, strerror(ENOENT)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
233 |
lua_pushinteger(L, ENOENT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
234 |
return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
235 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
236 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 |
if(!FD_ISSET(fd, &state->all)) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
239 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 |
lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 |
lua_pushinteger(L, ENOENT); |
10100
46a7792fdac5
util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents:
9958
diff
changeset
|
242 |
return 3; |
9316
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 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 |
if(!lua_isnoneornil(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 |
if(lua_toboolean(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 |
FD_SET(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 |
FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 |
} |
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 |
if(!lua_isnoneornil(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 |
if(lua_toboolean(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 |
FD_SET(fd, &state->wantwrite); |
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 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 |
FD_CLR(fd, &state->wantwrite); |
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 |
} |
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 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 |
* Remove FDs |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
271 |
static int Ldel(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 |
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
|
273 |
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
|
274 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 |
struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 |
event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 |
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
|
281 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 |
if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 |
return 1; |
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 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 |
ret = errno; |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
288 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 |
return 3; |
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 |
|
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
294 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
295 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
296 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
297 |
if(state->count == 0) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
298 |
luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
299 |
lua_pushstring(L, strerror(ENOENT)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
300 |
lua_pushinteger(L, ENOENT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
301 |
return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
302 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
303 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
304 |
/* |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
305 |
* Move the last item on top of the removed one |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
306 |
*/ |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
307 |
struct pollfd *last = &state->events[state->count - 1]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
308 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
309 |
for(nfds_t i = 0; i < state->count; i++) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
310 |
struct pollfd *event = &state->events[i]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
311 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
312 |
if(event->fd == fd) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
313 |
event->fd = last->fd; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
314 |
event->events = last->events; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
315 |
event->revents = last->revents; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
316 |
last->fd = -1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
317 |
state->count--; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
318 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
319 |
lua_pushboolean(L, 1); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
320 |
return 1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
321 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
322 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
323 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
324 |
luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
325 |
lua_pushstring(L, strerror(ENOENT)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
326 |
lua_pushinteger(L, ENOENT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
327 |
return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
328 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
329 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 |
if(!FD_ISSET(fd, &state->all)) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
332 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 |
lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 |
lua_pushinteger(L, ENOENT); |
10100
46a7792fdac5
util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents:
9958
diff
changeset
|
335 |
return 3; |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 |
FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 |
FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 |
FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 |
FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 |
FD_CLR(fd, &state->all); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 |
FD_CLR(fd, &state->err); |
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 |
lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 |
#endif |
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 |
|
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 |
* 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
|
353 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
354 |
static int Lpushevent(lua_State *L, struct Lpoll_state *state) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 |
#ifdef USE_EPOLL |
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 |
if(state->processed > 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 |
state->processed--; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
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
|
363 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 |
|
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
366 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
367 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
368 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
369 |
for(int i = state->processed - 1; i >= 0; i--) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
370 |
struct pollfd *event = &state->events[i]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
371 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
372 |
if(event->fd != -1 && event->revents != 0) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
373 |
lua_pushinteger(L, event->fd); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
374 |
lua_pushboolean(L, event->revents & (POLLIN | POLLHUP | POLLERR)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
375 |
lua_pushboolean(L, event->revents & POLLOUT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
376 |
event->revents = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
377 |
state->processed = i; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
378 |
return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
379 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
380 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
381 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
382 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
383 |
#ifdef USE_SELECT |
9316
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 |
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
|
386 |
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
|
387 |
lua_pushinteger(L, fd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 |
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
|
389 |
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
|
390 |
FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
391 |
FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 |
FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 |
state->processed = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
394 |
return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
396 |
} |
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 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
399 |
return 0; |
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 |
* Wait for event |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
404 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
405 |
static int Lwait(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
406 |
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
|
407 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
408 |
int ret = Lpushevent(L, state); |
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 |
if(ret != 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 |
return ret; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
412 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
413 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 |
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
|
415 |
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
|
416 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
417 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
418 |
ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000); |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
419 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
420 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
421 |
ret = poll(state->events, state->count, timeout * 1000); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
422 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
423 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
424 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 |
* 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
|
426 |
* 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
|
427 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 |
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
|
429 |
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
|
430 |
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
|
431 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
432 |
struct timeval tv; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 |
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
|
434 |
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
|
435 |
|
9440
b202aa1e2d7b
util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents:
9321
diff
changeset
|
436 |
ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 |
#endif |
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 |
if(ret == 0) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
440 |
/* Is this an error? */ |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 |
lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 |
lua_pushstring(L, "timeout"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
443 |
return 2; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 |
} |
9511
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
445 |
else if(ret < 0 && errno == EINTR) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
446 |
/* Is this an error? */ |
9511
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
447 |
lua_pushnil(L); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
448 |
lua_pushstring(L, "signal"); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
449 |
return 2; |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9510
diff
changeset
|
450 |
} |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 |
else if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
452 |
ret = errno; |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
453 |
luaL_pushfail(L); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 |
lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 |
lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
456 |
return 3; |
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 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
459 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 |
* 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
|
461 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 |
state->processed = ret; |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
464 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
465 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
466 |
state->processed = state->count; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
467 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
468 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
469 |
state->processed = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
470 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 |
return Lpushevent(L, state); |
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 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
474 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
475 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 |
* Return Epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
477 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
478 |
static int Lgetfd(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 |
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
|
480 |
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
|
481 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
483 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
484 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
485 |
* Close epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
486 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
487 |
static int Lgc(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
488 |
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
|
489 |
|
9481 | 490 |
if(state->epoll_fd == -1) { |
9478
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9453
diff
changeset
|
491 |
return 0; |
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9453
diff
changeset
|
492 |
} |
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9453
diff
changeset
|
493 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
494 |
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
|
495 |
state->epoll_fd = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 |
else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
498 |
lua_pushstring(L, strerror(errno)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
499 |
lua_error(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
500 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
501 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
502 |
return 0; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
503 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
504 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
505 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
506 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
507 |
* String representation |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
508 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
509 |
static int Ltos(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
510 |
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
|
511 |
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
|
512 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
513 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
514 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
515 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
516 |
* Create a new context |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
517 |
*/ |
9958
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9511
diff
changeset
|
518 |
static int Lnew(lua_State *L) { |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
519 |
/* Allocate state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
520 |
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
|
521 |
luaL_setmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
522 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
523 |
/* Initialize state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
524 |
#ifdef USE_EPOLL |
9479
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
525 |
state->epoll_fd = -1; |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
526 |
state->processed = 0; |
9479
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
527 |
|
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
528 |
int epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
529 |
|
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
530 |
if(epoll_fd <= 0) { |
10925
6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents:
10100
diff
changeset
|
531 |
luaL_pushfail(L); |
9479
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
532 |
lua_pushstring(L, strerror(errno)); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
533 |
lua_pushinteger(L, errno); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
534 |
return 3; |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
535 |
} |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
536 |
|
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
537 |
state->epoll_fd = epoll_fd; |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
538 |
#endif |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
539 |
#ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
540 |
state->processed = -1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
541 |
state->count = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
542 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
543 |
for(nfds_t i = 0; i < MAX_EVENTS; i++) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
544 |
state->events[i].fd = -1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
545 |
state->events[i].events = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
546 |
state->events[i].revents = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
547 |
} |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
548 |
|
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
549 |
#endif |
12318
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10925
diff
changeset
|
550 |
#ifdef USE_SELECT |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
551 |
FD_ZERO(&state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
552 |
FD_ZERO(&state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
553 |
FD_ZERO(&state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
554 |
FD_ZERO(&state->writable); |
9451
352e8b75c1ae
util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents:
9450
diff
changeset
|
555 |
FD_ZERO(&state->all); |
352e8b75c1ae
util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents:
9450
diff
changeset
|
556 |
FD_ZERO(&state->err); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
557 |
state->processed = FD_SETSIZE; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
558 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
560 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
561 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
562 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 |
/* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 |
* Open library |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
565 |
*/ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
566 |
int luaopen_util_poll(lua_State *L) { |
9321 | 567 |
#if (LUA_VERSION_NUM > 501) |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
568 |
luaL_checkversion(L); |
9321 | 569 |
#endif |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
570 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
571 |
luaL_newmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
572 |
{ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
573 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
574 |
lua_pushliteral(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
575 |
lua_setfield(L, -2, "__name"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
576 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
577 |
lua_pushcfunction(L, Ltos); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
578 |
lua_setfield(L, -2, "__tostring"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
579 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
580 |
lua_createtable(L, 0, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
581 |
{ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
582 |
lua_pushcfunction(L, Ladd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
583 |
lua_setfield(L, -2, "add"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
584 |
lua_pushcfunction(L, Lset); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 |
lua_setfield(L, -2, "set"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
586 |
lua_pushcfunction(L, Ldel); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 |
lua_setfield(L, -2, "del"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
588 |
lua_pushcfunction(L, Lwait); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
589 |
lua_setfield(L, -2, "wait"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
590 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
591 |
lua_pushcfunction(L, Lgetfd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
592 |
lua_setfield(L, -2, "getfd"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
593 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
594 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
595 |
lua_setfield(L, -2, "__index"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
596 |
|
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
597 |
#ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
598 |
lua_pushcfunction(L, Lgc); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
599 |
lua_setfield(L, -2, "__gc"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
600 |
#endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
601 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
602 |
|
9510
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
603 |
lua_createtable(L, 0, 3); |
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
604 |
{ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
605 |
lua_pushcfunction(L, Lnew); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
606 |
lua_setfield(L, -2, "new"); |
9509
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
607 |
|
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
608 |
#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:
9481
diff
changeset
|
609 |
lua_setfield(L, -2, #named_error); |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
610 |
|
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
611 |
push_errno(EEXIST); |
12320
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12319
diff
changeset
|
612 |
push_errno(EMFILE); |
9510
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9509
diff
changeset
|
613 |
push_errno(ENOENT); |
9509
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9481
diff
changeset
|
614 |
|
12319
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12318
diff
changeset
|
615 |
lua_pushliteral(L, POLL_BACKEND); |
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12318
diff
changeset
|
616 |
lua_setfield(L, -2, "api"); |
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12318
diff
changeset
|
617 |
|
9316
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
618 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 |
return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
620 |
} |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
621 |