tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

83 lignes · 2415 octetsBrut · Historique
1// `demonitor`: no `Down` arrives after it returns; `demonitor_flush` also
2// drops one that arrived before.
3
4import std/io
5import std/process.{type Pid, type Subject, type proc, Down}
6
7/// An actor that waits until told to return.
8fn waiting :: -> (Pid, Subject<()>) / <proc>
9{
10 let ready = process.subject ()
11 let pid =
12 {
13 use <- process.spawn
14 let go = process.subject ()
15 process.send ready go
16 process.receive_forever go
17 }
18 (pid, process.receive_forever ready)
19}
20
21/// Whether a message waits on `down`.
22fn pending :: down: Subject<process.Down> -> String / <proc>
23{
24 match process.receive down 0 { Some(_) -> "a Down", None -> "nothing" }
25}
26
27pub fn main :: -> () / <io, proc>
28{
29 // Demonitored before the exit: no `Down`, the other monitor's comes.
30 let (pid, go) = waiting ()
31 let down = process.monitor pid
32 let other = process.monitor pid
33 process.demonitor pid down
34 process.send go ()
35 let Down(_, _) = process.receive_forever other
36 io.println ("demonitored: " <> pending down)
37
38 // After the exit: the `Down` already there stays, unless flushed.
39 let (pid, go) = waiting ()
40 let kept = process.monitor pid
41 let flushed = process.monitor pid
42 let other = process.monitor pid
43 process.send go ()
44 let Down(_, _) = process.receive_forever other
45 process.demonitor pid kept
46 process.demonitor_flush pid flushed
47 io.println
48 ("after the exit: " <> pending kept <> ", flushed: " <> pending flushed)
49
50 // `spawn_monitor`'s monitor too; twice, or another pid: nothing more.
51 let (pid, down) =
52 {
53 use <- process.spawn_monitor
54 process.sleep 10
55 }
56 process.demonitor pid down
57 process.demonitor pid down
58 process.demonitor (process.self ()) down
59 let other = process.monitor pid
60 let Down(_, _) = process.receive_forever other
61 io.println ("spawn_monitor: " <> pending down)
62
63 // A pid that is gone: `monitor` answers `noproc` at once, and it stays.
64 let down = process.monitor pid
65 process.demonitor pid down
66 io.println ("gone: " <> pending down)
67
68 // Only the subject's owner can demonitor.
69 let (pid, _) = waiting ()
70 let down = process.monitor pid
71 let _ =
72 {
73 use <- process.spawn
74 process.demonitor pid down
75 }
76 process.sleep 10
77 io.println ("from another actor: " <> pending down)
78}
79//> demonitored: nothing
80//> after the exit: a Down, flushed: nothing
81//> spawn_monitor: nothing
82//> gone: a Down
83//> from another actor: nothing