tobogganSe connecter

louise

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

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