diff options
| author | ruki <[email protected]> | 2025-09-05 00:54:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-09-05 00:54:55 +0800 |
| commit | 0cc6892259f0ae9a94035fa4408f9fb47980189e (patch) | |
| tree | c5ccc6097dd3cb7b675ab2f372d62784b3977759 /xmake/core/base/thread.lua | |
| parent | 6b7638a37f0fb73b021d6f04ac51479ab02ce1cd (diff) | |
fix thread wait
Diffstat (limited to 'xmake/core/base/thread.lua')
| -rw-r--r-- | xmake/core/base/thread.lua | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/xmake/core/base/thread.lua b/xmake/core/base/thread.lua index d76f94f64..3856feb81 100644 --- a/xmake/core/base/thread.lua +++ b/xmake/core/base/thread.lua @@ -132,7 +132,7 @@ function _thread:start() local callinfo = {name = self:name(), argv = argv} -- we need a pipe pair to wait and listen thread exit event - local rpipe, wpipe = pipe.openpair("BA") -- rpipe (block) + local rpipe, wpipe = pipe.openpair("AA") self._RPIPE = rpipe callinfo.wpipe = libc.dataptr(wpipe:cdata(), {ffi = false}) -- we need to suppress gc to free it, because it has been transfer to thread in another lua state instance @@ -198,7 +198,14 @@ function _thread:wait(timeout) local ok, errors local rpipe = self._RPIPE if rpipe and scheduler:co_running() then - ok, errors = rpipe:wait(pipe.EV_READ, timeout) + local buff = bytes(16) + local read, data_or_errors = rpipe:read(buff, 1, {block = true, timeout = timeout}) + if read > 0 then + ok = 1 + else + ok = read + errors = data_or_errors + end else ok, errors = thread.thread_wait(self:cdata(), timeout) end @@ -206,6 +213,11 @@ function _thread:wait(timeout) return -1, errors or string.format("%s: failed to resume thread!", self) end + if rpipe then + rpipe:close() + self._RPIPE = nil + end + if ok > 0 then self._STATUS = thread.STATUS_DEAD end @@ -232,6 +244,10 @@ function _thread:__gc() if self:cdata() and self:is_dead() and thread.thread_exit(self:cdata()) then self._HANLDE = nil end + if self._RPIPE then + self._RPIPE:close() + self._RPIPE = nil + end end -- new an mutex @@ -862,10 +878,11 @@ function thread._run_thread(callback_str, callinfo_str) -- thread is finished, we need to notify the waited thread if wpipe then - local ok, errors = wpipe:write("exited") - if ok == nil then + local ok, errors = wpipe:write("1", {block = true}) + if ok < 0 then return false, errors end + wpipe:close() end return ok, errors end |
