void xbee_ll_destroy(struct xbee_ll_head *list, void (*freeCallback)(void *)) { void *p; while ((xbee_ll_ext_tail(list, &p)) == XBEE_ENONE && p) { if (freeCallback) freeCallback(p); } xsys_mutex_destroy(&list->mutex); }
static int luaevent_cb_close(lua_State* L) { le_callback *cb = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT); if(!cb->running) event_del(&cb->ev); freeCallback(cb, L); // Release reference to Lua callback return 0; }
/* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */ void luaevent_callback(int fd, short event, void* p) { le_callback* cb = p; struct event *ev = &cb->ev; lua_State* L; int ret = -1; struct timeval new_tv = { 0, 0 }; if(cb->callbackRef != LUA_NOREF) { L = cb->base->loop_L; lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef); lua_pushinteger(L, event); cb->running = 1; if(lua_pcall(L, 1, 2, 0)) { cb->running = 0; cb->base->errorMessage = luaL_ref(L, LUA_REGISTRYINDEX); event_base_loopbreak(cb->base->base); lua_pop(L, 1); /* Pop the 'false' from pcall */ return; } cb->running = 0; /* If nothing is returned, re-use the old event value */ ret = luaL_optinteger(L, -2, event); } if(ret == -1 || cb->callbackRef == LUA_NOREF) { event_del(ev); freeCallback(cb, L); assert(cb->selfRef != LUA_NOREF); luaL_unref(L, LUA_REGISTRYINDEX, cb->selfRef); cb->selfRef = LUA_NOREF; } else if( ret != event || (cb->timeout.tv_sec != new_tv.tv_sec || cb->timeout.tv_usec != new_tv.tv_usec) ) { /* Clone the old timeout value in case a new one wasn't set */ memcpy(&new_tv, &cb->timeout, sizeof(new_tv)); if(lua_isnumber(L, -1)) { double newTimeout = lua_tonumber(L, -1); if(newTimeout > 0) { load_timeval(newTimeout, &new_tv); } } struct timeval *ptv = &cb->timeout; cb->timeout = new_tv; if(!cb->timeout.tv_sec && !cb->timeout.tv_usec) ptv = NULL; event_del(ev); event_set(ev, fd, EV_PERSIST | ret, luaevent_callback, cb); /* Assume cannot set a new timeout.. */ event_add(ev, ptv); } lua_pop(L, 2); /* Pop two results from call */ }
static int luaevent_cb_gc(lua_State* L) { freeCallback(luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT), L); return 0; }