diff options
| author | ruki <[email protected]> | 2025-08-28 23:14:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 23:14:13 +0800 |
| commit | 628371d0edea22eb177fa2b1a6b9f28e8c0cff2d (patch) | |
| tree | 2c8e834e76870f3cfa2e2b2b3b3342d69b0f83bf /core/src | |
| parent | 8a331ab64fe560924b8bc3ab3dc4ac2831dd8920 (diff) | |
add thread queue
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/engine.c | 16 | ||||
| -rw-r--r-- | core/src/xmake/thread/prefix.h | 43 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_clear.c | 47 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_exit.c | 55 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_incref.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_init.c | 87 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_pop.c | 92 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_push.c | 96 | ||||
| -rw-r--r-- | core/src/xmake/thread/queue_size.c | 46 |
9 files changed, 528 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 850be59f4..d3d48c7cc 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -368,6 +368,15 @@ 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); +// the thread/queue functions +tb_int_t xm_thread_queue_init(lua_State* lua); +tb_int_t xm_thread_queue_exit(lua_State* lua); +tb_int_t xm_thread_queue_size(lua_State* lua); +tb_int_t xm_thread_queue_clear(lua_State* lua); +tb_int_t xm_thread_queue_incref(lua_State* lua); +tb_int_t xm_thread_queue_push(lua_State* lua); +tb_int_t xm_thread_queue_pop(lua_State* lua); + // open cjson __tb_extern_c_enter__ tb_int_t luaopen_cjson(lua_State *l); @@ -678,6 +687,13 @@ static luaL_Reg const g_thread_functions[] = , { "semaphore_post", xm_thread_semaphore_post } , { "semaphore_wait", xm_thread_semaphore_wait } , { "semaphore_incref", xm_thread_semaphore_incref } +, { "queue_init", xm_thread_queue_init } +, { "queue_exit", xm_thread_queue_exit } +, { "queue_size", xm_thread_queue_size } +, { "queue_clear", xm_thread_queue_clear } +, { "queue_incref", xm_thread_queue_incref } +, { "queue_push", xm_thread_queue_push } +, { "queue_pop", xm_thread_queue_pop } , { tb_null, tb_null } }; diff --git a/core/src/xmake/thread/prefix.h b/core/src/xmake/thread/prefix.h index be06d952e..8fc303b10 100644 --- a/core/src/xmake/thread/prefix.h +++ b/core/src/xmake/thread/prefix.h @@ -63,6 +63,40 @@ typedef struct __xm_thread_semaphore_t }xm_thread_semaphore_t; +// the thread queue type +typedef struct __xm_thread_queue_t +{ + tb_queue_ref_t handle; + tb_atomic_t refn; + +}xm_thread_queue_t; + +// the thread queue item kind +typedef enum __xm_thread_queue_item_kind_e +{ + XM_THREAD_QUEUE_ITEM_NIL = 0, + XM_THREAD_QUEUE_ITEM_BOOL = 1, + XM_THREAD_QUEUE_ITEM_INT = 2, + XM_THREAD_QUEUE_ITEM_NUM = 3, + XM_THREAD_QUEUE_ITEM_STR = 4 + +}xm_thread_queue_item_kind_e; + +// the thread queue item type +typedef struct __xm_thread_queue_item_t +{ + tb_uint32_t kind : 3; + tb_uint32_t size : 29; + union + { + tb_char_t* string; + tb_bool_t boolean; + lua_Integer integer; + lua_Number number; + } u; + +}xm_thread_queue_item_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) { @@ -90,6 +124,15 @@ static __tb_inline__ xm_thread_semaphore_t* xm_thread_semaphore_get(lua_State* l return thread_semaphore; } +// get the thread queue from arguments +static __tb_inline__ xm_thread_queue_t* xm_thread_queue_get(lua_State* lua, tb_int_t index) +{ + xm_thread_queue_t* thread_queue = tb_null; + if (xm_lua_isinteger(lua, index)) thread_queue = (xm_thread_queue_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + else if (xm_lua_ispointer(lua, index)) thread_queue = (xm_thread_queue_t*)xm_lua_topointer(lua, index); + return thread_queue; +} + #endif diff --git a/core/src/xmake/thread/queue_clear.c b/core/src/xmake/thread/queue_clear.c new file mode 100644 index 000000000..bf70d12e1 --- /dev/null +++ b/core/src/xmake/thread/queue_clear.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_queue_lock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_clear(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); + + tb_queue_clear(thread_queue->handle); + lua_pushboolean(lua, tb_true); + return 1; +} + diff --git a/core/src/xmake/thread/queue_exit.c b/core/src/xmake/thread/queue_exit.c new file mode 100644 index 000000000..8178ea22f --- /dev/null +++ b/core/src/xmake/thread/queue_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_queue_exit.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_exit(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); + + if (tb_atomic_fetch_and_sub(&thread_queue->refn, 1) == 1) + { + if (thread_queue->handle) + { + tb_queue_exit(thread_queue->handle); + thread_queue->handle = tb_null; + } + tb_free(thread_queue); + } + lua_pushboolean(lua, tb_true); + return 1; +} + diff --git a/core/src/xmake/thread/queue_incref.c b/core/src/xmake/thread/queue_incref.c new file mode 100644 index 000000000..4f06807c4 --- /dev/null +++ b/core/src/xmake/thread/queue_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_queue_incref.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_incref(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); + + lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_queue->refn, 1) >= 1); + return 1; +} + diff --git a/core/src/xmake/thread/queue_init.c b/core/src/xmake/thread/queue_init.c new file mode 100644 index 000000000..4c4e9a9f6 --- /dev/null +++ b/core/src/xmake/thread/queue_init.c @@ -0,0 +1,87 @@ +/*!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 queue_init.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_void_t xm_thread_queue_item_free(tb_element_ref_t element, tb_pointer_t buff) +{ + xm_thread_queue_item_t* item = (xm_thread_queue_item_t*)buff; + if (item) + { + if (item->kind == XM_THREAD_QUEUE_ITEM_STR) + { + if (item->u.string) tb_free((tb_pointer_t)item->u.string); + item->u.string = tb_null; + } + item->size = 0; + } +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_init(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + tb_bool_t ok = tb_false; + xm_thread_queue_t* thread_queue = tb_null; + do + { + thread_queue = tb_malloc0_type(xm_thread_queue_t); + tb_assert_and_check_break(thread_queue); + + thread_queue->refn = 1; + thread_queue->handle = tb_queue_init(0, tb_element_mem(sizeof(xm_thread_queue_item_t), xm_thread_queue_item_free, tb_null)); + tb_assert_and_check_break(thread_queue->handle); + + xm_lua_pushpointer(lua, (tb_pointer_t)thread_queue); + ok = tb_true; + + } while (0); + + if (!ok) + { + if (thread_queue) + { + if (thread_queue->handle) + { + tb_queue_exit(thread_queue->handle); + thread_queue->handle = tb_null; + } + tb_free(thread_queue); + } + lua_pushnil(lua); + } + return 1; +} diff --git a/core/src/xmake/thread/queue_pop.c b/core/src/xmake/thread/queue_pop.c new file mode 100644 index 000000000..4e205bab4 --- /dev/null +++ b/core/src/xmake/thread/queue_pop.c @@ -0,0 +1,92 @@ +/*!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_queue_unlock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_pop(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); + + if (tb_queue_null(thread_queue->handle)) + { + lua_pushnil(lua); + lua_pushliteral(lua, "the thread queue is empty"); + return 2; + } + + xm_thread_queue_item_t* item = (xm_thread_queue_item_t*)tb_queue_get(thread_queue->handle); + tb_assert_and_check_return_val(item, 0); + + tb_bool_t ok = tb_false; + switch (item->kind) + { + case XM_THREAD_QUEUE_ITEM_STR: + if (item->size) + lua_pushlstring(lua, item->u.string, item->size); + else lua_pushliteral(lua, ""); + ok = tb_true; + break; + case XM_THREAD_QUEUE_ITEM_INT: + lua_pushinteger(lua, item->u.integer); + ok = tb_true; + break; + case XM_THREAD_QUEUE_ITEM_NUM: + lua_pushnumber(lua, item->u.number); + ok = tb_true; + break; + case XM_THREAD_QUEUE_ITEM_BOOL: + lua_pushboolean(lua, item->u.boolean); + ok = tb_true; + break; + case XM_THREAD_QUEUE_ITEM_NIL: + lua_pushnil(lua); + ok = tb_true; + break; + default: + break; + } + + if (!ok) + { + lua_pushnil(lua); + lua_pushliteral(lua, "invalid thread queue item"); + return 2; + } + + tb_queue_pop(thread_queue->handle); + return 1; +} + diff --git a/core/src/xmake/thread/queue_push.c b/core/src/xmake/thread/queue_push.c new file mode 100644 index 000000000..510a6b89b --- /dev/null +++ b/core/src/xmake/thread/queue_push.c @@ -0,0 +1,96 @@ +/*!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_queue_lock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_push(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); + + if (tb_queue_full(thread_queue->handle)) + { + lua_pushboolean(lua, tb_false); + lua_pushliteral(lua, "the thread queue is full"); + return 2; + } + + xm_thread_queue_item_t item; + if (lua_isstring(lua, 2)) + { + size_t data_size = 0; + tb_char_t const* data = luaL_checklstring(lua, 2, &data_size); + tb_assert_and_check_return_val(data, 0); + + item.kind = (tb_uint32_t)XM_THREAD_QUEUE_ITEM_STR; + item.size = (tb_uint32_t)data_size; + if (data_size) + { + item.u.string = tb_malloc_cstr(data_size); + tb_assert_and_check_return_val(item.u.string, 0); + tb_memcpy(item.u.string, data, data_size); + } + } + else if (lua_isinteger(lua, 2)) + { + item.kind = (tb_uint32_t)XM_THREAD_QUEUE_ITEM_INT; + item.u.integer = lua_tointeger(lua, 2); + } + else if (lua_isnumber(lua, 2)) + { + item.kind = (tb_uint32_t)XM_THREAD_QUEUE_ITEM_NUM; + item.u.number = lua_tonumber(lua, 2); + } + else if (lua_isboolean(lua, 2)) + { + item.kind = (tb_uint32_t)XM_THREAD_QUEUE_ITEM_BOOL; + item.u.boolean = lua_toboolean(lua, 2); + } + else if (lua_isnil(lua, 2)) + { + item.kind = (tb_uint32_t)XM_THREAD_QUEUE_ITEM_NIL; + } + else + { + lua_pushboolean(lua, tb_false); + lua_pushliteral(lua, "unsupported thread queue item"); + return 2; + } + + tb_queue_put(thread_queue->handle, &item); + lua_pushboolean(lua, tb_true); + return 1; +} + diff --git a/core/src/xmake/thread/queue_size.c b/core/src/xmake/thread/queue_size.c new file mode 100644 index 000000000..559a1dceb --- /dev/null +++ b/core/src/xmake/thread/queue_size.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_queue_lock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_thread_queue_size(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, 0); + + xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); + + lua_pushinteger(lua, (tb_int_t)tb_queue_size(thread_queue->handle)); + return 1; +} + |
