
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1197 · main · 10 min 35 s
1Étoiles
0Forks
0Issues
| 1 | /// Actors: lightweight processes that share nothing and talk by messages |
| 2 | /// (ADR 0004). The `proc` effect is handled by the runtime around `main` |
| 3 | /// and around every actor, so tests can also handle it themselves. |
| 4 | |
| 5 | import std/list |
| 6 | import std/random.{type random} |
| 7 | import std/time.{type clock} |
| 8 | |
| 9 | /// Identifies an actor. |
| 10 | @builtin |
| 11 | pub type Pid |
| 12 | |
| 13 | /// Where messages of type `msg` are sent: an actor plus a tag that keeps |
| 14 | /// different message types apart. Only the owner can receive from it. |
| 15 | /// |
| 16 | /// Opaque (ADR 0072): only `subject` makes one, so a subject's tag always |
| 17 | /// carries messages of its type, which `select` relies on, and which |
| 18 | /// distribution relies on across hosts. `owner` and `tag` read it. |
| 19 | pub opaque type Subject<msg> |
| 20 | { Subject(owner: Pid, tag: Int) |
| 21 | /// Owned by an actor on another node: only the runtime makes one, when |
| 22 | /// a subject comes off the wire. `wire` is its message type's index in |
| 23 | /// the node's type registry. The runtime reads both constructors |
| 24 | /// (`process.rs`), so their order and fields are fixed. |
| 25 | , Remote(owner: Pid, tag: Int, wire: Int) |
| 26 | } |
| 27 | |
| 28 | /// The actor that owns `subject`: the only one that can receive from it. |
| 29 | pub fn owner :: subject: Subject<msg> -> Pid |
| 30 | { |
| 31 | match subject { Subject(owner, _) -> owner, Remote(owner, _, _) -> owner } |
| 32 | } |
| 33 | |
| 34 | /// The tag that tells `subject`'s messages apart in its owner's mailbox. |
| 35 | pub fn tag :: subject: Subject<msg> -> Int |
| 36 | { |
| 37 | match subject { Subject(_, tag) -> tag, Remote(_, tag, _) -> tag } |
| 38 | } |
| 39 | |
| 40 | /// Why an actor stopped. |
| 41 | @derive Eq Show |
| 42 | pub type ExitReason |
| 43 | { Normal |
| 44 | , Killed |
| 45 | , Crashed(String) |
| 46 | /// Asked to stop (`stop`), not crashed: a supervisor's orderly shutdown |
| 47 | /// (ADR 0060). |
| 48 | , Shutdown |
| 49 | } |
| 50 | |
| 51 | /// Sent to a monitoring actor when the monitored one stops. |
| 52 | pub type Down { Down(pid: Pid, reason: ExitReason) } |
| 53 | |
| 54 | /// Delivered to a linked actor that traps exits. |
| 55 | pub type ExitSignal { ExitSignal(pid: Pid, reason: ExitReason) } |
| 56 | |
| 57 | pub effect proc |
| 58 | { |
| 59 | /// Start an actor running `body`. It gets the runtime's handlers of |
| 60 | /// `proc`, `clock` and `random`, not the caller's (ADR 0036). |
| 61 | fn spawn :: body: send fn() -> () / <proc, clock, random, io, ffi> -> Pid |
| 62 | /// The current actor. |
| 63 | fn self :: -> Pid |
| 64 | /// A new subject owned by the current actor. |
| 65 | fn subject :: -> Subject<m> |
| 66 | /// Put `message` in the mailbox of `to`'s owner. It must be sendable |
| 67 | /// (ADR 0004, Revision 6): no `Ref`, no resource that is not |
| 68 | /// `@thread_safe`. |
| 69 | fn send :: to: Subject<m> message: m -> () where Send<m> |
| 70 | /// The oldest message on `from`, waiting up to `timeout` milliseconds |
| 71 | /// (`0`: don't wait, negative: forever). |
| 72 | fn receive :: from: Subject<m> timeout: Int -> Option<m> |
| 73 | /// Link with `pid`: when either stops abnormally, the other stops too |
| 74 | /// (or, if it traps exits, receives an `ExitSignal`). |
| 75 | fn link :: pid: Pid -> () |
| 76 | /// Receive a `Down` message on the returned subject when `pid` stops. |
| 77 | fn monitor :: pid: Pid -> Subject<Down> |
| 78 | /// Stop `pid` with reason `Killed`. |
| 79 | fn kill :: pid: Pid -> () |
| 80 | fn trap_exits :: on: Bool -> () |
| 81 | /// Where trapped exit signals arrive. |
| 82 | fn exits :: -> Subject<ExitSignal> |
| 83 | fn sleep :: milliseconds: Int -> () |
| 84 | /// The oldest message on any of these subject tags, with its tag (see |
| 85 | /// `select`, which checks the subjects are the caller's). |
| 86 | fn receive_any :: tags: List<Int> timeout: Int -> Option<(Int, Dynamic)> |
| 87 | /// Start an actor linked to the current one. Spawning and linking are |
| 88 | /// one step, so even a child that stops at once is linked. |
| 89 | fn spawn_link :: body: send fn() -> () / <proc, clock, random, io, ffi> -> Pid |
| 90 | /// Start an actor monitored by the current one: its `Down` message |
| 91 | /// arrives on the returned subject. Spawning and monitoring are one |
| 92 | /// step, so the reason is the real one even if the actor stops at once. |
| 93 | fn spawn_monitor :: |
| 94 | body: send fn() -> () / <proc, clock, random, io, ffi> |
| 95 | -> (Pid, Subject<Down>) |
| 96 | /// Ask `pid` to stop (ADR 0060): if it traps exits, it gets |
| 97 | /// `ExitSignal(self(), Shutdown)`, to clean up and return; otherwise it |
| 98 | /// stops at once with reason `Shutdown`, which its links and monitors |
| 99 | /// see. Unknown or exited pids are ignored. |
| 100 | fn stop :: pid: Pid -> () |
| 101 | /// Stop monitoring `pid`: `down` is the subject `monitor pid` (or |
| 102 | /// `spawn_monitor`) returned. From now on no `Down` arrives on it; one |
| 103 | /// that already arrived stays (`demonitor_flush` drops it, too). Any |
| 104 | /// other pid or subject is ignored; `down` must be the caller's. |
| 105 | fn demonitor :: pid: Pid down: Subject<Down> -> () |
| 106 | /// Remove the link with `pid`: once it returns, the link has no effect |
| 107 | /// on the caller (until a new `link`), and the caller's exit none on |
| 108 | /// `pid`. An `ExitSignal` that already arrived stays. No link, or a pid |
| 109 | /// that is gone: nothing. |
| 110 | fn unlink :: pid: Pid -> () |
| 111 | } |
| 112 | |
| 113 | /// `demonitor`, then drop the `Down` that may already have arrived on |
| 114 | /// `down` (Erlang's `demonitor(ref, [flush])`): afterwards, `down` holds no |
| 115 | /// message. |
| 116 | pub fn demonitor_flush :: pid: Pid down: Subject<Down> -> () / <proc> |
| 117 | { |
| 118 | demonitor pid down |
| 119 | let _ = receive down 0 |
| 120 | } |
| 121 | |
| 122 | /// A message whose type is known only to the subject it was sent on. |
| 123 | /// Programs never see one: `select` turns it back into its type. |
| 124 | @builtin |
| 125 | pub type Dynamic |
| 126 | |
| 127 | /// Receives from several subjects at once, mapping each one's messages to |
| 128 | /// one type (as in Gleam OTP): |
| 129 | /// |
| 130 | /// let selector = process.new_selector() |
| 131 | /// |> process.selecting(replies, Number(_)) |
| 132 | /// |> process.selecting(process.exits(), Exited(_)) |
| 133 | /// match process.select(selector, 1000) { ... } |
| 134 | /// |
| 135 | /// Opaque: an entry pairs a subject's tag with a map that reads its |
| 136 | /// messages as that subject's type, and only `selecting` may pair them. |
| 137 | /// Rebuilt by a program, an entry could read one subject's message as |
| 138 | /// another's type. |
| 139 | pub opaque type Selector<m> |
| 140 | { Selector(entries: List<(Pid, Int, fn(Dynamic) -> m)>) } |
| 141 | |
| 142 | pub fn new_selector :: -> Selector<m> |
| 143 | { |
| 144 | Selector([]) |
| 145 | } |
| 146 | |
| 147 | /// Also receive from `from`, mapping its messages with `map`. |
| 148 | pub fn selecting :: |
| 149 | selector: Selector<m> |
| 150 | from: Subject<a> |
| 151 | map: fn(a) -> m |
| 152 | -> Selector<m> |
| 153 | { |
| 154 | // Sound: every message under `from`'s tag was sent as an `a`. |
| 155 | Selector( [ (owner from, tag from, fn message { map (coerce message) }) |
| 156 | , ..selector.entries |
| 157 | ] |
| 158 | ) |
| 159 | } |
| 160 | |
| 161 | /// The oldest message on any of the selector's subjects, mapped, waiting |
| 162 | /// up to `timeout` milliseconds (`0`: don't wait, negative: forever). |
| 163 | pub fn select :: selector: Selector<m> timeout: Int -> Option<m> / <proc> |
| 164 | { |
| 165 | let me = self () |
| 166 | let entries = selector.entries |
| 167 | if list.any |
| 168 | entries |
| 169 | fn e |
| 170 | { |
| 171 | let (owner, _, _) = e |
| 172 | !pid_eq owner me |
| 173 | } |
| 174 | { |
| 175 | panic "select on a subject owned by another actor" |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | match receive_any (list.map entries fn e { e.1 }) timeout |
| 180 | { Some((tag, message)) -> Some(dispatch entries tag message), None -> None } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// `select` waiting as long as it takes. |
| 185 | pub fn select_forever :: selector: Selector<m> -> m / <proc> |
| 186 | { |
| 187 | match select selector (-1) |
| 188 | { Some(message) -> message, None -> panic "select_forever: no message" } |
| 189 | } |
| 190 | |
| 191 | fn dispatch :: |
| 192 | entries: List<(Pid, Int, fn(Dynamic) -> m)> |
| 193 | tag: Int |
| 194 | message: Dynamic |
| 195 | -> m |
| 196 | { |
| 197 | match entries |
| 198 | { [ (_, t, map) |
| 199 | , ..rest |
| 200 | ] -> if t == tag { map message } else { dispatch rest tag message } |
| 201 | , [] -> panic "select: a message for no subject" |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @extern c "tl_coerce" |
| 206 | fn coerce :: x: a -> b |
| 207 | |
| 208 | @extern c "tl_pid_eq" |
| 209 | fn pid_eq :: a: Pid b: Pid -> Bool |
| 210 | |
| 211 | impl Eq<Pid> |
| 212 | { |
| 213 | fn eq :: a b |
| 214 | { |
| 215 | pid_eq a b |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Wait for the next message on `from`, however long it takes. |
| 220 | pub fn receive_forever :: from: Subject<m> -> m / <proc> |
| 221 | { |
| 222 | match receive from (-1) |
| 223 | { Some(message) -> message, None -> panic "receive_forever: no message" } |
| 224 | } |