diff options
| author | ruki <[email protected]> | 2025-11-03 22:40:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-11-07 15:01:56 +0800 |
| commit | 85980580cf9fab96edda2325b59fb315fed4a7ce (patch) | |
| tree | c283b766e457a0b989eaf812ed1560015531c1a0 /xmake/core/base/private/async_task.lua | |
| parent | ec6e12c9f2fd8533d06e177ddc0a6dcf3e127757 (diff) | |
impl os.cp/rm in async task
Diffstat (limited to 'xmake/core/base/private/async_task.lua')
| -rw-r--r-- | xmake/core/base/private/async_task.lua | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/xmake/core/base/private/async_task.lua b/xmake/core/base/private/async_task.lua index 7b84cd979..1356072e5 100644 --- a/xmake/core/base/private/async_task.lua +++ b/xmake/core/base/private/async_task.lua @@ -25,6 +25,7 @@ local async_task = async_task or {} local os = require("base/os") local utils = require("base/utils") local thread = require("base/thread") +local option = require("base/option") -- the task status local is_stopped = false @@ -35,18 +36,48 @@ local task_event = nil local task_queue = nil -- the asynchronous task loop -function async_task._loop(event, queue, is_stopped) - print("started") - --utils.dprint("async_task: started") +function async_task._loop(event, queue, is_stopped, is_diagnosis) + local os = require("base/os") + + local function dprint(...) + if is_diagnosis then + print(...) + end + end + + local function _runcmd_cp(cmd) + os.cp(cmd.srcpath, cmd.dstpath) + end + local function _runcmd_rm(cmd) + os.rm(cmd.filepath) + end + local function _runcmd_rmdir(cmd) + os.rmdir(cmd.dir) + end + local runops = { + cp = _runcmd_cp, + rm = _runcmd_rm, + rmdir = _runcmd_rmdir + } + local function _runcmd(cmd) + local runop = runops[cmd.kind] + if runop then + runop(cmd) + end + end + + dprint("async_task: started") while not is_stopped:get() do if event:wait(-1) > 0 then while not queue:empty() do - print(queue:pop()) + local cmd = queue:pop() + if cmd then + _runcmd(cmd) + end end end end - print("exit") - --utils.dprint("async_task: exited") + dprint("async_task: exited") end -- start the asynchronous task @@ -55,7 +86,9 @@ function async_task._start() task_event = thread.event() task_queue = thread.queue() 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 task_thread = thread.new(async_task._loop, { + name = "core.base.async_task", internal = true, + argv = {task_event, task_queue, task_is_stopped, option.get("diagnosis")}}) local ok, errors = task_thread:start() if not ok then return false, errors @@ -96,11 +129,14 @@ end -- copy files or directories function async_task.cp(srcpath, dstpath, opt) + opt = opt or {} local ok, errors = async_task._ensure_started() if not ok then return false, errors end + srcpath = path.absolute(tostring(srcpath)) + dstpath = path.absolute(tostring(dstpath)) task_queue:push({kind = "cp", srcpath = srcpath, dstpath = dstpath}) task_event:post() return true @@ -108,11 +144,13 @@ end -- remove files or directories function async_task.rm(filepath, opt) + opt = opt or {} local ok, errors = async_task._ensure_started() if not ok then return false, errors end + filepath = path.absolute(tostring(filepath)) task_queue:push({kind = "rm", filepath = filepath}) task_event:post() return true @@ -120,11 +158,13 @@ end -- remove directories function async_task.rmdir(dir, opt) + opt = opt or {} local ok, errors = async_task._ensure_started() if not ok then return false, errors end + dir = path.absolute(tostring(dir)) task_queue:push({kind = "rmdir", dir = dir}) task_event:post() return true |
