diff options
| author | ruki <[email protected]> | 2025-08-28 22:45:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:54 +0800 |
| commit | 8a331ab64fe560924b8bc3ab3dc4ac2831dd8920 (patch) | |
| tree | 1b7406be4f6db49c198d36d180b4f98ff8185847 /core/src | |
| parent | a3ff692d028873e06cc9827e1ff72dc9ec8b54d4 (diff) | |
add thread semaphore
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/engine.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/thread/prefix.h | 17 | ||||
| -rw-r--r-- | core/src/xmake/thread/semaphore_exit.c | 55 | ||||
| -rw-r--r-- | core/src/xmake/thread/semaphore_incref.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/thread/semaphore_init.c | 72 | ||||
| -rw-r--r-- | core/src/xmake/thread/semaphore_post.c | 47 | ||||
| -rw-r--r-- | core/src/xmake/thread/semaphore_wait.c | 47 |
7 files changed, 313 insertions, 17 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 24b95d1fd..850be59f4 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -361,6 +361,13 @@ tb_int_t xm_thread_event_post(lua_State* lua); tb_int_t xm_thread_event_wait(lua_State* lua); tb_int_t xm_thread_event_incref(lua_State* lua); +// the thread/semaphore functions +tb_int_t xm_thread_semaphore_init(lua_State* lua); +tb_int_t xm_thread_semaphore_exit(lua_State* lua); +tb_int_t xm_thread_semaphore_post(lua_State* lua); +tb_int_t xm_thread_semaphore_wait(lua_State* lua); +tb_int_t xm_thread_semaphore_incref(lua_State* lua); + // open cjson __tb_extern_c_enter__ tb_int_t luaopen_cjson(lua_State *l); @@ -650,23 +657,28 @@ 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 } -, { "mutex_init", xm_thread_mutex_init } -, { "mutex_exit", xm_thread_mutex_exit } -, { "mutex_lock", xm_thread_mutex_lock } -, { "mutex_trylock", xm_thread_mutex_trylock } -, { "mutex_unlock", xm_thread_mutex_unlock } -, { "mutex_incref", xm_thread_mutex_incref } -, { "event_init", xm_thread_event_init } -, { "event_exit", xm_thread_event_exit } -, { "event_post", xm_thread_event_post } -, { "event_wait", xm_thread_event_wait } -, { "event_incref", xm_thread_event_incref } -, { 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_trylock", xm_thread_mutex_trylock } +, { "mutex_unlock", xm_thread_mutex_unlock } +, { "mutex_incref", xm_thread_mutex_incref } +, { "event_init", xm_thread_event_init } +, { "event_exit", xm_thread_event_exit } +, { "event_post", xm_thread_event_post } +, { "event_wait", xm_thread_event_wait } +, { "event_incref", xm_thread_event_incref } +, { "semaphore_init", xm_thread_semaphore_init } +, { "semaphore_exit", xm_thread_semaphore_exit } +, { "semaphore_post", xm_thread_semaphore_post } +, { "semaphore_wait", xm_thread_semaphore_wait } +, { "semaphore_incref", xm_thread_semaphore_incref } +, { tb_null, tb_null } }; // the lua global instance for signal handler diff --git a/core/src/xmake/thread/prefix.h b/core/src/xmake/thread/prefix.h index 6c25b234e..be06d952e 100644 --- a/core/src/xmake/thread/prefix.h +++ b/core/src/xmake/thread/prefix.h @@ -55,6 +55,14 @@ typedef struct __xm_thread_mutex_t }xm_thread_mutex_t; +// the thread semaphore type +typedef struct __xm_thread_semaphore_t +{ + tb_semaphore_ref_t handle; + tb_atomic_t refn; + +}xm_thread_semaphore_t; + // get the thread event from arguments static __tb_inline__ xm_thread_event_t* xm_thread_event_get(lua_State* lua, tb_int_t index) { @@ -73,6 +81,15 @@ static __tb_inline__ xm_thread_mutex_t* xm_thread_mutex_get(lua_State* lua, tb_i return thread_mutex; } +// get the thread semaphore from arguments +static __tb_inline__ xm_thread_semaphore_t* xm_thread_semaphore_get(lua_State* lua, tb_int_t index) +{ + xm_thread_semaphore_t* thread_semaphore = tb_null; + if (xm_lua_isinteger(lua, index)) thread_semaphore = (xm_thread_semaphore_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + else if (xm_lua_ispointer(lua, index)) thread_semaphore = (xm_thread_semaphore_t*)xm_lua_topointer(lua, index); + return thread_semaphore; +} + #endif diff --git a/core/src/xmake/thread/semaphore_exit.c b/core/src/xmake/thread/semaphore_exit.c new file mode 100644 index 000000000..cffffa249 --- /dev/null +++ b/core/src/xmake/thread/semaphore_exit.c @@ -0,0 +1,55 @@ +/*!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_semaphore_exit.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_semaphore_exit(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); + + if (tb_atomic_fetch_and_sub(&thread_semaphore->refn, 1) == 1) + { + if (thread_semaphore->handle) + { + tb_semaphore_exit(thread_semaphore->handle); + thread_semaphore->handle = tb_null; + } + tb_free(thread_semaphore); + } + lua_pushboolean(lua, tb_true); + return 1; +} + diff --git a/core/src/xmake/thread/semaphore_incref.c b/core/src/xmake/thread/semaphore_incref.c new file mode 100644 index 000000000..84c2118d3 --- /dev/null +++ b/core/src/xmake/thread/semaphore_incref.c @@ -0,0 +1,46 @@ +/*!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_semaphore_incref.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_semaphore_incref(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); + + lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_semaphore->refn, 1) >= 1); + return 1; +} + diff --git a/core/src/xmake/thread/semaphore_init.c b/core/src/xmake/thread/semaphore_init.c new file mode 100644 index 000000000..976b48de9 --- /dev/null +++ b/core/src/xmake/thread/semaphore_init.c @@ -0,0 +1,72 @@ +/*!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 semaphore_init.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_semaphore_init(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + tb_bool_t ok = tb_false; + xm_thread_semaphore_t* thread_semaphore = tb_null; + do + { + tb_long_t value = (tb_long_t)luaL_checknumber(lua, 1); + + thread_semaphore = tb_malloc0_type(xm_thread_semaphore_t); + tb_assert_and_check_break(thread_semaphore); + + thread_semaphore->refn = 1; + thread_semaphore->handle = tb_semaphore_init(value); + tb_assert_and_check_break(thread_semaphore->handle); + + xm_lua_pushpointer(lua, (tb_pointer_t)thread_semaphore); + ok = tb_true; + + } while (0); + + if (!ok) + { + if (thread_semaphore) + { + if (thread_semaphore->handle) + { + tb_semaphore_exit(thread_semaphore->handle); + thread_semaphore->handle = tb_null; + } + tb_free(thread_semaphore); + } + lua_pushnil(lua); + } + return 1; +} diff --git a/core/src/xmake/thread/semaphore_post.c b/core/src/xmake/thread/semaphore_post.c new file mode 100644 index 000000000..9ca94bda5 --- /dev/null +++ b/core/src/xmake/thread/semaphore_post.c @@ -0,0 +1,47 @@ +/*!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_semaphore_lock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_semaphore_post(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); + + tb_long_t value = (tb_long_t)luaL_checknumber(lua, 2); + lua_pushboolean(lua, tb_semaphore_post(thread_semaphore->handle, value)); + return 1; +} + diff --git a/core/src/xmake/thread/semaphore_wait.c b/core/src/xmake/thread/semaphore_wait.c new file mode 100644 index 000000000..c8e4d1491 --- /dev/null +++ b/core/src/xmake/thread/semaphore_wait.c @@ -0,0 +1,47 @@ +/*!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_semaphore_unlock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_semaphore_wait(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); + + tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 2); + lua_pushinteger(lua, tb_semaphore_wait(thread_semaphore->handle, timeout)); + return 1; +} + |
