-
Notifications
You must be signed in to change notification settings - Fork 1
/
threads.lua
68 lines (49 loc) · 1.13 KB
/
threads.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
local threads = createClass();
function createThread(start)
local thread, env = createClass();
local startTime = 0;
local susTime = 0;
local corot = coroutine.create(start);
function thread.sustime(ns)
susTime = ns;
return true;
end
function thread.resume()
if (isrunning()) then return end;
startTime = getTickCount();
coroutine.resume(corot);
if (coroutine.status(corot) == "dead") then
destroy();
end
end
function thread.isrunning()
return (coroutine.running() == corot);
end
function thread.yield()
if not (isrunning()) then return end;
if (getTickCount() - startTime < susTime) then return end;
coroutine.yield();
end
function thread.destroy()
corot = nil;
end
setfenv(start, env);
thread.setParent(threads);
return thread;
end
function threads_pulse()
local m,n;
threads.reference();
for m,n in ipairs(threads.children) do
n.resume();
end
threads.dereference();
return true;
end
createThread(function()
while (true) do
collectgarbage("step");
yield();
end
end, 50, 0
).sustime(5);