From 526c253d57b2daed8a122d56a0532dc1e7e83b40 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Tue, 31 Jul 2012 18:29:52 +0200 Subject: utils: remove timer function --- README.markdown | 14 ---------- lem/utils.c | 87 --------------------------------------------------------- test/test.lua | 9 +++++- test/test2.lua | 16 +++++++---- 4 files changed, 18 insertions(+), 108 deletions(-) diff --git a/README.markdown b/README.markdown index a7488a0..e882ce0 100644 --- a/README.markdown +++ b/README.markdown @@ -143,20 +143,6 @@ This sets `utils` to a table with the following functions. Returns `true` on succes and `nil, 'not sleeping'` if no coroutine is currently sleeping on the object. -* __utils.timer(seconds, func)__ - - This method will schedule the function `func` to be run in a new coroutine - after `seconds` seconds and return a new timer object. - - If `seconds` is zero or negative this method shall behave as `utils.spawn()` - except it will still return a timer object. - -* __timer:cancel()__ - - This method cancels the timer. - Returns `true` on success and `nil, 'expired'` if the coroutine has already - been scheduled to run. - The Stream Library ------------------ diff --git a/lem/utils.c b/lem/utils.c index 17f8fe5..d93cf98 100644 --- a/lem/utils.c +++ b/lem/utils.c @@ -114,79 +114,6 @@ sleeper_new(lua_State *T) return 1; } -static int -timer_cancel(lua_State *T) -{ - struct ev_timer *w; - lua_State *S; - - luaL_checktype(T, 1, LUA_TUSERDATA); - w = lua_touserdata(T, 1); - S = w->data; - if (S == NULL) { - lua_pushnil(T); - lua_pushliteral(T, "expired"); - return 2; - } - - ev_timer_stop(LEM_ w); - lem_forgetthread(S); - - /* return true */ - lua_pushboolean(T, 1); - return 1; -} - -static void -timer_handler(EV_P_ struct ev_timer *w, int revents) -{ - lua_State *T = w->data; - - (void)revents; - - lua_settop(T, 1); - lem_queue(T, 0); - - /* mark this timer as expired */ - w->data = NULL; -} - -static int -timer_new(lua_State *T) -{ - ev_tstamp delay = (ev_tstamp)luaL_checknumber(T, 1); - struct ev_timer *w; - lua_State *S; - - luaL_checktype(T, 2, LUA_TFUNCTION); - - S = lem_newthread(); - lua_settop(T, 2); - lua_xmove(T, S, 1); - - /* create new timer object and set metatable */ - w = lua_newuserdata(T, sizeof(struct ev_timer)); - lua_pushvalue(T, lua_upvalueindex(1)); - lua_setmetatable(T, -2); - - if (delay > 0) { - w->data = S; - - /* push a reference of w to S */ - lua_pushvalue(T, -1); - lua_xmove(T, S, 1); - - ev_timer_init(w, timer_handler, delay, 0); - ev_timer_start(LEM_ w); - } else { - w->data = NULL; - - lem_queue(S, 0); - } - - return 1; -} - static int spawn(lua_State *T) { @@ -231,9 +158,7 @@ static int exit_lua(lua_State *T) { int status = (int)luaL_checknumber(T, 1); - lem_exit(status); - return 0; } @@ -288,18 +213,6 @@ luaopen_lem_utils(lua_State *L) lua_pushcclosure(L, sleeper_new, 1); lua_setfield(L, -2, "sleeper"); - /* create new timer metatable */ - lua_newtable(L); - /* mt.__index = mt */ - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - /* mt.cancel = */ - lua_pushcfunction(L, timer_cancel); - lua_setfield(L, -2, "cancel"); - /* set timer function */ - lua_pushcclosure(L, timer_new, 1); - lua_setfield(L, -2, "timer"); - /* set spawn function */ lua_pushcfunction(L, spawn); lua_setfield(L, -2, "spawn"); diff --git a/test/test.lua b/test/test.lua index 57d76f2..8402e05 100755 --- a/test/test.lua +++ b/test/test.lua @@ -23,11 +23,18 @@ local function sleep(n) utils.sleeper():sleep(n) end +local function timer(n, f, ...) + utils.spawn(function(...) + sleep(n) + return f(...) + end, ...) +end + --print('package.cpath = ', package.cpath) print 'Saying booh in 2.5 seconds' -utils.timer(2.5, function() print 'Booh!' end) +timer(2.5, function() print 'Booh!' end) print 'Sleeping 5 seconds' diff --git a/test/test2.lua b/test/test2.lua index b64eedf..4988a0b 100755 --- a/test/test2.lua +++ b/test/test2.lua @@ -26,15 +26,19 @@ local function sleep(n) end print 'Saying "Fee!" in 1 second' -utils.timer(1, function() print 'Fee!' end) +utils.spawn(function() + sleep(1) + print 'Fee!' +end) print 'Saying "Fo!" in 3 seconds' -utils.timer(3, function() print 'Fo!' end) - utils.spawn(function() - print 'Sleeping for 2 seconds, then saying "Fi!" before the script ends' - sleep(2) - print 'Fi!' + sleep(3) + print 'Fo!' end) +print 'Sleeping for 2 seconds, then saying "Fi!" before the script ends' +sleep(2) +print 'Fi!' + -- vim: syntax=lua ts=3 sw=3 et: -- cgit v1.2.1