-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
83 lines (71 loc) · 1.5 KB
/
test.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
require("yieldhook")
do
local sethook, time = debug.sethook, os.clock
coroutine.settimeout = function(cr, to, count)
if not count then
count = 1000000
end
if type(to) == 'number' then
to = to + time()
sethook(cr, function()
if time() > to then
sethook()
return true
end
end, 'y', count)
else
sethook(cr)
end
end
local resume, yield = coroutine.resume, coroutine.yield
local status = coroutine.status
coroutine.yield = function(...)
yield(true, ...)
end
coroutine.resume = function(cr, ...)
res = {resume(cr, ...)}
if status(cr) == 'dead' then
return table.unpack(res)
elseif res[2] then
return table.unpack(res, 2)
end
end
end
local realprint = print
function print(...)
realprint(os.date(), ...)
end
local function sleep(s)
local d = os.time() + s
while os.time() < d do end
end
local cr = {2, 3, 5, 7}
for i, t in ipairs(cr) do
cr[i] = coroutine.create(function()
local i, t = i, t
for j = 1, 4 do
print(i, j)
coroutine.yield(j)
sleep(t)
end
return 'done'
end)
end
local f = true
while f do
f = false
for i, t in ipairs(cr) do
if coroutine.status(t) ~= 'dead' then
coroutine.settimeout(t, 0.5)
local r, v = coroutine.resume(t)
if r == nil then
print(i, 'timed out')
elseif r then
print(i, 'yielded:', v)
else
print(i, 'crashed:', v)
end
f = true
end
end
end