summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-03 22:40:25 +0800
committerruki <[email protected]>2025-11-07 15:01:56 +0800
commit85980580cf9fab96edda2325b59fb315fed4a7ce (patch)
treec283b766e457a0b989eaf812ed1560015531c1a0
parentec6e12c9f2fd8533d06e177ddc0a6dcf3e127757 (diff)
impl os.cp/rm in async task
-rw-r--r--xmake/core/base/private/async_task.lua54
-rw-r--r--xmake/core/base/thread.lua10
-rw-r--r--xmake/core/sandbox/sandbox.lua5
3 files changed, 61 insertions, 8 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
diff --git a/xmake/core/base/thread.lua b/xmake/core/base/thread.lua
index 3856feb81..65e161050 100644
--- a/xmake/core/base/thread.lua
+++ b/xmake/core/base/thread.lua
@@ -52,6 +52,7 @@ function _thread.new(callback, opt)
instance._CALLBACK = callback
instance._STACKSIZE = opt.stacksize or 0
instance._STATUS = thread.STATUS_READY
+ instance._INTERNAL = opt.internal
setmetatable(instance, _thread)
return instance
end
@@ -129,7 +130,7 @@ function _thread:start()
-- init callback info
local callback = string._dump(self._CALLBACK)
- local callinfo = {name = self:name(), argv = argv}
+ local callinfo = {name = self:name(), argv = argv, internal = self._INTERNAL}
-- we need a pipe pair to wait and listen thread exit event
local rpipe, wpipe = pipe.openpair("AA")
@@ -808,6 +809,7 @@ function thread._run_thread(callback_str, callinfo_str)
local argv
local threadname
local wpipe
+ local is_internal = false
if callinfo_str then
local result, errors = string.deserialize(callinfo_str)
if not result then
@@ -817,6 +819,7 @@ function thread._run_thread(callback_str, callinfo_str)
if callinfo then
argv = callinfo.argv
threadname = callinfo.name
+ is_internal = callinfo.internal
wpipe = pipe.new(libc.ptraddr(callinfo.wpipe, {ffi = false}))
end
end
@@ -850,6 +853,11 @@ function thread._run_thread(callback_str, callinfo_str)
return false, errors
end
+ -- if it's an internal thread, we need to bind some additional internal interfaces.
+ if is_internal then
+ sandbox_inst:api_register_builtin("require", require)
+ end
+
-- save the running thread name
thread._RUNNING = threadname
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index c3fa92a2f..758a48030 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -245,6 +245,11 @@ function sandbox:namespace()
return self._PRIVATE._NAMESPACE
end
+-- register api for builtin
+function sandbox:api_register_builtin(name, func)
+ sandbox._api_register_builtin(self, name, func)
+end
+
-- get current instance in the sandbox modules
function sandbox.instance(script)