diff options
| author | ruki <[email protected]> | 2019-12-13 00:44:56 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-12 22:29:39 +0800 |
| commit | 148619a18aea3acbc69523c99468a8315c59d9f2 (patch) | |
| tree | 40a9a3f59d9dfccaccae13940a6d8747e57de1ba /core/src | |
| parent | bfbdaf039adfd2c145f17b256772d52cb97c8d19 (diff) | |
fix poller_wait and insert
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/io/poller_insert.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_modify.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_wait.c | 25 |
3 files changed, 16 insertions, 13 deletions
diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c index 3b763007c..eeb6fd824 100644 --- a/core/src/xmake/io/poller_insert.c +++ b/core/src/xmake/io/poller_insert.c @@ -53,7 +53,7 @@ tb_int_t xm_io_poller_insert(lua_State* lua) tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2); // insert events to poller - lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), sock, events, lua)); + lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), sock, events, tb_null)); return 1; } diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c index 2bba2cabd..24a27e241 100644 --- a/core/src/xmake/io/poller_modify.c +++ b/core/src/xmake/io/poller_modify.c @@ -53,7 +53,7 @@ tb_int_t xm_io_poller_modify(lua_State* lua) tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2); // modify events in poller - lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), sock, events, lua)); + lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), sock, events, tb_null)); return 1; } diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c index eceb44cbf..03cd90efa 100644 --- a/core/src/xmake/io/poller_wait.c +++ b/core/src/xmake/io/poller_wait.c @@ -35,8 +35,9 @@ * globals */ -// we need only one global poller and main thread, so it is thread-safe. -static tb_int_t g_events_count = 0; +// 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; /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation @@ -44,16 +45,15 @@ static tb_int_t g_events_count = 0; static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_socket_ref_t sock, tb_size_t events, tb_cpointer_t priv) { // check - lua_State* lua = (lua_State*)priv; - tb_assert_and_check_return(lua); + tb_assert_and_check_return(g_lua && !g_events_count); // save socket and events - lua_newtable(lua); - lua_pushlightuserdata(lua, (tb_pointer_t)sock); - lua_rawseti(lua, -2, 1); - lua_pushinteger(lua, (tb_int_t)events); - lua_rawseti(lua, -2, 2); - lua_rawseti(lua, -2, ++g_events_count); + lua_newtable(g_lua); + lua_pushlightuserdata(g_lua, (tb_pointer_t)sock); + lua_rawseti(g_lua, -2, 1); + lua_pushinteger(g_lua, (tb_int_t)events); + lua_rawseti(g_lua, -2, 2); + lua_rawseti(g_lua, -2, ++g_events_count); } /* ////////////////////////////////////////////////////////////////////////////////////// @@ -69,9 +69,12 @@ tb_int_t xm_io_poller_wait(lua_State* lua) // get timeout 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 lua_newtable(lua); - g_events_count = 0; tb_long_t count = tb_poller_wait(xm_io_poller(), xm_io_poller_event, timeout); if (count > 0) { |
