tobogganSe connecter

louise

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

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