/* * Arguments: ..., function, [arguments (any) ...] * Returns: [results (any) ...] */ static int levq_sync_call (lua_State *L, struct event_queue *evq, struct evq_sync_op *op, const int is_sched_add, const int fn_idx) { op->L = L; op->fn_idx = fn_idx; op->status = 0; op->is_sched_add = is_sched_add; op->next = evq->sync_op; evq->sync_op = op; evq_signal(evq, EVQ_SIGEVQ); if (is_sched_add) { lua_pushlightuserdata(L, op); /* sync_op_ludata */ return 1; } else { struct sys_thread *td = sys_thread_get(); const int old_top = fn_idx - 1; if (!td) luaL_argerror(L, 0, "Threading not initialized"); op->td = td; sys_thread_suspend(td, TIMEOUT_INFINITE); /* wait result */ if (op->status) lua_error(L); return lua_gettop(L) - old_top; } }
/* * Arguments: [timeout (milliseconds)] * Returns: [boolean] */ static int thread_suspend_wrap (lua_State *L) { struct sys_thread *td = sys_thread_get(); const msec_t timeout = lua_isnoneornil(L, 1) ? TIMEOUT_INFINITE : (msec_t) lua_tointeger(L, 1); const int res = sys_thread_suspend(td, timeout); if (res >= 0) { if (res == 1) { lua_pushboolean(L, 0); return 1; /* timed out */ } lua_pushboolean(L, 1); return 1; } return sys_seterror(L, 0); }