summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-16 00:50:55 +0800
committerruki <[email protected]>2025-08-28 11:35:53 +0800
commitd8862afe89a16a9f4ad0a21188bd174b3c81a935 (patch)
tree3e057180ed3a87cddd1f31f804712debdb77e2b3
parent6adec9d467ccfa7da048944df3dddfef59e75fe5 (diff)
improve thread apis
-rw-r--r--tests/modules/thread/sleep.lua3
-rw-r--r--xmake/core/sandbox/modules/import/core/thread/thread.lua16
-rw-r--r--xmake/core/thread/thread.lua33
3 files changed, 36 insertions, 16 deletions
diff --git a/tests/modules/thread/sleep.lua b/tests/modules/thread/sleep.lua
index 75586d615..1890278b3 100644
--- a/tests/modules/thread/sleep.lua
+++ b/tests/modules/thread/sleep.lua
@@ -10,8 +10,7 @@ end
function main()
for i = 1, 10 do
- -- TODO use thread.start and group
- thread.new(callback, {argv = {i}, name = "session_" .. i}):start()
+ thread.start_named("thread_" .. i, callback, i)
end
--thread.wait()
end
diff --git a/xmake/core/sandbox/modules/import/core/thread/thread.lua b/xmake/core/sandbox/modules/import/core/thread/thread.lua
index d88b6ef1f..39a808341 100644
--- a/xmake/core/sandbox/modules/import/core/thread/thread.lua
+++ b/xmake/core/sandbox/modules/import/core/thread/thread.lua
@@ -70,6 +70,7 @@ function sandbox_core_thread_instance.start(instance)
if not ok then
raise(errors)
end
+ return instance
end
-- suspend thread
@@ -105,6 +106,21 @@ function sandbox_core_thread.new(callback, opt)
return _thread_wrap(instance)
end
+-- start a thread
+function sandbox_core_thread.start(callback, ...)
+ return sandbox_core_thread.start_withopt(callback, {argv = table.pack(...)})
+end
+
+-- start a named thread
+function sandbox_core_thread.start_named(name, callback, ...)
+ return sandbox_core_thread.start_withopt(callback, {name = name, argv = table.pack(...)})
+end
+
+-- start a thread with options
+function sandbox_core_thread.start_withopt(callback, opt)
+ return sandbox_core_thread.new(callback, opt):start()
+end
+
-- get the running thread
function sandbox_core_thread.running()
local instance, errors = thread.running()
diff --git a/xmake/core/thread/thread.lua b/xmake/core/thread/thread.lua
index 3c8170f69..ddc96543e 100644
--- a/xmake/core/thread/thread.lua
+++ b/xmake/core/thread/thread.lua
@@ -36,11 +36,14 @@ thread.STATUS_SUSPENDED = 3
thread.STATUS_DEAD = 4
-- new a thread
-function _instance.new(name, callback, handle, opt)
- local instance = table.inherit(_instance)
- instance._NAME = name or "anonymous"
- instance._HANLDE = handler
- instance._STATUS = thread.STATUS_READY
+function _instance.new(callback, opt)
+ opt = opt or {}
+ local instance = table.inherit(_instance)
+ instance._NAME = opt.name or "anonymous"
+ instance._ARGV = opt.argv
+ instance._CALLBACK = opt.callback
+ instance._STACKSIZE = opt.stacksize or 0
+ instance._STATUS = thread.STATUS_READY
setmetatable(instance, _instance)
return instance
end
@@ -82,6 +85,14 @@ end
-- start thread
function _instance:start(instance)
+ if not self:is_ready() then
+ end
+
+ -- TODO
+ local handle, errors = thread.thread_create(name, callback, argv, stacksize)
+ if not handle then
+ return nil, errors or string.format("failed to create thread(%s)!", name)
+ end
end
-- suspend thread
@@ -126,16 +137,10 @@ end
-- @return the thread instance
--
function thread.new(callback, opt)
- opt = opt or {}
- local name = opt.name
- local argv = opt.argv
- local stacksize = opt.stacksize
- local handle, errors = thread.thread_create(name, callback, argv, stacksize)
- if handle then
- return _instance.new(name, callback, handle, opt)
- else
- return nil, errors or string.format("failed to create thread(%s)!", name)
+ if callback == nil then
+ return nil, "invalid thread, callback is nil"
end
+ return _instance.new(callback, opt)
end
-- get the running thread