From 2e7ed3132883509ef86b70bf52fb7affa6322ab3 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 16 Jan 2012 04:25:48 +0000 Subject: event_callback.c: Don't delete and re-add the event if the timeout hasn't changed --- src/event_callback.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/event_callback.c b/src/event_callback.c index 1bf18ec..1ffe684 100644 --- a/src/event_callback.c +++ b/src/event_callback.c @@ -22,7 +22,7 @@ void luaevent_callback(int fd, short event, void* p) { le_callback* cb = p; lua_State* L; int ret; - double newTimeout = -1; + struct timeval new_tv = { 0, 0 }; assert(cb); if(!cb->base) return; /* Event has already been collected + destroyed */ @@ -34,13 +34,11 @@ void luaevent_callback(int fd, short event, void* p) { if(!cb->base) return; /* event was destroyed during callback */ ret = lua_tointeger(L, -2); - if(lua_isnumber(L, -1)) { - newTimeout = lua_tonumber(L, -1); - if(newTimeout <= 0) { - memset(&cb->timeout, 0, sizeof(cb->timeout)); - } else { - load_timeval(newTimeout, &cb->timeout); - } + if(lua_isnumber(L, -1)) + { + double newTimeout = lua_tonumber(L, -1); + if(newTimeout>0) + load_timeval(newTimeout, &new_tv); } lua_pop(L, 2); if(ret == -1) { @@ -48,9 +46,10 @@ void luaevent_callback(int fd, short event, void* p) { } else { struct event *ev = &cb->ev; int newEvent = ret; - /* NOTE: Currently, even if new timeout is the same as the old, a new event is setup regardless... */ - if(newEvent != event || newTimeout != -1) { // Need to hook up new event... + if( newEvent != event || (cb->timeout.tv_sec != new_tv.tv_sec || cb->timeout.tv_usec != new_tv.tv_usec) ) + { struct timeval *ptv = &cb->timeout; + cb->timeout = new_tv; if(!cb->timeout.tv_sec && !cb->timeout.tv_usec) ptv = NULL; event_del(ev); -- cgit v1.2.3