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
|
local timer = require 'timer'
local util = require 'utility'
---@class await
local m = {}
m.type = 'await'
m.coMap = setmetatable({}, { __mode = 'k' })
m.idMap = {}
m.delayQueue = {}
m.delayQueueIndex = 1
--- 设置错误处理器
---@param errHandle function {comment = '当有错误发生时,会以错误堆栈为参数调用该函数'}
function m.setErrorHandle(errHandle)
m.errorHandle = errHandle
end
function m.checkResult(co, ...)
local suc, err = ...
if not suc and m.errorHandle then
m.errorHandle(debug.traceback(co, err))
end
return ...
end
--- 创建一个任务
function m.call(callback, ...)
local co = coroutine.create(callback)
local closers = {}
m.coMap[co] = {
closers = closers,
priority = false,
}
for i = 1, select('#', ...) do
local id = select(i, ...)
if not id then
break
end
m.setID(id, co)
end
local currentCo = coroutine.running()
local current = m.coMap[currentCo]
if current then
for closer in pairs(current.closers) do
closers[closer] = true
closer(co)
end
end
return m.checkResult(co, coroutine.resume(co))
end
--- 创建一个任务,并挂起当前线程,当任务完成后再延续当前线程/若任务被关闭,则返回nil
function m.await(callback, ...)
if not coroutine.isyieldable() then
return callback(...)
end
return m.wait(function (waker, ...)
m.call(function ()
local returnNil <close> = util.defer(waker)
waker(callback())
end, ...)
end, ...)
end
--- 设置一个id,用于批量关闭任务
function m.setID(id, co)
co = co or coroutine.running()
if not m.idMap[id] then
m.idMap[id] = setmetatable({}, { __mode = 'k' })
end
m.idMap[id][co] = true
end
--- 根据id批量关闭任务
function m.close(id)
local map = m.idMap[id]
if not map then
return
end
local count = 0
for co in pairs(map) do
map[co] = nil
coroutine.close(co)
count = count + 1
end
log.debug('Close await:', count)
end
--- 休眠一段时间
---@param time number
function m.sleep(time)
if not coroutine.isyieldable() then
if m.errorHandle then
m.errorHandle(debug.traceback('Cannot yield'))
end
return
end
local co = coroutine.running()
timer.wait(time, function ()
return m.checkResult(co, coroutine.resume(co))
end)
return coroutine.yield()
end
--- 等待直到唤醒
---@param callback function
function m.wait(callback, ...)
if not coroutine.isyieldable() then
return
end
local co = coroutine.running()
local waked
callback(function (...)
if waked then
return
end
waked = true
if coroutine.status(co) == 'dead' then
return
end
return m.checkResult(co, coroutine.resume(co, ...))
end, ...)
return coroutine.yield()
end
--- 延迟
function m.delay()
if not coroutine.isyieldable() then
return
end
local co = coroutine.running()
local current = m.coMap[co]
-- TODO
if current.priority then
return
end
m.delayQueue[#m.delayQueue+1] = function ()
if coroutine.status(co) ~= 'suspended' then
return
end
return m.checkResult(co, coroutine.resume(co))
end
return coroutine.yield()
end
--- 步进
function m.step()
local waker = m.delayQueue[m.delayQueueIndex]
if waker then
m.delayQueue[m.delayQueueIndex] = false
m.delayQueueIndex = m.delayQueueIndex + 1
local clock = os.clock()
waker()
local passed = os.clock() - clock
if passed > 0.1 then
log.warn(('Await step takes [%.3f] sec.'):format(passed))
end
return true
else
m.delayQueue = {}
m.delayQueueIndex = 1
return false
end
end
function m.setPriority(n)
m.coMap[coroutine.running()].priority = true
end
return m
|