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.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // `unlink`: once it returns, neither side's exit reaches the other |
| 2 | // through the link, until a new `link`; an exit signal already in the |
| 3 | // mailbox stays. Each exit is followed by a witness, linked to the |
| 4 | // exiting actor after the others: its exit signal comes after theirs. |
| 5 | |
| 6 | import std/io |
| 7 | import std/process. |
| 8 | { type ExitReason |
| 9 | , type Pid |
| 10 | , type Subject |
| 11 | , type proc |
| 12 | , Crashed |
| 13 | , ExitSignal |
| 14 | , Killed |
| 15 | , Normal |
| 16 | , Shutdown |
| 17 | } |
| 18 | |
| 19 | fn reason :: r: ExitReason -> String |
| 20 | { |
| 21 | match r |
| 22 | { Normal -> "normal" |
| 23 | , Killed -> "killed" |
| 24 | , Crashed(m) -> "crashed: " <> m |
| 25 | , Shutdown -> "shutdown" |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// An actor that waits until told, then crashes with `why`. |
| 30 | fn doomed :: why: String -> (Pid, Subject<()>) / <proc> |
| 31 | { |
| 32 | let ready = process.subject () |
| 33 | let pid = |
| 34 | { |
| 35 | use <- process.spawn |
| 36 | let go = process.subject () |
| 37 | process.send ready go |
| 38 | process.receive_forever go |
| 39 | panic why |
| 40 | } |
| 41 | (pid, process.receive_forever ready) |
| 42 | } |
| 43 | |
| 44 | /// Link `linked`, if any, and unlink it at once. |
| 45 | fn link_and_unlink :: linked: Option<Pid> -> () / <proc> |
| 46 | { |
| 47 | match linked |
| 48 | { Some(pid) -> |
| 49 | { |
| 50 | process.link pid |
| 51 | process.unlink pid |
| 52 | } |
| 53 | , None -> () |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// An actor that links and unlinks `linked`, then waits until told and |
| 58 | /// crashes with `why`. |
| 59 | fn unlinker :: linked: Option<Pid> why: String -> (Pid, Subject<()>) / <proc> |
| 60 | { |
| 61 | let ready = process.subject () |
| 62 | let pid = |
| 63 | { |
| 64 | use <- process.spawn |
| 65 | link_and_unlink linked |
| 66 | let go = process.subject () |
| 67 | process.send ready go |
| 68 | process.receive_forever go |
| 69 | panic why |
| 70 | } |
| 71 | (pid, process.receive_forever ready) |
| 72 | } |
| 73 | |
| 74 | /// An actor that does not trap exits, links and unlinks `linked`, then |
| 75 | /// answers every question on the returned subject. |
| 76 | fn bystander :: linked: Option<Pid> -> (Pid, Subject<Subject<String>>) / <proc> |
| 77 | { |
| 78 | let ready = process.subject () |
| 79 | let pid = |
| 80 | { |
| 81 | use <- process.spawn |
| 82 | link_and_unlink linked |
| 83 | let questions = process.subject () |
| 84 | process.send ready questions |
| 85 | answer questions |
| 86 | } |
| 87 | (pid, process.receive_forever ready) |
| 88 | } |
| 89 | |
| 90 | fn answer :: questions: Subject<Subject<String>> -> () / <proc> |
| 91 | { |
| 92 | let reply = process.receive_forever questions |
| 93 | process.send reply "alive" |
| 94 | answer questions |
| 95 | } |
| 96 | |
| 97 | /// Whether the actor of `questions` still answers. |
| 98 | fn alive :: questions: Subject<Subject<String>> -> String / <proc> |
| 99 | { |
| 100 | let reply = process.subject () |
| 101 | process.send questions reply |
| 102 | match process.receive reply 1000 { Some(s) -> s, None -> "gone" } |
| 103 | } |
| 104 | |
| 105 | /// An actor that traps exits, linked to `pid` once this returns: the |
| 106 | /// subject its first exit signal's reason goes to. |
| 107 | fn witness :: pid: Pid -> Subject<String> / <proc> |
| 108 | { |
| 109 | let told = process.subject () |
| 110 | let linked = process.subject () |
| 111 | let _ = |
| 112 | { |
| 113 | use <- process.spawn |
| 114 | process.trap_exits True |
| 115 | process.link pid |
| 116 | process.send linked () |
| 117 | let ExitSignal(_, why) = process.receive_forever (process.exits ()) |
| 118 | process.send told (reason why) |
| 119 | } |
| 120 | process.receive_forever linked |
| 121 | told |
| 122 | } |
| 123 | |
| 124 | /// Tell `pid` to crash, and wait until its witness is told. |
| 125 | fn crash :: pid: Pid go: Subject<()> -> () / <proc> |
| 126 | { |
| 127 | let seen = witness pid |
| 128 | process.send go () |
| 129 | let _ = process.receive_forever seen |
| 130 | } |
| 131 | |
| 132 | /// What waits on `exits`. |
| 133 | fn pending :: exits: Subject<process.ExitSignal> -> String / <proc> |
| 134 | { |
| 135 | match process.receive exits 0 |
| 136 | { Some(ExitSignal(_, why)) -> "an exit signal, " <> reason why |
| 137 | , None -> "nothing" |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn main :: -> () / <io, proc> |
| 142 | { |
| 143 | process.trap_exits True |
| 144 | let exits = process.exits () |
| 145 | |
| 146 | // The other's crash, after an unlink: no exit signal here. |
| 147 | let (pid, go) = doomed "boom" |
| 148 | process.link pid |
| 149 | process.unlink pid |
| 150 | crash pid go |
| 151 | io.println ("the other's crash: " <> pending exits) |
| 152 | |
| 153 | // A caller that does not trap exits lives on. |
| 154 | let (pid, go) = doomed "boom" |
| 155 | let (_, questions) = bystander (Some(pid)) |
| 156 | crash pid go |
| 157 | io.println ("the other's crash, not trapping: " <> alive questions) |
| 158 | |
| 159 | // The caller's crash does not reach the other. |
| 160 | let (other, questions) = bystander None |
| 161 | let (pid, go) = unlinker (Some(other)) "the caller crashed" |
| 162 | crash pid go |
| 163 | io.println ("the caller's crash: the other is " <> alive questions) |
| 164 | |
| 165 | // Linked again, the link works. |
| 166 | let (pid, go) = doomed "again" |
| 167 | process.link pid |
| 168 | process.unlink pid |
| 169 | process.link pid |
| 170 | crash pid go |
| 171 | let ExitSignal(from, why) = process.receive_forever exits |
| 172 | io.println ("linked again: " <> reason why <> " from it: {from == pid}") |
| 173 | |
| 174 | // No link, a dead pid, itself: nothing. |
| 175 | let (live, go) = unlinker None "later" |
| 176 | process.unlink live |
| 177 | process.unlink pid |
| 178 | process.unlink (process.self ()) |
| 179 | crash live go |
| 180 | io.println ("no link, a dead pid, itself: " <> pending exits) |
| 181 | |
| 182 | // An exit signal that arrived before the unlink stays. |
| 183 | let (pid, go) = doomed "early" |
| 184 | process.link pid |
| 185 | crash pid go |
| 186 | process.unlink pid |
| 187 | io.println ("arrived before: " <> pending exits) |
| 188 | } |
| 189 | //> the other's crash: nothing |
| 190 | //> the other's crash, not trapping: alive |
| 191 | //> the caller's crash: the other is alive |
| 192 | //> linked again: crashed: again from it: True |
| 193 | //> no link, a dead pid, itself: nothing |
| 194 | //> arrived before: an exit signal, crashed: early |