summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-02 22:42:35 +0800
committerruki <[email protected]>2025-11-07 15:01:56 +0800
commit0fec3c1ff95f37cdfa6f09dc8e5d7efd8128c133 (patch)
tree00a45d878658c9699288062386efc1a2a7b21aa7
parent0b91f564b00bf22d611a70c17088ba3a155b8a84 (diff)
add async_task
-rw-r--r--xmake/core/base/os.lua29
-rw-r--r--xmake/core/base/private/async_task.lua123
-rw-r--r--xmake/core/sandbox/modules/os.lua4
3 files changed, 152 insertions, 4 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 647c76c50..7b573169d 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -54,6 +54,16 @@ os.SYSERR_NOT_PERM = 1
os.SYSERR_NOT_FILEDIR = 2
os.SYSERR_NOT_ACCESS = 3
+-- get the async task
+function os._async_task()
+ local async_task = os._ASYNC_TASK
+ if async_task == nil then
+ async_task = require("base/private/async_task")
+ os._ASYNC_TASK = async_task
+ end
+ return async_task
+end
+
-- copy single file or directory
function os._cp(src, dst, rootdir, opt)
opt = opt or {}
@@ -511,6 +521,11 @@ function os.cp(srcpath, dstpath, opt)
return false, string.format("invalid arguments!")
end
+ -- do it in the asynchronous task
+ if opt and opt.async then
+ return os._async_task().cp(srcpath, dstpath, {detach = opt.detach})
+ end
+
-- reserve the source directory structure if opt.rootdir is given
local rootdir = opt and opt.rootdir
if rootdir then
@@ -564,14 +579,19 @@ end
-- remove files or directories
function os.rm(filepath, opt)
+ opt = opt or {}
-- check arguments
if not filepath then
return false, string.format("invalid arguments!")
end
+ -- do it in the asynchronous task
+ if opt.async then
+ return os._async_task().rm(filepath, {detach = opt.detach})
+ end
+
-- remove file or directories
- opt = opt or {}
filepath = tostring(filepath)
local filepathes = os._match_wildcard_pathes(filepath)
if type(filepathes) == "string" then
@@ -682,13 +702,18 @@ function os.mkdir(dir)
end
-- remove directories
-function os.rmdir(dir)
+function os.rmdir(dir, opt)
-- check arguments
if not dir then
return false, string.format("invalid arguments!")
end
+ -- do it in the asynchronous task
+ if opt and opt.async then
+ return os._async_task().rmdir(dir, {detach = opt.detach})
+ end
+
-- support path instance
dir = tostring(dir)
diff --git a/xmake/core/base/private/async_task.lua b/xmake/core/base/private/async_task.lua
new file mode 100644
index 000000000..c1f67ff27
--- /dev/null
+++ b/xmake/core/base/private/async_task.lua
@@ -0,0 +1,123 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file async_task.lua
+--
+
+-- define module: async_task
+local async_task = async_task or {}
+
+-- load modules
+local os = require("base/os")
+local thread = require("base/thread")
+
+-- the task status
+local is_stopped = false
+local is_started = false
+
+-- the task event and queue
+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
+ if event:wait(-1) > 0 then
+ while not queue:empty() do
+ print(queue:pop())
+ end
+ end
+ end
+ dprint("async_task: exited")
+end
+
+-- start the asynchronous task
+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")
+ end
+ os.atexit(function (errors)
+ if t then
+ dprint("async_task: wait for exiting ..")
+ is_stopped = true
+ task_event:post()
+ t:wait(-1)
+ end
+ end)
+ return true
+end
+
+-- ensure the asynchronous task is started
+function async_task._ensure_started()
+ if is_stopped then
+ return false, string.format("async_task has been stopped")
+ end
+ if not is_started then
+ local ok, errors = async_task._start()
+ if ok then
+ is_started = true
+ end
+ return ok, errors
+ end
+ assert(task_queue and task_event)
+ return true
+end
+
+-- copy files or directories
+function async_task.cp(srcpath, dstpath, opt)
+ local ok, errors = async_task._ensure_started()
+ if not ok then
+ return false, errors
+ end
+
+ task_queue:push({kind = "cp", srcpath = srcpath, dstpath = dstpath})
+ task_event:post()
+ return true
+end
+
+-- remove files or directories
+function async_task.rm(filepath, opt)
+ local ok, errors = async_task._ensure_started()
+ if not ok then
+ return false, errors
+ end
+
+ task_queue:push({kind = "rm", filepath = filepath})
+ task_event:post()
+ return true
+end
+
+-- remove directories
+function async_task.rmdir(dir, opt)
+ local ok, errors = async_task._ensure_started()
+ if not ok then
+ return false, errors
+ end
+
+ task_queue:push({kind = "rmdir", dir = dir})
+ task_event:post()
+ return true
+end
+
+-- return module: async_task
+return async_task
+
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 580e9e31d..39784d80b 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -225,9 +225,9 @@ function sandbox_os.mkdir(dir)
end
-- remove directories
-function sandbox_os.rmdir(dir)
+function sandbox_os.rmdir(dir, opt)
assert(dir)
- local ok, errors = os.rmdir(vformat(dir))
+ local ok, errors = os.rmdir(vformat(dir), opt)
if not ok then
os.raise(errors)
end