Use libevent2 instead of epoll

This commit is contained in:
Łukasz Magiera 2016-02-26 20:09:38 +01:00
parent 5221e5e113
commit 6b5bbba8bd
7 changed files with 75 additions and 85 deletions

View File

@ -13,7 +13,7 @@ SOURCE = src/c
CORELUA = src/lua/core CORELUA = src/lua/core
RESOURCES = resources RESOURCES = resources
LIBS=-lm -lssl -lcrypto LIBS=-lm -lssl -lcrypto -levent_core
INCLUDES=-I$(SOURCE) -Isrc/c/lib/lua -Iinclude -Idependencies/include -Idependencies/include-$(PREFIX) INCLUDES=-I$(SOURCE) -Isrc/c/lib/lua -Iinclude -Idependencies/include -Idependencies/include-$(PREFIX)

View File

@ -25,14 +25,7 @@ void luanative_start(lua_State *L);
void setup_modules(lua_State *L); void setup_modules(lua_State *L);
void termutils_start(lua_State *L); void termutils_start(lua_State *L);
void internet_start(lua_State *L); void internet_start(lua_State *L);
void epoll_prepare(); void event_prepare();
int epoll_pull(int timeout); int event_pull(int timeout);
struct lupi_event_handler {
int (*handler)(int, void*); /* FD, data, return number of pushed events */
/* TODO: doc? */
int fd;
void* data;
};
#endif #endif

View File

@ -1,72 +0,0 @@
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <unistd.h>
#include <event2/event.h>
static int epollfd;
static int handleStdin(int fd, void* data) {
char buf;
int r = read(fd, &buf, 1); /* TODO: Wide chars? */
if(r > 0) {
/* if(buf == 10) buf = 13; */
lua_State* L = getL();
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_down");
lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "user");
lua_call(L, 5, 0);
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_up");
lua_pushstring(L, "TODO:SetThisUuid");
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "user");
lua_call(L, 5, 0);
return 2;
}
return 0;
}
void epoll_prepare() {
if ((epollfd = epoll_create1(0)) < 0) {
perror("epoll_create");
exit(EXIT_FAILURE);
}
struct lupi_event_handler* stdin_handler = malloc(sizeof(struct lupi_event_handler));
stdin_handler->data = NULL;
stdin_handler->handler = handleStdin;
stdin_handler->fd = STDIN_FILENO;
struct epoll_event stdinEvent;
stdinEvent.events = EPOLLIN | EPOLLPRI;
stdinEvent.data.ptr = stdin_handler;
if ((epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &stdinEvent) < 0)) {
perror("epoll_ctl");
exit(EXIT_FAILURE);
}
}
int epoll_pull(int timeout) {
struct epoll_event evBuffer;
int pushed = 0;
int eres = epoll_wait(epollfd, &evBuffer, 1, timeout);
if(eres > 0) {
struct lupi_event_handler* handler = (struct lupi_event_handler*)evBuffer.data.ptr;
pushed = handler->handler(handler->fd, handler->data);
}
return pushed;
}

69
src/c/event.c Normal file
View File

@ -0,0 +1,69 @@
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <event2/event.h>
#include <event2/event_struct.h>
struct event_base *base;
struct event stdinEvent;
int nevt = 0;
static void handleStdin(evutil_socket_t fd, short what, void *ptr) {
char buf;
int r = read(fd, &buf, 1); /* TODO: Wide chars? */
if(r > 0) {
lua_State* L = getL();
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_down");
lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_up");
lua_pushstring(L, "TODO:SetThisUuid");
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
nevt += 2;
}
}
void event_prepare() {
base = event_base_new();
event_assign(&stdinEvent, base, STDIN_FILENO, EV_READ, handleStdin, NULL);
}
static void add_events(struct timeval* timeout) {
event_add(&stdinEvent, timeout);
}
int event_pull(int _timeout) {
if(_timeout > 0) { /* wait max this much time for event */
struct timeval timeout = {_timeout / 1000, (_timeout % 1000) * 1000000};
add_events(&timeout);
/* event_base_loopexit(base, &timeout); */
event_base_loop(base, EVLOOP_ONCE);
} else if(_timeout == 0) { /* Get event without blocking */
add_events(NULL);
event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
} else { /* wait for event to appear */
add_events(NULL);
event_base_loopexit(base, NULL);
event_base_loop(base, EVLOOP_ONCE);
}
int n = nevt;
nevt = 0;
return n;
}

View File

@ -359,7 +359,7 @@ static int l_freeMemory (lua_State *L) {
} }
static int l_pull (lua_State *L) { static int l_pull (lua_State *L) {
lua_pushnumber(L, epoll_pull(lua_tonumber(L, 1))); lua_pushnumber(L, event_pull(lua_tonumber(L, 1)));
return 1; return 1;
} }

View File

@ -29,7 +29,7 @@ void run_init() {
luanative_start (L); luanative_start (L);
internet_start (L); internet_start (L);
termutils_start (L); termutils_start (L);
epoll_prepare(); event_prepare();
/* int status = luaL_loadstring(L, lua_init); */ /* int status = luaL_loadstring(L, lua_init); */
int status = luaL_loadbuffer(L, lua_init, strlen(lua_init), "=INIT"); int status = luaL_loadbuffer(L, lua_init, strlen(lua_init), "=INIT");

View File

@ -135,7 +135,7 @@ function api.pullSignal(timeout)
local nevts = 0 local nevts = 0
repeat repeat
nevts = native.pull(timeout) nevts = native.pull(timeout)
until nevts > 0 or native.uptime() > timeoutuptime until nevts > 0 or native.uptime() >= timeoutuptime
if signalQueue[1] then if signalQueue[1] then
native.log("pullSignal native: " .. signalQueue[1][1]) native.log("pullSignal native: " .. signalQueue[1][1])
return table.unpack(table.remove(signalQueue, 1)) return table.unpack(table.remove(signalQueue, 1))