aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/poll.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-src/poll.c')
-rw-r--r--util-src/poll.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/util-src/poll.c b/util-src/poll.c
index 81caa953..fc70bdb6 100644
--- a/util-src/poll.c
+++ b/util-src/poll.c
@@ -8,7 +8,6 @@
*
*/
-#include <unistd.h>
#include <string.h>
#include <errno.h>
@@ -24,15 +23,18 @@
#endif
#ifdef USE_EPOLL
+#include <unistd.h>
#include <sys/epoll.h>
#ifndef MAX_EVENTS
-#define MAX_EVENTS 64
+/* Maximum number of returned events, retrieved into Lpoll_state */
+#define MAX_EVENTS 256
#endif
#endif
#ifdef USE_POLL
#include <poll.h>
-#ifndef MAX_EVENTS
-#define MAX_EVENTS 10000
+#ifndef MAX_WATCHED
+/* Maximum number of watched sockets, kept in Lpoll_state */
+#define MAX_WATCHED 10000
#endif
#endif
#ifdef USE_SELECT
@@ -44,9 +46,6 @@
#define STATE_MT "util.poll<" POLL_BACKEND ">"
-#if (LUA_VERSION_NUM == 501)
-#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
-#endif
#if (LUA_VERSION_NUM < 504)
#define luaL_pushfail lua_pushnil
#endif
@@ -62,7 +61,7 @@ typedef struct Lpoll_state {
#endif
#ifdef USE_POLL
nfds_t count;
- struct pollfd events[MAX_EVENTS];
+ struct pollfd events[MAX_WATCHED];
#endif
#ifdef USE_SELECT
fd_set wantread;
@@ -123,7 +122,7 @@ static int Ladd(lua_State *L) {
}
}
- if(state->count >= MAX_EVENTS) {
+ if(state->count >= MAX_WATCHED) {
luaL_pushfail(L);
lua_pushstring(L, strerror(EMFILE));
lua_pushinteger(L, EMFILE);
@@ -414,6 +413,12 @@ static int Lwait(lua_State *L) {
lua_Number timeout = luaL_checknumber(L, 2);
luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
+ if(timeout == 0.0) {
+ lua_pushnil(L);
+ lua_pushstring(L, "timeout");
+ return 2;
+ }
+
#ifdef USE_EPOLL
ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
#endif
@@ -540,7 +545,7 @@ static int Lnew(lua_State *L) {
state->processed = -1;
state->count = 0;
- for(nfds_t i = 0; i < MAX_EVENTS; i++) {
+ for(nfds_t i = 0; i < MAX_WATCHED; i++) {
state->events[i].fd = -1;
state->events[i].events = 0;
state->events[i].revents = 0;
@@ -563,10 +568,8 @@ static int Lnew(lua_State *L) {
/*
* Open library
*/
-int luaopen_util_poll(lua_State *L) {
-#if (LUA_VERSION_NUM > 501)
+int luaopen_prosody_util_poll(lua_State *L) {
luaL_checkversion(L);
-#endif
luaL_newmetatable(L, STATE_MT);
{
@@ -619,3 +622,8 @@ int luaopen_util_poll(lua_State *L) {
return 1;
}
+/* COMPAT */
+int luaopen_util_poll(lua_State *L) {
+ return luaopen_prosody_util_poll(L);
+}
+