1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
--!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 utils = require("base/utils")
local thread = require("base/thread")
local option = require("base/option")
-- 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, 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
local cmd = queue:pop()
if cmd then
_runcmd(cmd)
end
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 task_is_stopped = thread.sharedata()
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
end
os.atexit(function (errors)
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()
task_thread: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)
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
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
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
end
-- return module: async_task
return async_task
|