summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-16 23:10:13 +0800
committerruki <[email protected]>2025-08-28 11:35:53 +0800
commit5420b2209dd3657e75b6cfb586f0f206005c5a5a (patch)
tree2a655c2b765e4079811e9033b099162772816e4c /core/src
parent91c6ceedf1acc7ce856130913474ec55750fd8bb (diff)
bind poller to engine and lua
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/engine.c62
-rw-r--r--core/src/xmake/engine.h18
-rw-r--r--core/src/xmake/io/poller.c34
-rw-r--r--core/src/xmake/io/poller.h2
-rw-r--r--core/src/xmake/io/poller_insert.c2
-rw-r--r--core/src/xmake/io/poller_modify.c2
-rw-r--r--core/src/xmake/io/poller_remove.c2
-rw-r--r--core/src/xmake/io/poller_spank.c2
-rw-r--r--core/src/xmake/io/poller_support.c2
-rw-r--r--core/src/xmake/io/poller_wait.c2
-rw-r--r--core/src/xmake/thread/thread_init.c1
11 files changed, 92 insertions, 37 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 435e9fb5b..fb1031e49 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -64,6 +64,11 @@
# include "lz4/prefix.h"
#endif
+// for lua
+#ifndef USE_LUAJIT
+# include "../../lua/lua/lstate.h"
+#endif
+
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
@@ -95,6 +100,9 @@ typedef struct __xm_engine_t
// the engine name
tb_char_t name[64];
+ // the io poller
+ tb_poller_ref_t poller;
+
#ifdef XM_EMBED_ENABLE
// the temporary directory
tb_char_t tmpdir[TB_PATH_MAXN];
@@ -1207,6 +1215,7 @@ static tb_void_t xm_engine_init_signal(xm_engine_t* engine)
}
#if XM_HOOK_LUA_MEMALLOC
+// udata is unused, it has been used by engine. see xm_engine_bind_to_lua()
static tb_pointer_t xm_engine_lua_realloc(tb_pointer_t udata, tb_pointer_t data, size_t osize, size_t nsize)
{
tb_pointer_t ptr = tb_null;
@@ -1337,6 +1346,17 @@ static tb_bool_t xm_engine_extract_programfiles(xm_engine_t* engine, tb_char_t c
}
#endif
+static tb_void_t xm_engine_bind_to_lua(lua_State* lua, xm_engine_t* engine)
+{
+#ifdef USE_LUAJIT
+ lua_pushlightuserdata(lua, engine);
+ lua_setglobal(lua, "__global_engine");
+#else
+ global_State* g = G(lua);
+ g->ud = engine;
+#endif
+}
+
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
@@ -1358,8 +1378,8 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
tb_assert_and_check_break(engine->lua);
#if XM_HOOK_LUA_MEMALLOC
- // hook lua memmory
- lua_setallocf(engine->lua, xm_engine_lua_realloc, engine->lua);
+ // hook lua memmory, @note we cannot set udata argument, xm_engine_bind_to_lua() has used it.
+ lua_setallocf(engine->lua, xm_engine_lua_realloc, tb_null);
#endif
// open lua libraries
@@ -1437,6 +1457,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
lua_setglobal(engine->lua, "cjson");
#endif
+ // bind engine to lua
+ xm_engine_bind_to_lua(engine->lua, engine);
+
// init host
xm_engine_init_host(engine);
@@ -1531,6 +1554,10 @@ tb_void_t xm_engine_exit(xm_engine_ref_t self)
if (engine->lua) lua_close(engine->lua);
engine->lua = tb_null;
+ // exit poller
+ if (engine->poller) tb_poller_exit(engine->poller);
+ engine->poller = tb_null;
+
// exit it
tb_free(engine);
}
@@ -1621,6 +1648,24 @@ tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t self, tb_byte_t const* data,
engine->embedcount++;
}
#endif
+tb_poller_ref_t xm_engine_poller(xm_engine_ref_t self)
+{
+ // check
+ xm_engine_t* engine = (xm_engine_t*)self;
+ tb_assert_and_check_return_val(engine, tb_null);
+
+ if (!engine->poller)
+ {
+ // init poller
+ tb_poller_ref_t poller = tb_poller_init(tb_null);
+ tb_assert_and_check_return_val(poller, tb_null);
+
+ // attach poller to the current thread
+ tb_poller_attach(poller);
+ engine->poller = poller;
+ }
+ return engine->poller;
+}
tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv, xm_engine_lni_initalizer_cb_t lni_initalizer)
{
tb_int_t ok = -1;
@@ -1637,3 +1682,16 @@ tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, t
return ok;
}
+xm_engine_ref_t xm_engine_get(lua_State* lua)
+{
+ tb_assert_and_check_return_val(lua, tb_null);
+
+#ifdef USE_LUAJIT
+ lua_getglobal(lua, "__global_engine");
+ return (xm_engine_ref_t)lua_touserdata(lua, -1);
+#else
+ global_State* g = G(lua);
+ return (xm_engine_ref_t)g->ud;
+#endif
+}
+
diff --git a/core/src/xmake/engine.h b/core/src/xmake/engine.h
index 89344cef1..a67024360 100644
--- a/core/src/xmake/engine.h
+++ b/core/src/xmake/engine.h
@@ -81,12 +81,20 @@ tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t
/*! add the embed files
*
- * @param name the engine name
+ * @param engine the engine
* @param data the embedfiles data
* @param size the data size
*/
tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t engine, tb_byte_t const* data, tb_size_t size);
+/* get poller from engine
+ *
+ * @param engine the engine
+ *
+ * @return the poller
+ */
+tb_poller_ref_t xm_engine_poller(xm_engine_ref_t engine);
+
/*! run main entry of the engine singleton
*
* @param name the engine name
@@ -99,6 +107,14 @@ tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t engine, tb_
*/
tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv, xm_engine_lni_initalizer_cb_t lni_initalizer);
+/*! get engine from the given lua state
+ *
+ * @param lua the lua state
+ *
+ * @return the engine
+ */
+xm_engine_ref_t xm_engine_get(lua_State* lua);
+
/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
diff --git a/core/src/xmake/io/poller.c b/core/src/xmake/io/poller.c
index 77c35e639..7700c6858 100644
--- a/core/src/xmake/io/poller.c
+++ b/core/src/xmake/io/poller.c
@@ -29,37 +29,17 @@
* includes
*/
#include "poller.h"
-
-/* //////////////////////////////////////////////////////////////////////////////////////
- * macros
- */
-
-// the singleton type of poller
-#define XM_IO_POLLER (TB_SINGLETON_TYPE_USER + 4)
-
-/* //////////////////////////////////////////////////////////////////////////////////////
- * private implementation
- */
-static tb_handle_t xm_io_poller_instance_init(tb_cpointer_t* ppriv)
-{
- // init poller
- tb_poller_ref_t poller = tb_poller_init(tb_null);
- tb_assert_and_check_return_val(poller, tb_null);
-
- // attach poller to the current thread
- tb_poller_attach(poller);
- return (tb_handle_t)poller;
-}
-static tb_void_t xm_io_poller_instance_exit(tb_handle_t poller, tb_cpointer_t priv)
-{
- if (poller) tb_poller_exit((tb_poller_ref_t)poller);
-}
+#include "../engine.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
-tb_poller_ref_t xm_io_poller()
+tb_poller_ref_t xm_io_poller(lua_State* lua)
{
- return (tb_poller_ref_t)tb_singleton_instance(XM_IO_POLLER, xm_io_poller_instance_init, xm_io_poller_instance_exit, tb_null, tb_null);
+ tb_poller_ref_t poller = tb_null;
+ xm_engine_ref_t engine = xm_engine_get(lua);
+ if (engine) poller = xm_engine_poller(engine);
+ tb_assert(poller);
+ return poller;
}
diff --git a/core/src/xmake/io/poller.h b/core/src/xmake/io/poller.h
index 83252afd5..6713e3257 100644
--- a/core/src/xmake/io/poller.h
+++ b/core/src/xmake/io/poller.h
@@ -34,7 +34,7 @@
*
* @return the io poller
*/
-tb_poller_ref_t xm_io_poller(tb_noarg_t);
+tb_poller_ref_t xm_io_poller(lua_State* lua);
#endif
diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c
index b8890842a..e78c3c18f 100644
--- a/core/src/xmake/io/poller_insert.c
+++ b/core/src/xmake/io/poller_insert.c
@@ -64,7 +64,7 @@ tb_int_t xm_io_poller_insert(lua_State* lua)
tb_poller_object_t object;
object.type = otype;
object.ref.ptr = cdata;
- lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), &object, events, cdata_str));
+ lua_pushboolean(lua, tb_poller_insert(xm_io_poller(lua), &object, events, cdata_str));
return 1;
}
diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c
index e16f715d4..5ed858030 100644
--- a/core/src/xmake/io/poller_modify.c
+++ b/core/src/xmake/io/poller_modify.c
@@ -64,7 +64,7 @@ tb_int_t xm_io_poller_modify(lua_State* lua)
tb_poller_object_t object;
object.type = otype;
object.ref.ptr = cdata;
- lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), &object, events, cdata_str));
+ lua_pushboolean(lua, tb_poller_modify(xm_io_poller(lua), &object, events, cdata_str));
return 1;
}
diff --git a/core/src/xmake/io/poller_remove.c b/core/src/xmake/io/poller_remove.c
index a0c67b9bc..3cad1aa0a 100644
--- a/core/src/xmake/io/poller_remove.c
+++ b/core/src/xmake/io/poller_remove.c
@@ -60,7 +60,7 @@ tb_int_t xm_io_poller_remove(lua_State* lua)
tb_poller_object_t object;
object.type = otype;
object.ref.ptr = cdata;
- lua_pushboolean(lua, tb_poller_remove(xm_io_poller(), &object));
+ lua_pushboolean(lua, tb_poller_remove(xm_io_poller(lua), &object));
return 1;
}
diff --git a/core/src/xmake/io/poller_spank.c b/core/src/xmake/io/poller_spank.c
index e7d0bfcaf..ac7207763 100644
--- a/core/src/xmake/io/poller_spank.c
+++ b/core/src/xmake/io/poller_spank.c
@@ -42,7 +42,7 @@ tb_int_t xm_io_poller_spank(lua_State* lua)
tb_assert_and_check_return_val(lua, 0);
// spank the poller, break the tb_poller_wait() and return all events
- tb_poller_spak(xm_io_poller());
+ tb_poller_spak(xm_io_poller(lua));
return 0;
}
diff --git a/core/src/xmake/io/poller_support.c b/core/src/xmake/io/poller_support.c
index b26810a43..36ae52b34 100644
--- a/core/src/xmake/io/poller_support.c
+++ b/core/src/xmake/io/poller_support.c
@@ -45,7 +45,7 @@ tb_int_t xm_io_poller_support(lua_State* lua)
tb_size_t events = (tb_size_t)luaL_checknumber(lua, 1);
// support events for poller
- lua_pushboolean(lua, tb_poller_support(xm_io_poller(), events));
+ lua_pushboolean(lua, tb_poller_support(xm_io_poller(lua), events));
return 1;
}
diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c
index 01021d943..52155559f 100644
--- a/core/src/xmake/io/poller_wait.c
+++ b/core/src/xmake/io/poller_wait.c
@@ -93,7 +93,7 @@ tb_int_t xm_io_poller_wait(lua_State* lua)
// wait it
lua_newtable(lua);
- tb_long_t count = tb_poller_wait(xm_io_poller(), xm_io_poller_event, timeout);
+ tb_long_t count = tb_poller_wait(xm_io_poller(lua), xm_io_poller_event, timeout);
if (count > 0)
{
lua_pushinteger(lua, (tb_int_t)count);
diff --git a/core/src/xmake/thread/thread_init.c b/core/src/xmake/thread/thread_init.c
index a050066e8..5b8c733b5 100644
--- a/core/src/xmake/thread/thread_init.c
+++ b/core/src/xmake/thread/thread_init.c
@@ -43,6 +43,7 @@
static tb_void_t xm_thread_lni_initalizer(xm_engine_ref_t engine, lua_State* lua)
{
tb_trace_i("thread: initializer");
+ tb_trace_i("thread: engine: %p %p", engine, xm_engine_get(lua));
}
static tb_int_t xm_thread_func(tb_cpointer_t priv)