diff options
| author | ruki <[email protected]> | 2025-08-28 22:31:24 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:54 +0800 |
| commit | 09854e59988b3c1269c97dc95ed4f6132bb851f8 (patch) | |
| tree | e97acc68739be83d31e71f117ad8d3eec80312a3 | |
| parent | c316009d37c853c070030823c257d1cbcf94ea48 (diff) | |
add mutex refn
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/thread/mutex_incref.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/thread/mutex_init.c | 2 | ||||
| -rw-r--r-- | xmake/core/base/thread.lua | 2 |
4 files changed, 50 insertions, 2 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 1c5bc1a79..8dd4f5656 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -352,6 +352,7 @@ 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_trylock(lua_State* lua); tb_int_t xm_thread_mutex_unlock(lua_State* lua); +tb_int_t xm_thread_mutex_incref(lua_State* lua); // open cjson __tb_extern_c_enter__ @@ -652,6 +653,7 @@ static luaL_Reg const g_thread_functions[] = , { "mutex_lock", xm_thread_mutex_lock } , { "mutex_trylock", xm_thread_mutex_trylock } , { "mutex_unlock", xm_thread_mutex_unlock } +, { "mutex_incref", xm_thread_mutex_incref } , { tb_null, tb_null } }; diff --git a/core/src/xmake/thread/mutex_incref.c b/core/src/xmake/thread/mutex_incref.c new file mode 100644 index 000000000..34a234757 --- /dev/null +++ b/core/src/xmake/thread/mutex_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_mutex_incref.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_incref(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_mutex_t* thread_mutex = xm_thread_mutex_get(lua, 1); + tb_assert_and_check_return_val(thread_mutex && thread_mutex->handle, 0); + + lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_mutex->refn, 1) >= 1); + return 1; +} + diff --git a/core/src/xmake/thread/mutex_init.c b/core/src/xmake/thread/mutex_init.c index 5476dd970..ff69e82ad 100644 --- a/core/src/xmake/thread/mutex_init.c +++ b/core/src/xmake/thread/mutex_init.c @@ -44,7 +44,7 @@ tb_int_t xm_thread_mutex_init(lua_State* lua) thread_mutex = tb_malloc0_type(xm_thread_mutex_t); tb_assert_and_check_break(thread_mutex); - thread_mutex->refn = 0; + thread_mutex->refn = 1; thread_mutex->handle = tb_mutex_init(); tb_assert_and_check_break(thread_mutex->handle); diff --git a/xmake/core/base/thread.lua b/xmake/core/base/thread.lua index 1e0551f75..24686ba49 100644 --- a/xmake/core/base/thread.lua +++ b/xmake/core/base/thread.lua @@ -97,8 +97,8 @@ function _thread:start() for _, arg in ipairs(self._ARGV) do -- is mutex? we can only pass cdata address if type(arg) == "table" and arg._LOCK and arg.cdata then + thread.mutex_incref(arg:cdata()) arg = {mutex = true, name = arg:name(), caddr = libc.dataptr(arg:cdata())} - -- TODO increase refn end table.insert(argv, arg) end |
