-
Notifications
You must be signed in to change notification settings - Fork 25
/
async.lua
199 lines (158 loc) · 4.16 KB
/
async.lua
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
local async = { ensure = {} }
local function threadfunc(...)
local definitionsChannel, requestChannel, returnChannel = ...
require "love.filesystem"
local functions = {}
local isolatemt = {__index = _G}
local function isolate(f)
local env = setmetatable({}, isolatemt)
setfenv(f, env)
return f
end
local keepRunning = true
local interface = {
define = function(name, contents)
functions[name] = loadstring(contents)
end,
call = function(id, name, ...)
if not functions[name] then
return print("Async thread received call to unknown function: '" .. name .. "'")
end
if not pcall(returnChannel.push, returnChannel, {id, pcall(isolate(functions[name]), ...)}) then
returnChannel:push{id, false, "[async] Tried to push unserializable data"}
end
end,
shutdown = function(amount)
keepRunning = false
if amount > 1 then
requestChannel:push{"shutdown", amount-1}
end
end,
}
local msg, command
while keepRunning do
msg = requestChannel:demand()
-- Update our definitions first
while true do
local defs = definitionsChannel:pop()
if not defs then break end
for i, v in pairs(defs) do
functions[i] = loadstring(v)
end
end
-- And only then handle the actual request
command = table.remove(msg, 1)
if interface[command] then
interface[command](unpack(msg))
else
print("Async thread received unknown command: '" .. command .. "'")
end
end
end
local threaddata = string.dump(threadfunc)
local threads, definitionsChannels,
requestChannel, returnChannel,
nextid, cbregistry,
functionregistry
function async.load(numthreads)
requestChannel = love.thread.newChannel()
returnChannel = love.thread.newChannel()
nextid = 1
cbregistry = {}
functionregistry = {}
threads = {}
definitionsChannels = {}
numthreads = numthreads or 1
for i = 1, numthreads do
async.addWorker()
end
end
function async.shutdown()
requestChannel:push{"shutdown", math.huge}
for i, v in ipairs(threads) do
v:wait()
end
threads, requestChannel, returnChannel, cbregistry = nil, nil, nil, nil
end
function async.addWorker()
local thread, definitionsChannel
local id = #threads+1
thread = love.thread.newThread(love.filesystem.newFileData(threaddata, "async.lua-thread"))
definitionsChannel = love.thread.newChannel()
threads[id] = thread
definitionsChannels[id] = definitionsChannel
definitionsChannel:push(functionregistry)
thread:start(definitionsChannel, requestChannel, returnChannel)
end
function async.stopWorkers(amount)
requestChannel:push{"shutdown", amount}
end
function async.define(name, func)
local contents = string.dump(func)
assert(contents, "Could not dump function, did you use upvalues?")
functionregistry[name] = contents
for i, v in ipairs(definitionsChannels) do
v:push{[name] = contents}
end
return function(cb, ...)
return async.call(cb, name, ...)
end
end
function async.call(callback, name, ...)
local id = nextid
nextid = id + 1
cbregistry[id] = callback
requestChannel:push{"call", id, name, ...}
end
function async.update()
-- Clean up any shut down threads
for i = #threads, 1, -1 do
if not threads[i]:isRunning() then
table.remove(threads, i):wait()
table.remove(definitionsChannels, i)
end
end
local result = returnChannel:pop()
while result do
local id = table.remove(result, 1)
local cb = cbregistry[id]
cbregistry[id] = nil
if not cb then
print("Async thread returned a value, but no callback was registered")
else
if type(cb) == "table" then
if table.remove(result, 1) then
cb.success(unpack(result))
else
cb.error(unpack(result))
end
elseif table.remove(result, 1) then
cb(unpack(result))
else
-- don't call the callback, do note
-- it is removed from the registry though
end
end
result = returnChannel:pop()
end
end
function async.getWorkerCount()
return #threads
end
function async.ensure.exactly(n)
async.ensure.atLeast(n).atMost(n)
return async.ensure
end
function async.ensure.atLeast(n)
for i = #threads+1, n do
async.addWorker()
end
return async.ensure
end
function async.ensure.atMost(n)
if n < #threads then
async.stopWorkers(n-#threads)
end
return async.ensure
end
return async