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 | |
| parent | a3ff692d028873e06cc9827e1ff72dc9ec8b54d4 (diff) | |
add thread semaphore
| -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 | ||||
| -rw-r--r-- | tests/modules/thread/semaphore.lua | 25 | ||||
| -rw-r--r-- | xmake/core/base/thread.lua | 116 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/thread.lua | 55 |
10 files changed, 497 insertions, 29 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; +} + diff --git a/tests/modules/thread/semaphore.lua b/tests/modules/thread/semaphore.lua new file mode 100644 index 000000000..b05774e1d --- /dev/null +++ b/tests/modules/thread/semaphore.lua @@ -0,0 +1,25 @@ +import("core.base.thread") + +function callback(semaphore) + import("core.base.thread") + print("%s: starting ..", thread.running()) + while true do + print("%s: waiting ..", thread.running()) + if semaphore:wait(-1) > 0 then + print("%s: triggered", thread.running()) + end + end +end + +function main() + local semaphore = thread.semaphore("", 1) + local t = thread.start_named("keyboard", callback, semaphore) + while true do + local ch = io.read() + if ch then + semaphore:post(2) + end + end + t:wait(-1) +end + diff --git a/xmake/core/base/thread.lua b/xmake/core/base/thread.lua index 5428dc3f2..c924724b8 100644 --- a/xmake/core/base/thread.lua +++ b/xmake/core/base/thread.lua @@ -19,10 +19,11 @@ -- -- define module -local thread = thread or {} -local _thread = _thread or {} -local _mutex = _mutex or {} -local _event = _event or {} +local thread = thread or {} +local _thread = _thread or {} +local _mutex = _mutex or {} +local _event = _event or {} +local _semaphore = _semaphore or {} -- load modules local io = require("base/io") @@ -104,6 +105,10 @@ function _thread:start() elseif type(arg) == "table" and arg._EVENT and arg.cdata then thread.event_incref(arg:cdata()) arg = {event = true, name = arg:name(), caddr = libc.dataptr(arg:cdata())} + -- is semaphore? we can only pass cdata address + elseif type(arg) == "table" and arg._SEMAPHORE and arg.cdata then + thread.semaphore_incref(arg:cdata()) + arg = {semaphore = true, name = arg:name(), caddr = libc.dataptr(arg:cdata())} end table.insert(argv, arg) end @@ -379,8 +384,7 @@ function _event:close() ok = thread.event_exit(self:cdata()) if ok then - self._MUTEX = nil - self._LOCKED_NUM = 0 + self._EVENT = nil end return ok end @@ -401,8 +405,92 @@ end -- gc(event) function _event:__gc() if self:cdata() and thread.event_exit(self:cdata()) then - self._MUTEX = nil - self._LOCKED_NUM = 0 + self._EVENT = nil + end +end + +-- new an semaphore +function _semaphore.new(name, cdata) + local semaphore = table.inherit(_semaphore) + semaphore._NAME = name + semaphore._SEMAPHORE = cdata + setmetatable(semaphore, _semaphore) + return semaphore +end + +-- get the semaphore name +function _semaphore:name() + return self._NAME +end + +-- get the cdata +function _semaphore:cdata() + return self._SEMAPHORE +end + +-- post semaphore +-- +-- @param value the semaphore value +-- +-- @return ok, errors +-- +function _semaphore:post(value) + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + if not thread.semaphore_post(self:cdata(), value) then + return false, string.format("%s: post failed!", self) + end + return true +end + +-- wait semaphore +function _semaphore:wait(timeout) + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + local ok, errors = thread.semaphore_wait(self:cdata(), timeout) + if ok < 0 then + return false, string.format("%s: wait failed, errors: %s!", self, errors or "unknown") + end + return ok +end + +-- close semaphore +function _semaphore:close() + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + ok = thread.semaphore_exit(self:cdata()) + if ok then + self._SEMAPHORE = nil + end + return ok +end + +-- ensure the file is opened +function _semaphore:_ensure_opened() + if not self:cdata() then + return false, string.format("%s: has been closed!", self) + end + return true +end + +-- tostring(semaphore) +function _semaphore:__tostring() + return "<semaphore: " .. (self:name() or tostring(self:cdata())) .. ">" +end + +-- gc(semaphore) +function _semaphore:__gc() + if self:cdata() and thread.semaphore_exit(self:cdata()) then + self._SEMAPHORE = nil end end @@ -486,6 +574,8 @@ function thread._run_thread(callback_str, callinfo_str) arg = _mutex.new(arg.name, libc.ptraddr(arg.caddr)) elseif type(arg) == "table" and arg.event and arg.caddr then arg = _event.new(arg.name, libc.ptraddr(arg.caddr)) + elseif type(arg) == "table" and arg.semaphore and arg.caddr then + arg = _semaphore.new(arg.name, libc.ptraddr(arg.caddr)) end table.insert(newargv, arg) end @@ -516,6 +606,16 @@ function thread.event(name) end end +-- open a semaphore +function thread.semaphore(name, value) + local semaphore = thread.semaphore_init(value or 0) + if semaphore then + return _semaphore.new(name, semaphore) + else + return nil, string.format("cannot open semaphore: %s", os.strerror()) + end +end + -- return module return thread diff --git a/xmake/core/sandbox/modules/import/core/base/thread.lua b/xmake/core/sandbox/modules/import/core/base/thread.lua index d3b5d4ae4..cb8de9ac6 100644 --- a/xmake/core/sandbox/modules/import/core/base/thread.lua +++ b/xmake/core/sandbox/modules/import/core/base/thread.lua @@ -25,10 +25,11 @@ local thread = require("base/thread") local raise = require("sandbox/modules/raise") -- define module -local sandbox_core_base_thread = sandbox_core_base_thread or {} -local sandbox_core_base_thread_instance = sandbox_core_base_thread_instance or {} -local sandbox_core_base_thread_mutex = sandbox_core_base_thread_mutex or {} -local sandbox_core_base_thread_event = sandbox_core_base_thread_event or {} +local sandbox_core_base_thread = sandbox_core_base_thread or {} +local sandbox_core_base_thread_instance = sandbox_core_base_thread_instance or {} +local sandbox_core_base_thread_mutex = sandbox_core_base_thread_mutex or {} +local sandbox_core_base_thread_event = sandbox_core_base_thread_event or {} +local sandbox_core_base_thread_semaphore = sandbox_core_base_thread_semaphore or {} -- export the thread status sandbox_core_base_thread.STATUS_READY = thread.STATUS_READY @@ -135,6 +136,31 @@ function sandbox_core_base_thread_event.close(event) end end +-- post semaphore +function sandbox_core_base_thread_semaphore.post(semaphore, value) + local ok, errors = semaphore:_post(value) + if not ok then + raise(errors) + end +end + +-- wait semaphore +function sandbox_core_base_thread_semaphore.wait(semaphore, timeout) + local ok, errors = semaphore:_wait(timeout) + if ok < 0 then + raise(errors) + end + return ok +end + +-- close semaphore +function sandbox_core_base_thread_semaphore.close(semaphore) + local ok, errors = semaphore:_close() + if not ok then + raise(errors) + end +end + -- new thread function sandbox_core_base_thread.new(callback, opt) local instance, errors = thread.new(callback, opt) @@ -210,6 +236,27 @@ function sandbox_core_base_thread.event(name) return event end +-- open a semaphore +function sandbox_core_base_thread.semaphore(name, value) + local semaphore, errors = thread.semaphore(name, value) + if not semaphore then + raise(errors) + end + + -- hook filesemaphore interfaces + local hooked = {} + for name, func in pairs(sandbox_core_base_thread_semaphore) do + if not name:startswith("_") and type(func) == "function" then + hooked["_" .. name] = semaphore["_" .. name] or semaphore[name] + hooked[name] = func + end + end + for name, func in pairs(hooked) do + semaphore[name] = func + end + return semaphore +end + -- return module return sandbox_core_base_thread |
