diff options
| author | ruki <[email protected]> | 2025-08-26 23:22:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:54 +0800 |
| commit | 7bf08a61f291294d0dc021be62dec253146115f1 (patch) | |
| tree | 993099f4ec69707537661dc87174d17f4ae5359d | |
| parent | b1a196f9795aa03dbffb58895a37304149895e6a (diff) | |
add mutex apis
| -rw-r--r-- | core/src/xmake/engine.c | 22 | ||||
| -rw-r--r-- | core/src/xmake/thread/mutex_exit.c | 58 | ||||
| -rw-r--r-- | core/src/xmake/thread/mutex_init.c | 70 | ||||
| -rw-r--r-- | core/src/xmake/thread/mutex_lock.c | 49 | ||||
| -rw-r--r-- | core/src/xmake/thread/mutex_unlock.c | 49 | ||||
| -rw-r--r-- | core/src/xmake/thread/prefix.h | 8 |
6 files changed, 250 insertions, 6 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 2f8467899..2540dff07 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -346,6 +346,12 @@ 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); +// the thread/mutex functions +tb_int_t xm_thread_mutex_init(lua_State* lua); +tb_int_t xm_thread_mutex_exit(lua_State* lua); +tb_int_t xm_thread_mutex_lock(lua_State* lua); +tb_int_t xm_thread_mutex_unlock(lua_State* lua); + // open cjson __tb_extern_c_enter__ tb_int_t luaopen_cjson(lua_State *l); @@ -635,12 +641,16 @@ static luaL_Reg const g_utils_functions[] = // the thread functions 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 } + { "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 } +, { "mutex_init", xm_thread_mutex_init } +, { "mutex_exit", xm_thread_mutex_exit } +, { "mutex_lock", xm_thread_mutex_lock } +, { "mutex_unlock", xm_thread_mutex_unlock } +, { tb_null, tb_null } }; // the lua global instance for signal handler diff --git a/core/src/xmake/thread/mutex_exit.c b/core/src/xmake/thread/mutex_exit.c new file mode 100644 index 000000000..43c31c6c7 --- /dev/null +++ b/core/src/xmake/thread/mutex_exit.c @@ -0,0 +1,58 @@ +/*!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, Xmake Open Source Community. + * + * @author ruki + * @file thread_mutex_exit.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_mutex_exit(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + if (!xm_lua_ispointer(lua, 1)) + return 0; + + xm_thread_mutex_t* thread_mutex = (xm_thread_mutex_t*)xm_lua_topointer(lua, 1); + tb_check_return_val(thread_mutex, 0); + + if (thread_mutex && tb_atomic_fetch_and_sub(&thread_mutex->refn, 1) == 1) + { + if (thread_mutex->handle) + { + tb_mutex_exit(thread_mutex->handle); + thread_mutex->handle = tb_null; + } + tb_free(thread_mutex); + } + lua_pushboolean(lua, tb_true); + return 1; +} + diff --git a/core/src/xmake/thread/mutex_init.c b/core/src/xmake/thread/mutex_init.c new file mode 100644 index 000000000..5476dd970 --- /dev/null +++ b/core/src/xmake/thread/mutex_init.c @@ -0,0 +1,70 @@ +/*!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, Xmake Open Source Community. + * + * @author ruki + * @file mutex_init.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_mutex_init(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + tb_bool_t ok = tb_false; + xm_thread_mutex_t* thread_mutex = tb_null; + do + { + thread_mutex = tb_malloc0_type(xm_thread_mutex_t); + tb_assert_and_check_break(thread_mutex); + + thread_mutex->refn = 0; + thread_mutex->handle = tb_mutex_init(); + tb_assert_and_check_break(thread_mutex->handle); + + xm_lua_pushpointer(lua, (tb_pointer_t)thread_mutex); + ok = tb_true; + + } while (0); + + if (!ok) + { + if (thread_mutex) + { + if (thread_mutex->handle) + { + tb_mutex_exit(thread_mutex->handle); + thread_mutex->handle = tb_null; + } + tb_free(thread_mutex); + } + lua_pushnil(lua); + } + return 1; +} diff --git a/core/src/xmake/thread/mutex_lock.c b/core/src/xmake/thread/mutex_lock.c new file mode 100644 index 000000000..631dfb4a0 --- /dev/null +++ b/core/src/xmake/thread/mutex_lock.c @@ -0,0 +1,49 @@ +/*!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, Xmake Open Source Community. + * + * @author ruki + * @file thread_mutex_lock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_mutex_lock(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + if (!xm_lua_ispointer(lua, 1)) + return 0; + + xm_thread_mutex_t* thread_mutex = (xm_thread_mutex_t*)xm_lua_topointer(lua, 1); + tb_check_return_val(thread_mutex && thread_mutex->handle, 0); + + lua_pushboolean(lua, tb_mutex_enter(thread_mutex->handle)); + return 1; +} + diff --git a/core/src/xmake/thread/mutex_unlock.c b/core/src/xmake/thread/mutex_unlock.c new file mode 100644 index 000000000..09887416c --- /dev/null +++ b/core/src/xmake/thread/mutex_unlock.c @@ -0,0 +1,49 @@ +/*!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, Xmake Open Source Community. + * + * @author ruki + * @file thread_mutex_unlock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_mutex_unlock(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + if (!xm_lua_ispointer(lua, 1)) + return 0; + + xm_thread_mutex_t* thread_mutex = (xm_thread_mutex_t*)xm_lua_topointer(lua, 1); + tb_check_return_val(thread_mutex && thread_mutex->handle, 0); + + lua_pushboolean(lua, tb_mutex_leave(thread_mutex->handle)); + return 1; +} + diff --git a/core/src/xmake/thread/prefix.h b/core/src/xmake/thread/prefix.h index 9f64fd7e6..08eeb73a5 100644 --- a/core/src/xmake/thread/prefix.h +++ b/core/src/xmake/thread/prefix.h @@ -39,6 +39,14 @@ typedef struct __xm_thread_t }xm_thread_t; +// the thread mutex type +typedef struct __xm_thread_mutex_t +{ + tb_mutex_ref_t handle; + tb_atomic_t refn; + +}xm_thread_mutex_t; + #endif |
