diff options
| author | ruki <[email protected]> | 2025-08-28 22:42:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:54 +0800 |
| commit | a3ff692d028873e06cc9827e1ff72dc9ec8b54d4 (patch) | |
| tree | 75815a4402733834ebbd728cb05c7f134d2da51b /core/src/xmake/thread | |
| parent | 09854e59988b3c1269c97dc95ed4f6132bb851f8 (diff) | |
add thread event
Diffstat (limited to 'core/src/xmake/thread')
| -rw-r--r-- | core/src/xmake/thread/event_exit.c | 55 | ||||
| -rw-r--r-- | core/src/xmake/thread/event_incref.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/thread/event_init.c | 70 | ||||
| -rw-r--r-- | core/src/xmake/thread/event_post.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/thread/event_wait.c | 47 | ||||
| -rw-r--r-- | core/src/xmake/thread/prefix.h | 17 |
6 files changed, 281 insertions, 0 deletions
diff --git a/core/src/xmake/thread/event_exit.c b/core/src/xmake/thread/event_exit.c new file mode 100644 index 000000000..54e977068 --- /dev/null +++ b/core/src/xmake/thread/event_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_event_exit.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_event_exit(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); + + if (tb_atomic_fetch_and_sub(&thread_event->refn, 1) == 1) + { + if (thread_event->handle) + { + tb_event_exit(thread_event->handle); + thread_event->handle = tb_null; + } + tb_free(thread_event); + } + lua_pushboolean(lua, tb_true); + return 1; +} + diff --git a/core/src/xmake/thread/event_incref.c b/core/src/xmake/thread/event_incref.c new file mode 100644 index 000000000..ff8b658d9 --- /dev/null +++ b/core/src/xmake/thread/event_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_event_incref.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_event_incref(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); + + lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_event->refn, 1) >= 1); + return 1; +} + diff --git a/core/src/xmake/thread/event_init.c b/core/src/xmake/thread/event_init.c new file mode 100644 index 000000000..942537718 --- /dev/null +++ b/core/src/xmake/thread/event_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 event_init.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_event_init(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + tb_bool_t ok = tb_false; + xm_thread_event_t* thread_event = tb_null; + do + { + thread_event = tb_malloc0_type(xm_thread_event_t); + tb_assert_and_check_break(thread_event); + + thread_event->refn = 1; + thread_event->handle = tb_event_init(); + tb_assert_and_check_break(thread_event->handle); + + xm_lua_pushpointer(lua, (tb_pointer_t)thread_event); + ok = tb_true; + + } while (0); + + if (!ok) + { + if (thread_event) + { + if (thread_event->handle) + { + tb_event_exit(thread_event->handle); + thread_event->handle = tb_null; + } + tb_free(thread_event); + } + lua_pushnil(lua); + } + return 1; +} diff --git a/core/src/xmake/thread/event_post.c b/core/src/xmake/thread/event_post.c new file mode 100644 index 000000000..0b8e5dabd --- /dev/null +++ b/core/src/xmake/thread/event_post.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_event_lock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_event_post(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); + + lua_pushboolean(lua, tb_event_post(thread_event->handle)); + return 1; +} + diff --git a/core/src/xmake/thread/event_wait.c b/core/src/xmake/thread/event_wait.c new file mode 100644 index 000000000..0831036c6 --- /dev/null +++ b/core/src/xmake/thread/event_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_event_unlock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_event_wait(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); + + tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 2); + lua_pushinteger(lua, tb_event_wait(thread_event->handle, timeout)); + return 1; +} + diff --git a/core/src/xmake/thread/prefix.h b/core/src/xmake/thread/prefix.h index 62e1c7729..6c25b234e 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 event type +typedef struct __xm_thread_event_t +{ + tb_event_ref_t handle; + tb_atomic_t refn; + +}xm_thread_event_t; + // the thread mutex type typedef struct __xm_thread_mutex_t { @@ -47,6 +55,15 @@ typedef struct __xm_thread_mutex_t }xm_thread_mutex_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) +{ + xm_thread_event_t* thread_event = tb_null; + if (xm_lua_isinteger(lua, index)) thread_event = (xm_thread_event_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + else if (xm_lua_ispointer(lua, index)) thread_event = (xm_thread_event_t*)xm_lua_topointer(lua, index); + return thread_event; +} + // get the thread mutex from arguments static __tb_inline__ xm_thread_mutex_t* xm_thread_mutex_get(lua_State* lua, tb_int_t index) { |
