This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.lua
More file actions
69 lines (58 loc) · 1.68 KB
/
root.lua
File metadata and controls
69 lines (58 loc) · 1.68 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
67
68
69
-- Submodule for emulating the control of a script as a process.
module('concurrent._root', package.seeall)
concurrent._process.processes[0] = 0 -- Root process has PID of 0.
concurrent._message.mailboxes[0] = {} -- Root process mailbox.
-- The existing versions of these functions are renamed before replacing them.
_self = concurrent.self
_isalive = concurrent.isalive
_wait_yield = concurrent._scheduler.wait_yield
_sleep_yield = concurrent._scheduler.sleep_yield
-- Returns 0 if the process is not a coroutine.
function self()
return _self() or 0
end
-- The root process is always alive.
function isalive(pid)
if pid ~= 0 then
return _isalive(pid)
end
return true
end
-- Special care must be taken if the root process is blocked.
function wait_yield()
local s = self()
if s ~= 0 then
return _wait_yield()
end
while true do
if concurrent._scheduler.barriers[s] then
break
end
concurrent.step()
concurrent.tick()
end
end
-- Special care must be taken if the root process is sleeping.
function sleep_yield()
local timeouts = concurrent._scheduler.timeouts
local mailboxes = concurrent._message.mailboxes
local s = self()
if s ~= 0 then
return _sleep_yield()
end
while true do
if #mailboxes[s] > 0 then
break
end
if timeouts[s] and cltime.time() - timeouts[s] >= 0 then
timeouts[s] = nil
return
end
concurrent.step()
concurrent.tick()
end
end
concurrent.self = self
concurrent.isalive = isalive
concurrent._scheduler.wait_yield = wait_yield
concurrent._scheduler.sleep_yield = sleep_yield