forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_monitor.nula
More file actions
58 lines (52 loc) · 1.94 KB
/
Copy pathlink_monitor.nula
File metadata and controls
58 lines (52 loc) · 1.94 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
// Link, monitor, and the name registry — BEAM-style fault-tolerance
// primitives, driven from Nulang source via the built-in `Actor` effect.
//
// - `perform Actor.link(t)` links the current actor to `t`: abnormal exits
// propagate to linked peers and kill them, unless they trap exits.
// - `perform Actor.trap_exit(true)` makes the current actor convert
// linked-peer exit signals into system messages instead of dying.
// - `perform Actor.monitor(t)` delivers a DOWN system message to the
// current actor when `t` exits; `Actor.demonitor(t)` cancels it.
// - `perform Actor.register("name")` names the current actor, and
// `Actor.whereis("name")` resolves a name back to its actor ref
// (nil when the name is not registered).
// - `perform Actor.exit(reason)` terminates the current actor:
// 0/"normal", 1/"error", 2/"kill", anything else is a custom reason.
//
// System messages (link exit signals and monitor DOWNs) are delivered to
// the actor's first behavior as ordinary messages: exit signals carry
// [exited, self], DOWNs carry [target, watcher, reason].
//
// Run with: nulang examples/link_monitor.nula
actor Watcher {
state notices: Int = 0
state seen: Int = 0
// First behavior: receives the system messages. The victim's exit
// produces two of them here — one link exit signal and one DOWN.
behavior system(a, b, c) {
self.notices = self.notices + 1
}
behavior watch(victim) {
perform Actor.register("watcher")
perform Actor.trap_exit(true)
perform Actor.link(victim)
perform Actor.monitor(victim)
0
}
behavior check() {
// Resolves the name registered in `watch` back to this actor.
self.seen = perform Actor.whereis("watcher")
0
}
}
actor Victim {
behavior die() { perform Actor.exit(1) }
}
fn main() {
let w = spawn Watcher {}
let v = spawn Victim {}
w ! watch(v)
w ! check()
v ! die()
0
}