-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy paththread.lua
More file actions
66 lines (59 loc) · 1.63 KB
/
Copy paththread.lua
File metadata and controls
66 lines (59 loc) · 1.63 KB
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
local computer = require("computer")
computer.SingleThread = computer.pullSignal
local thread = {}
local mainThread
local timeouts
local function MultiThread( _timeout )
if coroutine.running()==mainThread then
local mintime = _timeout or math.huge
local co=next(timeouts)
while co do
if coroutine.status( co ) == "dead" then
timeouts[co],co=nil,next(timeouts,co)
else
if timeouts[co] < mintime then mintime=timeouts[co] end
co=next(timeouts,co)
end
end
if not next(timeouts) then
computer.pullSignal=computer.SingleThread
computer.pushSignal("AllThreadsDead")
end
local event={computer.SingleThread(mintime)}
local ok, param
for co in pairs(timeouts) do
ok, param = coroutine.resume( co, table.unpack(event) )
if not ok then timeouts={} error( param )
else timeouts[co] = param or math.huge end
end
return table.unpack(event)
else
return coroutine.yield( _timeout )
end
end
function thread.init()
mainThread=coroutine.running()
timeouts={}
end
function thread.create(f,...)
computer.pullSignal=MultiThread
local co=coroutine.create(f)
timeouts[co]=math.huge
local ok, param = coroutine.resume( co, ... )
if not ok then timeouts={} error( param )
else timeouts[co] = param or math.huge end
return co
end
function thread.kill(co)
timeouts[co]=nil
end
function thread.killAll()
timeouts={}
computer.pullSignal=computer.SingleThread
end
function thread.waitForAll()
repeat
until MultiThread()=="AllThreadsDead"
end
-------------------------------------------------------------------------------
return thread