summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-02 23:33:04 +0800
committerruki <[email protected]>2025-11-07 15:01:56 +0800
commitec6e12c9f2fd8533d06e177ddc0a6dcf3e127757 (patch)
tree3051e972ea306e81c36ae2d1d07069b61f5cc080
parent0fec3c1ff95f37cdfa6f09dc8e5d7efd8128c133 (diff)
do async task in os.rm
-rw-r--r--xmake/core/base/private/async_task.lua34
-rw-r--r--xmake/core/base/utils.lua14
2 files changed, 37 insertions, 11 deletions
diff --git a/xmake/core/base/private/async_task.lua b/xmake/core/base/private/async_task.lua
index c1f67ff27..7b84cd979 100644
--- a/xmake/core/base/private/async_task.lua
+++ b/xmake/core/base/private/async_task.lua
@@ -22,7 +22,8 @@
local async_task = async_task or {}
-- load modules
-local os = require("base/os")
+local os = require("base/os")
+local utils = require("base/utils")
local thread = require("base/thread")
-- the task status
@@ -34,16 +35,18 @@ local task_event = nil
local task_queue = nil
-- the asynchronous task loop
-function async_task._loop(event, queue)
- dprint("async_task: started")
- while not is_stopped do
+function async_task._loop(event, queue, is_stopped)
+ print("started")
+ --utils.dprint("async_task: started")
+ while not is_stopped:get() do
if event:wait(-1) > 0 then
while not queue:empty() do
print(queue:pop())
end
end
end
- dprint("async_task: exited")
+ print("exit")
+ --utils.dprint("async_task: exited")
end
-- start the asynchronous task
@@ -51,16 +54,25 @@ function async_task._start()
assert(task_queue == nil and task_event == nil)
task_event = thread.event()
task_queue = thread.queue()
- local t = thread.start_named("core.base.async_task", async_task._loop, task_event, task_queue)
- if not t then
- return false, string.format("cannot start async_task")
+ local task_is_stopped = thread.sharedata()
+ local task_thread = thread.new(async_task._loop, {name = "core.base.async_task", argv = {task_event, task_queue, task_is_stopped}})
+ local ok, errors = task_thread:start()
+ if not ok then
+ return false, errors
end
os.atexit(function (errors)
- if t then
- dprint("async_task: wait for exiting ..")
+ if task_thread then
+ utils.dprint("async_task: wait for exiting ..")
is_stopped = true
+ -- Perhaps the thread hasn't started yet.
+ -- Let's wait a while and let it finish executing the tasks in the current queue.
+ if not task_queue:empty() then
+ task_event:post()
+ os.sleep(300)
+ end
+ task_is_stopped:set(true)
task_event:post()
- t:wait(-1)
+ task_thread:wait(-1)
end
end)
return true
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index d20b3c774..ac9884851 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -193,6 +193,20 @@ function utils.vprintf(format, ...)
end
end
+-- print the diagnosis information
+function utils.dprint(format, ...)
+ if option.get("diagnosis") and format ~= nil then
+ utils.print(format, ...)
+ end
+end
+
+-- print the diagnosis information without newline
+function utils.dprintf(format, ...)
+ if option.get("diagnosis") and format ~= nil then
+ utils.printf(format, ...)
+ end
+end
+
-- print the error information
function utils.error(format, ...)
if format ~= nil then