summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-16 23:11:25 +0800
committerruki <[email protected]>2025-08-28 11:35:53 +0800
commitb34749df3d7af5314ae2fad9c208708ae2d6cab4 (patch)
treea72e4202bad5b2492167f5ddce5bd710107fa410 /core/src
parent5420b2209dd3657e75b6cfb586f0f206005c5a5a (diff)
bind lua to poller
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/io/poller_wait.c37
2 files changed, 19 insertions, 20 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index fb1031e49..51a30f0d2 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -1657,7 +1657,7 @@ tb_poller_ref_t xm_engine_poller(xm_engine_ref_t self)
if (!engine->poller)
{
// init poller
- tb_poller_ref_t poller = tb_poller_init(tb_null);
+ tb_poller_ref_t poller = tb_poller_init(engine->lua);
tb_assert_and_check_return_val(poller, tb_null);
// attach poller to the current thread
diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c
index 52155559f..69c223f9e 100644
--- a/core/src/xmake/io/poller_wait.c
+++ b/core/src/xmake/io/poller_wait.c
@@ -36,7 +36,6 @@
*/
// we need only one global lua state/poller in main thread, so it is thread-safe.
-static lua_State* g_lua = tb_null;
static tb_int_t g_events_count = 0;
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -45,33 +44,34 @@ static tb_int_t g_events_count = 0;
static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref_t object, tb_long_t events, tb_cpointer_t priv)
{
// check
- tb_assert_and_check_return(g_lua);
+ lua_State* lua = (lua_State*)tb_poller_priv(poller);
+ tb_assert_and_check_return(lua);
// save object and events
- lua_newtable(g_lua);
- lua_pushinteger(g_lua, (tb_int_t)object->type);
- lua_rawseti(g_lua, -2, 1);
- if (priv) lua_pushstring(g_lua, (tb_char_t const*)priv);
- else lua_pushlightuserdata(g_lua, object->ref.ptr);
- lua_rawseti(g_lua, -2, 2);
+ lua_newtable(lua);
+ lua_pushinteger(lua, (tb_int_t)object->type);
+ lua_rawseti(lua, -2, 1);
+ if (priv) lua_pushstring(lua, (tb_char_t const*)priv);
+ else lua_pushlightuserdata(lua, object->ref.ptr);
+ lua_rawseti(lua, -2, 2);
if (object->type == TB_POLLER_OBJECT_FWATCHER)
{
- lua_newtable(g_lua);
+ lua_newtable(lua);
tb_fwatcher_event_t* event = (tb_fwatcher_event_t*)events;
if (event)
{
- lua_pushstring(g_lua, "path");
- lua_pushstring(g_lua, event->filepath);
- lua_settable(g_lua, -3);
+ lua_pushstring(lua, "path");
+ lua_pushstring(lua, event->filepath);
+ lua_settable(lua, -3);
- lua_pushstring(g_lua, "type");
- lua_pushinteger(g_lua, event->event);
- lua_settable(g_lua, -3);
+ lua_pushstring(lua, "type");
+ lua_pushinteger(lua, event->event);
+ lua_settable(lua, -3);
}
}
- else lua_pushinteger(g_lua, (tb_int_t)events);
- lua_rawseti(g_lua, -2, 3);
- lua_rawseti(g_lua, -2, ++g_events_count);
+ else lua_pushinteger(lua, (tb_int_t)events);
+ lua_rawseti(lua, -2, 3);
+ lua_rawseti(lua, -2, ++g_events_count);
}
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -88,7 +88,6 @@ tb_int_t xm_io_poller_wait(lua_State* lua)
tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 1);
// pass lua and count to the events callback
- g_lua = lua;
g_events_count = 0;
// wait it