diff options
| author | ruki <[email protected]> | 2025-04-16 22:32:58 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:53 +0800 |
| commit | d985de5de38fb0d041f8e9182c076575603b5b4f (patch) | |
| tree | e3f4d6fa47335e74285ce5888495190717b00383 /xmake/core/thread/thread.lua | |
| parent | d8862afe89a16a9f4ad0a21188bd174b3c81a935 (diff) | |
improve api and tests
Diffstat (limited to 'xmake/core/thread/thread.lua')
| -rw-r--r-- | xmake/core/thread/thread.lua | 60 |
1 files changed, 53 insertions, 7 deletions
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 |
