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 pathmonitor.lua
More file actions
75 lines (67 loc) · 1.99 KB
/
monitor.lua
File metadata and controls
75 lines (67 loc) · 1.99 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
70
71
72
73
74
75
-- Submodule for process monitoring.
module('concurrent._monitor', package.seeall)
monitors = {} -- Active monitors between processes.
-- The calling process starts monitoring the specified process.
function monitor(dest)
local s = concurrent.self()
local pid = concurrent.whereis(dest)
if not pid then
return
end
if type(monitors[pid]) == 'nil' then
monitors[pid] = {}
end
for _, v in pairs(monitors[pid]) do
if s == v then
return
end
end
table.insert(monitors[pid], s)
end
-- Creates a new process which is also monitored by the calling process.
function spawnmonitor(...)
local pid, errmsg = concurrent.spawn(...)
if not pid then
return nil, errmsg
end
concurrent.monitor(pid)
return pid
end
-- The calling process stops monitoring the specified process.
function demonitor(dest)
local s = concurrent.self()
local pid = concurrent.whereis(dest)
if not pid then
return
end
if monitors[pid] == 'nil' then
return
end
for key, value in pairs(monitors[pid]) do
if s == value then
monitors[pid][key] = nil
return
end
end
end
-- Notifies all the monitoring processes about the status change of the
-- specified process.
function notify_all(dead, reason)
if type(monitors[dead]) == 'nil' then
return
end
for _, v in pairs(monitors[dead]) do
notify(v, dead, reason)
end
monitors[dead] = nil
end
-- Notifies a single process about the status change of the specified process.
function notify(dest, dead, reason)
concurrent.send(dest, { signal = 'DOWN', from = dead, reason = reason })
end
-- Processes that monitor terminated or aborted processes should be notified.
table.insert(concurrent._process.ondeath, notify_all)
table.insert(concurrent._process.ondestruction, notify_all)
concurrent.monitor = monitor
concurrent.demonitor = demonitor
concurrent.spawnmonitor = spawnmonitor