From 6b5bbba8bd910dfaa911390b2f0b6145f7a7b114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 26 Feb 2016 20:09:38 +0100 Subject: [PATCH] Use libevent2 instead of epoll --- Makefile | 2 +- include/lupi.h | 11 ++---- src/c/epoll.c | 72 --------------------------------------- src/c/event.c | 69 +++++++++++++++++++++++++++++++++++++ src/c/lnative.c | 2 +- src/c/run.c | 2 +- src/lua/core/computer.lua | 2 +- 7 files changed, 75 insertions(+), 85 deletions(-) delete mode 100644 src/c/epoll.c create mode 100644 src/c/event.c diff --git a/Makefile b/Makefile index 3e482de..a4c5af8 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ SOURCE = src/c CORELUA = src/lua/core 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) diff --git a/include/lupi.h b/include/lupi.h index 381c87b..df6b57b 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -25,14 +25,7 @@ void luanative_start(lua_State *L); void setup_modules(lua_State *L); void termutils_start(lua_State *L); void internet_start(lua_State *L); -void epoll_prepare(); -int epoll_pull(int timeout); - -struct lupi_event_handler { - int (*handler)(int, void*); /* FD, data, return number of pushed events */ - /* TODO: doc? */ - int fd; - void* data; -}; +void event_prepare(); +int event_pull(int timeout); #endif diff --git a/src/c/epoll.c b/src/c/epoll.c deleted file mode 100644 index 33187f7..0000000 --- a/src/c/epoll.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "lupi.h" -#include -#include -#include -#include -#include -#include - -#include - -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; -} diff --git a/src/c/event.c b/src/c/event.c new file mode 100644 index 0000000..02f82c3 --- /dev/null +++ b/src/c/event.c @@ -0,0 +1,69 @@ +#include "lupi.h" +#include +#include +#include +#include +#include + +#include +#include + +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; +} diff --git a/src/c/lnative.c b/src/c/lnative.c index 63da238..ff077b5 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -359,7 +359,7 @@ static int l_freeMemory (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; } diff --git a/src/c/run.c b/src/c/run.c index bc45cef..da7e03d 100644 --- a/src/c/run.c +++ b/src/c/run.c @@ -29,7 +29,7 @@ void run_init() { luanative_start (L); internet_start (L); termutils_start (L); - epoll_prepare(); + event_prepare(); /* int status = luaL_loadstring(L, lua_init); */ int status = luaL_loadbuffer(L, lua_init, strlen(lua_init), "=INIT"); diff --git a/src/lua/core/computer.lua b/src/lua/core/computer.lua index 92f3025..7d4522d 100644 --- a/src/lua/core/computer.lua +++ b/src/lua/core/computer.lua @@ -135,7 +135,7 @@ function api.pullSignal(timeout) local nevts = 0 repeat nevts = native.pull(timeout) - until nevts > 0 or native.uptime() > timeoutuptime + until nevts > 0 or native.uptime() >= timeoutuptime if signalQueue[1] then native.log("pullSignal native: " .. signalQueue[1][1]) return table.unpack(table.remove(signalQueue, 1))