-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.lua
53 lines (42 loc) · 1.12 KB
/
thread.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
-- thread: wrapper around coroutines
k.log(k.loglevels.info, "base/thread")
do
local function handler(err)
return debug.traceback(err, 3)
end
local old_coroutine = coroutine
local _coroutine = {}
_G.coroutine = _coroutine
-- [[
k.hooks.add("sandbox", function()
k.userspace.coroutine = old_coroutine
end)
--]]
function _coroutine.create(func)
checkArg(1, func, "function")
return setmetatable({
__thread = old_coroutine.create(function()
return select(2, k.util.lassert(xpcall(func, handler)))
end)
}, {
__index = _coroutine,
__name = "thread"
})
end
function _coroutine.wrap(fnth)
checkArg(1, fnth, "function", "thread")
if type(fnth) == "function" then fnth = _coroutine.create(fnth) end
return function(...)
return select(2, fnth:resume(...))
end
end
function _coroutine:resume(...)
return old_coroutine.resume(self.__thread, ...)
end
function _coroutine:status()
return old_coroutine.status(self.__thread)
end
for k,v in pairs(old_coroutine) do
_coroutine[k] = _coroutine[k] or v
end
end