diff options
| author | ruki <[email protected]> | 2025-04-16 22:41:21 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:53 +0800 |
| commit | 28e35d67e2cf08069c971c2235845389143b5f0e (patch) | |
| tree | 79b494583b542484b2dc4753126e8954a93bd5e1 | |
| parent | 8bf1576c641f3b68bb43c50929cdcec8ee73ff86 (diff) | |
bind native thread apis
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/socket_close.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/thread/thread_exit.c | 13 | ||||
| -rw-r--r-- | core/src/xmake/thread/thread_init.c | 21 | ||||
| -rw-r--r-- | core/src/xmake/thread/thread_resume.c | 12 | ||||
| -rw-r--r-- | core/src/xmake/thread/thread_suspend.c | 12 | ||||
| -rw-r--r-- | core/src/xmake/thread/thread_wait.c | 57 | ||||
| -rw-r--r-- | tests/modules/thread/sleep.lua | 4 |
8 files changed, 115 insertions, 8 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index fa3462c7d..435e9fb5b 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -332,6 +332,7 @@ tb_int_t xm_lua_curses_register(lua_State* lua, tb_char_t const* module); // the thread functions tb_int_t xm_thread_init(lua_State* lua); tb_int_t xm_thread_exit(lua_State* lua); +tb_int_t xm_thread_wait(lua_State* lua); tb_int_t xm_thread_suspend(lua_State* lua); tb_int_t xm_thread_resume(lua_State* lua); @@ -626,6 +627,7 @@ static luaL_Reg const g_thread_functions[] = { { "thread_init", xm_thread_init } , { "thread_exit", xm_thread_exit } +, { "thread_wait", xm_thread_wait } , { "thread_resume", xm_thread_resume } , { "thread_suspend", xm_thread_suspend } , { tb_null, tb_null } diff --git a/core/src/xmake/io/socket_close.c b/core/src/xmake/io/socket_close.c index fca95c186..a424a707f 100644 --- a/core/src/xmake/io/socket_close.c +++ b/core/src/xmake/io/socket_close.c @@ -50,8 +50,6 @@ tb_int_t xm_io_socket_close(lua_State* lua) // exit socket lua_pushboolean(lua, tb_socket_exit(sock)); - - // ok return 1; } diff --git a/core/src/xmake/thread/thread_exit.c b/core/src/xmake/thread/thread_exit.c index c43f2d10e..021f2a55c 100644 --- a/core/src/xmake/thread/thread_exit.c +++ b/core/src/xmake/thread/thread_exit.c @@ -38,6 +38,17 @@ tb_int_t xm_thread_exit(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - return 0; + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get thread + tb_thread_ref_t thread = (tb_thread_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(thread, 0); + + // exit thread + tb_thread_exit(thread); + lua_pushboolean(lua, tb_true); + return 1; } diff --git a/core/src/xmake/thread/thread_init.c b/core/src/xmake/thread/thread_init.c index 758380b8a..03c62e909 100644 --- a/core/src/xmake/thread/thread_init.c +++ b/core/src/xmake/thread/thread_init.c @@ -31,6 +31,15 @@ #include "prefix.h" /* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_int_t xm_thread_func(tb_cpointer_t priv) +{ + tb_trace_i("thread .."); + return 0; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_int_t xm_thread_init(lua_State* lua) @@ -38,5 +47,15 @@ tb_int_t xm_thread_init(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - return 0; + // get thread name + tb_char_t const* name = luaL_checkstring(lua, 1); + + // get stack size + tb_size_t stacksize = (tb_size_t)luaL_checkinteger(lua, 4); + + // init thread + tb_thread_ref_t thread = tb_thread_init(name, xm_thread_func, tb_null, stacksize); + if (thread) xm_lua_pushpointer(lua, (tb_pointer_t)thread); + else lua_pushnil(lua); + return 1; } diff --git a/core/src/xmake/thread/thread_resume.c b/core/src/xmake/thread/thread_resume.c index 3ed446cf7..d611c81e4 100644 --- a/core/src/xmake/thread/thread_resume.c +++ b/core/src/xmake/thread/thread_resume.c @@ -38,6 +38,16 @@ tb_int_t xm_thread_resume(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - return 0; + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get thread + tb_thread_ref_t thread = (tb_thread_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(thread, 0); + + // resume thread + lua_pushboolean(lua, tb_thread_resume(thread)); + return 1; } diff --git a/core/src/xmake/thread/thread_suspend.c b/core/src/xmake/thread/thread_suspend.c index 0bfcae3f4..825155b5e 100644 --- a/core/src/xmake/thread/thread_suspend.c +++ b/core/src/xmake/thread/thread_suspend.c @@ -38,6 +38,16 @@ tb_int_t xm_thread_suspend(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - return 0; + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get thread + tb_thread_ref_t thread = (tb_thread_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(thread, 0); + + // suspend thread + lua_pushboolean(lua, tb_thread_suspend(thread)); + return 1; } diff --git a/core/src/xmake/thread/thread_wait.c b/core/src/xmake/thread/thread_wait.c new file mode 100644 index 000000000..5145a59db --- /dev/null +++ b/core/src/xmake/thread/thread_wait.c @@ -0,0 +1,57 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file thread_wait.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_wait(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get thread + tb_thread_ref_t thread = (tb_thread_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(thread, 0); + + // get timeout + tb_size_t timeout = (tb_size_t)luaL_checkinteger(lua, 2); + + // wait thread + tb_int_t retval; + lua_pushinteger(lua, (tb_int_t)tb_thread_wait(thread, timeout, &retval)); + return 1; +} + diff --git a/tests/modules/thread/sleep.lua b/tests/modules/thread/sleep.lua index 4dc350cd3..8d4c410f6 100644 --- a/tests/modules/thread/sleep.lua +++ b/tests/modules/thread/sleep.lua @@ -14,7 +14,7 @@ end function main() local t0 = thread.start_named("thread_0", callback, 0) local t1 = thread.start_named("thread_1", callback, 1) - thread.wait(t0, -1) - thread.wait(t1, -1) + t0:wait(-1) + t1:wait(-1) end |
