diff options
| author | ruki <[email protected]> | 2025-04-16 23:20:46 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:53 +0800 |
| commit | fb894bfe8b8b8ff2279a68f8b4d9a24572a90e74 (patch) | |
| tree | 70f4e4ad1fb0738801a6f5de2652bb68a240ba47 /core/src | |
| parent | b34749df3d7af5314ae2fad9c208708ae2d6cab4 (diff) | |
remove global events
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/engine.c | 5 | ||||
| -rw-r--r-- | core/src/xmake/io/poller.h | 12 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_wait.c | 24 |
3 files changed, 26 insertions, 15 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 51a30f0d2..5e5a13693 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -29,6 +29,7 @@ * includes */ #include "xmake.h" +#include "io/poller.h" #if defined(TB_CONFIG_OS_WINDOWS) # include <windows.h> # include <io.h> @@ -102,6 +103,7 @@ typedef struct __xm_engine_t // the io poller tb_poller_ref_t poller; + xm_poller_state_t poller_state; #ifdef XM_EMBED_ENABLE // the temporary directory @@ -1657,7 +1659,8 @@ 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(engine->lua); + engine->poller_state.lua = engine->lua; + tb_poller_ref_t poller = tb_poller_init(&engine->poller_state); tb_assert_and_check_return_val(poller, tb_null); // attach poller to the current thread diff --git a/core/src/xmake/io/poller.h b/core/src/xmake/io/poller.h index 6713e3257..887fc25ba 100644 --- a/core/src/xmake/io/poller.h +++ b/core/src/xmake/io/poller.h @@ -27,6 +27,18 @@ #include "prefix.h" /* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the poller state in wait events +typedef struct __xm_poller_state_t +{ + lua_State* lua; + tb_int_t events_count; + +}xm_poller_state_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// * interfaces */ diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c index 69c223f9e..341893f74 100644 --- a/core/src/xmake/io/poller_wait.c +++ b/core/src/xmake/io/poller_wait.c @@ -32,22 +32,16 @@ #include "poller.h" /* ////////////////////////////////////////////////////////////////////////////////////// - * globals - */ - -// we need only one global lua state/poller in main thread, so it is thread-safe. -static tb_int_t g_events_count = 0; - -/* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ 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 - lua_State* lua = (lua_State*)tb_poller_priv(poller); - tb_assert_and_check_return(lua); + xm_poller_state_t* state = (xm_poller_state_t*)tb_poller_priv(poller); + tb_assert_and_check_return(state && state->lua); // save object and events + lua_State* lua = state->lua; lua_newtable(lua); lua_pushinteger(lua, (tb_int_t)object->type); lua_rawseti(lua, -2, 1); @@ -71,7 +65,7 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref } else lua_pushinteger(lua, (tb_int_t)events); lua_rawseti(lua, -2, 3); - lua_rawseti(lua, -2, ++g_events_count); + lua_rawseti(lua, -2, ++state->events_count); } /* ////////////////////////////////////////////////////////////////////////////////////// @@ -82,17 +76,19 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref tb_int_t xm_io_poller_wait(lua_State* lua) { // check - tb_assert_and_check_return_val(lua, 0); + tb_poller_ref_t poller = xm_io_poller(lua); + tb_assert_and_check_return_val(poller && lua, 0); // get timeout tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 1); - // pass lua and count to the events callback - g_events_count = 0; + // reset events count + xm_poller_state_t* state = (xm_poller_state_t*)tb_poller_priv(poller); + state->events_count = 0; // wait it lua_newtable(lua); - tb_long_t count = tb_poller_wait(xm_io_poller(lua), xm_io_poller_event, timeout); + tb_long_t count = tb_poller_wait(poller, xm_io_poller_event, timeout); if (count > 0) { lua_pushinteger(lua, (tb_int_t)count); |
