Skip to content

Commit 8099a2d

Browse files
committed
fix thread wait
1 parent d2cdef8 commit 8099a2d

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

xmake/core/base/private/async_task.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ function async_task._loop(event, queue, mutex, is_stopped, is_diagnosis)
8181
local thread = require("base/thread")
8282

8383
local function dprint(...)
84-
-- TODO
85-
is_diagnosis = true
8684
if is_diagnosis then
8785
print(...)
8886
end
@@ -194,6 +192,7 @@ function async_task._start()
194192

195193
-- Perhaps the thread hasn't started yet.
196194
-- Let's wait a while and let it finish executing the tasks in the current queue.
195+
utils.dprint("async_task: wait the pending tasks(%d) for exiting ..", task_queue:size())
197196
task_mutex:lock()
198197
local is_empty = task_queue:empty()
199198
task_mutex:unlock()
@@ -204,7 +203,7 @@ function async_task._start()
204203
task_is_stopped:set(true)
205204
task_event:post()
206205

207-
utils.dprint("async_task: wait the pending tasks(%d) for exiting ..", task_queue:size())
206+
utils.dprint("async_task: wait thread for exiting ..")
208207
task_thread:wait(-1)
209208
utils.dprint("async_task: wait finished")
210209
end

xmake/core/base/thread.lua

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,18 @@ function _thread:start()
113113
end
114114

115115
-- init callback info
116+
local is_internal = self._INTERNAL
116117
local callback = string._dump(self._CALLBACK)
117-
local callinfo = {name = self:name(), argv = argv, internal = self._INTERNAL}
118+
local callinfo = {name = self:name(), argv = argv, internal = is_internal}
118119

119120
-- we need a pipe pair to wait and listen thread exit event
120-
local rpipe, wpipe = pipe.openpair("AA")
121-
self._RPIPE = rpipe
122-
callinfo.wpipe = libc.dataptr(wpipe:cdata(), {ffi = false})
123-
-- we need to suppress gc to free it, because it has been transfer to thread in another lua state instance
124-
wpipe._PIPE = nil
121+
if not is_internal then
122+
local rpipe, wpipe = pipe.openpair("AA")
123+
self._RPIPE = rpipe
124+
callinfo.wpipe = libc.dataptr(wpipe:cdata(), {ffi = false})
125+
-- we need to suppress gc to free it, because it has been transfer to thread in another lua state instance
126+
wpipe._PIPE = nil
127+
end
125128

126129
-- serialize and pass callback and arguments to this thread
127130
-- we do not use string.serialize to serialize callback, because it's slower (deserialize)
@@ -182,7 +185,7 @@ function _thread:wait(timeout)
182185

183186
local ok, errors
184187
local rpipe = self._RPIPE
185-
if rpipe then
188+
if rpipe and scheduler:co_running() then
186189
local buff = bytes(16)
187190
local read, data_or_errors = rpipe:read(buff, 1, {block = true, timeout = timeout})
188191
if read > 0 then
@@ -192,7 +195,7 @@ function _thread:wait(timeout)
192195
errors = data_or_errors
193196
end
194197
end
195-
if not scheduler:co_running() then
198+
if not rpipe then
196199
local waitok, wait_errors = thread.thread_wait(self:cdata(), timeout)
197200
if ok == nil or ok > 0 then
198201
ok = waitok
@@ -857,7 +860,9 @@ function thread._run_thread(callback_str, callinfo_str)
857860
argv = callinfo.argv
858861
threadname = callinfo.name
859862
is_internal = callinfo.internal
860-
wpipe = pipe.new(libc.ptraddr(callinfo.wpipe, {ffi = false}))
863+
if callinfo.wpipe then
864+
wpipe = pipe.new(libc.ptraddr(callinfo.wpipe, {ffi = false}))
865+
end
861866
end
862867
end
863868

0 commit comments

Comments
 (0)