summaryrefslogtreecommitdiff
path: root/xmake/core
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-16 22:32:58 +0800
committerruki <[email protected]>2025-08-28 11:35:53 +0800
commitd985de5de38fb0d041f8e9182c076575603b5b4f (patch)
treee3f4d6fa47335e74285ce5888495190717b00383 /xmake/core
parentd8862afe89a16a9f4ad0a21188bd174b3c81a935 (diff)
improve api and tests
Diffstat (limited to 'xmake/core')
-rw-r--r--xmake/core/sandbox/modules/import/core/thread/thread.lua23
-rw-r--r--xmake/core/thread/thread.lua60
2 files changed, 58 insertions, 25 deletions
diff --git a/xmake/core/sandbox/modules/import/core/thread/thread.lua b/xmake/core/sandbox/modules/import/core/thread/thread.lua
index 39a808341..57fd10485 100644
--- a/xmake/core/sandbox/modules/import/core/thread/thread.lua
+++ b/xmake/core/sandbox/modules/import/core/thread/thread.lua
@@ -28,24 +28,11 @@ local raise = require("sandbox/modules/raise")
local sandbox_core_thread = sandbox_core_thread or {}
local sandbox_core_thread_instance = sandbox_core_thread_instance or {}
--- export the thread types
-sandbox_core_thread.TCP = thread.TCP
-sandbox_core_thread.UDP = thread.UDP
-sandbox_core_thread.ICMP = thread.ICMP
-
--- export the thread families
-sandbox_core_thread.IPV4 = thread.IPV4
-sandbox_core_thread.IPV6 = thread.IPV6
-
--- export the thread events
-sandbox_core_thread.EV_RECV = thread.EV_RECV
-sandbox_core_thread.EV_SEND = thread.EV_SEND
-sandbox_core_thread.EV_CONN = thread.EV_CONN
-sandbox_core_thread.EV_ACPT = thread.EV_ACPT
-
--- export the thread control code
-sandbox_core_thread.CTRL_SET_RECVBUFF = thread.CTRL_SET_RECVBUFF
-sandbox_core_thread.CTRL_SET_SENDBUFF = thread.CTRL_SET_SENDBUFF
+-- export the thread status
+sandbox_core_thread.STATUS_READY = thread.STATUS_READY
+sandbox_core_thread.STATUS_RUNNING = thread.STATUS_RUNNING
+sandbox_core_thread.STATUS_SUSPENDED = thread.STATUS_SUSPENDED
+sandbox_core_thread.STATUS_DEAD = thread.STATUS_DEAD
-- wrap thread
function _thread_wrap(instance)
diff --git a/xmake/core/thread/thread.lua b/xmake/core/thread/thread.lua
index ddc96543e..3a3de2705 100644
--- a/xmake/core/thread/thread.lua
+++ b/xmake/core/thread/thread.lua
@@ -84,27 +84,72 @@ function _instance:is_dead()
end
-- start thread
-function _instance:start(instance)
+function _instance:start()
if not self:is_ready() then
+ return nil, string.format("%s: cannot start non-ready thread!", self)
end
+ assert(not self:cdata())
- -- TODO
- local handle, errors = thread.thread_create(name, callback, argv, stacksize)
+ local handle, errors = thread.thread_create(self:name(), self._CALLBACK, self._ARGV, self._STACKSIZE)
if not handle then
- return nil, errors or string.format("failed to create thread(%s)!", name)
+ return nil, errors or string.format("%s: failed to create thread!", self)
end
+
+ self._HANLDE = handle
+ self._STATUS = thread.STATUS_RUNNING
+ return true
end
-- suspend thread
-function _instance:suspend(instance)
+function _instance:suspend()
+ if not self:is_running() then
+ return nil, string.format("%s: cannot suspend non-running thread!", self)
+ end
+ assert(self:cdata())
+
+ local ok, errors = thread.thread_suspend(self:cdata())
+ if not ok then
+ return nil, errors or string.format("%s: failed to suspend thread!", self)
+ end
+
+ self._STATUS = thread.STATUS_SUSPENDED
+ return true
end
-- resume thread
-function _instance:resume(instance)
+function _instance:resume()
+ if not self:is_suspended() then
+ return nil, string.format("%s: cannot suspend non-suspended thread!", self)
+ end
+ assert(self:cdata())
+
+ local ok, errors = thread.thread_resume(self:cdata())
+ if not ok then
+ return nil, errors or string.format("%s: failed to resume thread!", self)
+ end
+
+ self._STATUS = thread.STATUS_RUNNING
+ return true
end
-- wait thread
-function _instance:wait(instance, timeout)
+function _instance:wait(timeout)
+ if self:is_dead() then
+ return 1
+ elseif self:is_ready() then
+ return -1, string.format("%s: cannot wait ready thread!", self)
+ end
+ assert(self:cdata())
+
+ local ok, errors = thread.thread_wait(self:cdata(), timeout)
+ if ok < 0 then
+ return -1, errors or string.format("%s: failed to resume thread!", self)
+ end
+
+ if ok > 0 then
+ self._STATUS = thread.STATUS_DEAD
+ end
+ return ok
end
-- tostring(thread)
@@ -145,6 +190,7 @@ end
-- get the running thread
function thread.running()
+ -- TODO
end
-- return module