-
Notifications
You must be signed in to change notification settings - Fork 1
/
threadify.lua
152 lines (147 loc) · 5.97 KB
/
threadify.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
local log = require("log")("threadify")
local modname, is_thread = ...
if is_thread then
local api = require(modname)
local in_channel = love.thread.getChannel(modname .. "_cmd")
local out_channel = love.thread.getChannel(modname .. "_out")
local run = true
local running_coroutines = {}
while run do
local cmd
if #running_coroutines > 0 then
cmd = in_channel:demand(0.01)
else
cmd = in_channel:demand()
end
if cmd then
local call_id = cmd[1]
xpcall(function()
local fn = api[cmd[2]]
if api[cmd[2] .. "_co"] then
-- coroutine
local co = coroutine.create(fn)
local _, ret = coroutine.resume(co, unpack(cmd, 3))
if coroutine.status(co) == "dead" then
out_channel:push({ call_id, true, ret })
else
running_coroutines[#running_coroutines + 1] = { call_id, cmd[2], co }
end
else
-- normal function
out_channel:push({ call_id, true, fn(unpack(cmd, 3)) })
end
end, function(err)
out_channel:push({ call_id, false, "Failed to call '" .. modname .. "." .. cmd[2] .. "'", err })
end)
else
for i = #running_coroutines, 1, -1 do
local call_id, name, co = unpack(running_coroutines[i])
xpcall(function()
local _, ret = coroutine.resume(co)
if coroutine.status(co) == "dead" then
table.remove(running_coroutines, i)
out_channel:push({ call_id, true, ret })
end
end, function(err)
table.remove(running_coroutines, i)
out_channel:push({ call_id, false, "Failed to call '" .. modname .. "." .. name .. "'", err })
end)
end
end
end
else
local async = require("async")
local threads = {}
local thread_names = {}
local threadify = {}
local threads_channel = love.thread.getChannel("threads")
function threadify.require(require_string)
if not threads[require_string] then
local thread_table = {
resolvers = {},
rejecters = {},
}
local global_threads = threads_channel:peek()
if global_threads and global_threads[require_string] then
thread_table.thread = global_threads[require_string]
else
thread_table.thread = love.thread.newThread("threadify.lua")
end
if not thread_table.thread:isRunning() then
thread_table.thread:start(require_string, true)
end
threads[require_string] = thread_table
thread_names[#thread_names + 1] = require_string
threads_channel:performAtomic(function(channel, thread_object, module_name)
local all_threads = channel:pop() or {}
all_threads[module_name] = thread_object
channel:push(all_threads)
end, thread_table.thread, require_string)
end
local thread = threads[require_string]
local interface = {}
return setmetatable(interface, {
__index = function(_, key)
return function(...)
local msg = { -1, key, ... }
return async.promise:new(function(resolve, reject)
local cmd_channel = love.thread.getChannel(require_string .. "_cmd")
local request_id = 1
cmd_channel:performAtomic(function(channel)
local last_cmd = cmd_channel:peek()
if last_cmd then
request_id = last_cmd[1] + 1
end
while thread.resolvers[request_id] ~= nil do
request_id = request_id + 1
end
msg[1] = request_id
thread.resolvers[request_id] = resolve
thread.rejecters[request_id] = reject
channel:push(msg)
end)
end)
end
end,
})
end
function threadify.update()
for i = 1, #thread_names do
local require_string = thread_names[i]
local thread = threads[require_string]
local channel = love.thread.getChannel(require_string .. "_out")
local result
local check_result = channel:peek()
if check_result then
if thread.resolvers[check_result[1]] and thread.rejecters[check_result[1]] then
result = channel:pop()
end
end
if result then
if result[2] then
thread.resolvers[result[1]](unpack(result, 3))
else
log(result[3])
thread.rejecters[result[1]](result[4])
end
thread.resolvers[result[1]] = nil
thread.rejecters[result[1]] = nil
end
end
end
function threadify.stop()
for require_string, thread_table in pairs(threads) do
local thread = thread_table.thread
if thread:isRunning() then
-- effectively kills the thread (sending stop doesn't work sometimes and when it does it would still cause unresponsiveness on closing)
thread:release()
else
local err = thread:getError()
if err then
log("Error in '" .. require_string .. "' thread: " .. err)
end
end
end
end
return threadify
end