util-src/poll.c
author Kim Alvefur <zash@zash.se>
Sat, 15 Sep 2018 01:05:59 +0200
changeset 9320 d8496858c809
parent 9316 b95ef295c66d
child 9321 3429006518bf
permissions -rw-r--r--
util.poll: Remove 'inline' from function declaration Why does this fix building?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
 * Copyright (C) 2017-2018 Kim Alvefur
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
 *
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
 * This project is MIT licensed. Please see the
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
 * COPYING file in the source package for more information.
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
 *
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
#include <unistd.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
#include <string.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
#include <errno.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
#ifdef __linux__
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
#define USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
#include <sys/epoll.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
#ifndef MAX_EVENTS
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
#define MAX_EVENTS 10
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
#include <sys/select.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
#include <lualib.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
#include <lauxlib.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
#define STATE_MT "util.poll.epoll"
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
#define STATE_MT "util.poll.select"
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
 * 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
    39
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
typedef struct Lpoll_state {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
	int processed;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
	int epoll_fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
	struct epoll_event events[MAX_EVENTS];
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
	fd_set wantread;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
	fd_set wantwrite;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
	fd_set readable;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
	fd_set writable;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
	fd_set all;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
	fd_set err;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
} Lpoll_state;
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
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
 * 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
    57
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
int Ladd(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    59
	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
    60
	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
    61
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
	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
    63
	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
    64
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
	struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
	event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    68
	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
    69
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
	event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    71
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    72
	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
    73
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    74
	if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    75
		ret = errno;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    76
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    77
		lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
		lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
		return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    80
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
	lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    84
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    85
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    87
	if(FD_ISSET(fd, &state->all)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    88
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
		lua_pushstring(L, strerror(EEXIST));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
		lua_pushinteger(L, EEXIST);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    93
	FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    94
	FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
	FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
	FD_SET(fd, &state->all);
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
	if(wantread) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   100
		FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   101
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   102
	else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   103
		FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   105
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   106
	if(wantwrite) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
		FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   108
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   109
	else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   110
		FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   111
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   112
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   113
	lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   114
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   115
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   116
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   117
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   118
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   119
 * 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
   120
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   121
int Lset(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   122
	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
   123
	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
   124
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   125
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
	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
   128
	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
   129
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   130
	struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
	event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   132
	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
   133
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   134
	event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   135
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   136
	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
   137
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   138
	if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   139
		lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   140
		return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   141
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   142
	else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   143
		ret = errno;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   144
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   145
		lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   146
		lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   147
		return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   148
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   149
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   151
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
	if(!FD_ISSET(fd, &state->all)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   153
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   154
		lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
		lua_pushinteger(L, ENOENT);
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
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
	if(!lua_isnoneornil(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
		if(lua_toboolean(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   160
			FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   161
		}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   162
		else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   163
			FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   164
		}
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
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
	if(!lua_isnoneornil(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   168
		if(lua_toboolean(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   169
			FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   170
		}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   171
		else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   172
			FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   173
		}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   174
	}
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
	lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   177
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   178
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   179
}
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
 * Remove FDs
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   183
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   184
int Ldel(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   185
	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
   186
	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
   187
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   188
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   189
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   190
	struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   191
	event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   192
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   193
	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
   194
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
	if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   196
		lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   197
		return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   198
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   199
	else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   200
		ret = errno;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   201
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   202
		lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   203
		lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   204
		return 3;
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
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   207
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   208
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   209
	if(!FD_ISSET(fd, &state->all)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   210
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   211
		lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   212
		lua_pushinteger(L, ENOENT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   213
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   214
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   215
	FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   216
	FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   217
	FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   218
	FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   219
	FD_CLR(fd, &state->all);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   220
	FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   221
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   222
	lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   223
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   224
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   225
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   226
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   227
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   228
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   229
 * 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
   230
 */
9320
d8496858c809 util.poll: Remove 'inline' from function declaration
Kim Alvefur <zash@zash.se>
parents: 9316
diff changeset
   231
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
   232
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   233
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   234
	if(state->processed > 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   235
		state->processed--;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   236
		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
   237
		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
   238
		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
   239
		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
   240
		return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   241
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   242
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   243
#else
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
	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
   246
		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
   247
			lua_pushinteger(L, fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   248
			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
   249
			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
   250
			FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   251
			FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   252
			FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   253
			state->processed = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   254
			return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   255
		}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   256
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   257
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   258
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   259
	return 0;
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
 * Wait for event
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   264
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   265
int Lwait(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   266
	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
   267
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   268
	int ret = Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   269
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   270
	if(ret != 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   271
		return ret;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   272
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   273
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   274
	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
   275
	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
   276
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   277
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   278
	ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   279
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   280
	/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   281
	 * 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
   282
	 * 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
   283
	 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   284
	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
   285
	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
   286
	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
   287
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   288
	struct timeval tv;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   289
	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
   290
	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
   291
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   292
	ret = select(FD_SETSIZE, &state->readable, &state->writable, NULL, &tv);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   293
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   294
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   295
	if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   296
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   297
		lua_pushstring(L, "timeout");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   298
		return 2;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   299
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   300
	else if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   301
		ret = errno;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   302
		lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   303
		lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   304
		lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   305
		return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   306
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   307
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   308
	/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   309
	 * 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
   310
	 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   311
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   312
	state->processed = ret;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   313
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   314
	state->processed = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   315
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   316
	return Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   317
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   318
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   319
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   320
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   321
 * Return Epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   322
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   323
int Lgetfd(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   324
	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
   325
	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
   326
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   327
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   328
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   329
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   330
 * Close epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   331
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   332
int Lgc(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   333
	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
   334
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   335
	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
   336
		state->epoll_fd = -1;
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
	else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   339
		lua_pushstring(L, strerror(errno));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   340
		lua_error(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   341
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   342
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   343
	return 0;
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
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   346
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   347
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   348
 * String representation
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
int Ltos(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   351
	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
   352
	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
   353
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   354
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   355
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
 * Create a new context
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   358
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   359
int Lnew(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   360
	/* Allocate state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   361
	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
   362
	luaL_setmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   363
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   364
	/* Initialize state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   365
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   366
	state->epoll_fd = epoll_create1(0);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   367
	state->processed = 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   368
#else
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   369
	FD_ZERO(&state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   370
	FD_ZERO(&state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   371
	FD_ZERO(&state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   372
	FD_ZERO(&state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   373
	state->processed = FD_SETSIZE;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   374
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   375
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   376
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   377
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   378
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   379
/*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   380
 * Open library
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   381
 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   382
int luaopen_util_poll(lua_State *L) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   383
	luaL_checkversion(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   384
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   385
	luaL_newmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   386
	{
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   387
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   388
		lua_pushliteral(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   389
		lua_setfield(L, -2, "__name");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   390
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   391
		lua_pushcfunction(L, Ltos);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   392
		lua_setfield(L, -2, "__tostring");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   393
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   394
		lua_createtable(L, 0, 2);
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
			lua_pushcfunction(L, Ladd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   397
			lua_setfield(L, -2, "add");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   398
			lua_pushcfunction(L, Lset);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   399
			lua_setfield(L, -2, "set");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   400
			lua_pushcfunction(L, Ldel);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   401
			lua_setfield(L, -2, "del");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   402
			lua_pushcfunction(L, Lwait);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   403
			lua_setfield(L, -2, "wait");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   404
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   405
			lua_pushcfunction(L, Lgetfd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   406
			lua_setfield(L, -2, "getfd");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   407
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   408
		}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   409
		lua_setfield(L, -2, "__index");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   410
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   411
#ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   412
		lua_pushcfunction(L, Lgc);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   413
		lua_setfield(L, -2, "__gc");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   414
#endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   415
	}
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
	lua_createtable(L, 0, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   418
	{
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   419
		lua_pushcfunction(L, Lnew);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   420
		lua_setfield(L, -2, "new");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   421
	}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   422
	return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   423
}
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   424