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.

226 lignes · 7924 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/list
6import std/random.{type random}
7import std/time.{type clock}
8
9/// Identifies an actor.
10@builtin
11pub 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.
19pub 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.
29pub 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.
35pub 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
42pub 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.
52pub type Down { Down(pid: Pid, reason: ExitReason) }
53
54/// Delivered to a linked actor that traps exits.
55pub type ExitSignal { ExitSignal(pid: Pid, reason: ExitReason) }
56
57pub 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`. For a local sender, a send in progress at its exit
69 /// delivers before its exit's local `Down`; a send started after its kill
70 /// drops the message instead.
71 fn send :: to: Subject<m> message: m -> () where Send<m>
72 /// The oldest message on `from`, waiting up to `timeout` milliseconds
73 /// (`0`: don't wait, negative: forever).
74 fn receive :: from: Subject<m> timeout: Int -> Option<m>
75 /// Link with `pid`: when either stops abnormally, the other stops too
76 /// (or, if it traps exits, receives an `ExitSignal`).
77 fn link :: pid: Pid -> ()
78 /// Receive a `Down` message on the returned subject when `pid` stops.
79 fn monitor :: pid: Pid -> Subject<Down>
80 /// Stop `pid` with reason `Killed`.
81 fn kill :: pid: Pid -> ()
82 fn trap_exits :: on: Bool -> ()
83 /// Where trapped exit signals arrive.
84 fn exits :: -> Subject<ExitSignal>
85 fn sleep :: milliseconds: Int -> ()
86 /// The oldest message on any of these subject tags, with its tag (see
87 /// `select`, which checks the subjects are the caller's).
88 fn receive_any :: tags: List<Int> timeout: Int -> Option<(Int, Dynamic)>
89 /// Start an actor linked to the current one. Spawning and linking are
90 /// one step, so even a child that stops at once is linked.
91 fn spawn_link :: body: send fn() -> () / <proc, clock, random, io, ffi> -> Pid
92 /// Start an actor monitored by the current one: its `Down` message
93 /// arrives on the returned subject. Spawning and monitoring are one
94 /// step, so the reason is the real one even if the actor stops at once.
95 fn spawn_monitor ::
96 body: send fn() -> () / <proc, clock, random, io, ffi>
97 -> (Pid, Subject<Down>)
98 /// Ask `pid` to stop (ADR 0060): if it traps exits, it gets
99 /// `ExitSignal(self(), Shutdown)`, to clean up and return; otherwise it
100 /// stops at once with reason `Shutdown`, which its links and monitors
101 /// see. Unknown or exited pids are ignored.
102 fn stop :: pid: Pid -> ()
103 /// Stop monitoring `pid`: `down` is the subject `monitor pid` (or
104 /// `spawn_monitor`) returned. From now on no `Down` arrives on it; one
105 /// that already arrived stays (`demonitor_flush` drops it, too). Any
106 /// other pid or subject is ignored; `down` must be the caller's.
107 fn demonitor :: pid: Pid down: Subject<Down> -> ()
108 /// Remove the link with `pid`: once it returns, the link has no effect
109 /// on the caller (until a new `link`), and the caller's exit none on
110 /// `pid`. An `ExitSignal` that already arrived stays. No link, or a pid
111 /// that is gone: nothing.
112 fn unlink :: pid: Pid -> ()
113}
114
115/// `demonitor`, then drop the `Down` that may already have arrived on
116/// `down` (Erlang's `demonitor(ref, [flush])`): afterwards, `down` holds no
117/// message.
118pub fn demonitor_flush :: pid: Pid down: Subject<Down> -> () / <proc>
119{
120 demonitor pid down
121 let _ = receive down 0
122}
123
124/// A message whose type is known only to the subject it was sent on.
125/// Programs never see one: `select` turns it back into its type.
126@builtin
127pub type Dynamic
128
129/// Receives from several subjects at once, mapping each one's messages to
130/// one type (as in Gleam OTP):
131///
132/// let selector = process.new_selector()
133/// |> process.selecting(replies, Number(_))
134/// |> process.selecting(process.exits(), Exited(_))
135/// match process.select(selector, 1000) { ... }
136///
137/// Opaque: an entry pairs a subject's tag with a map that reads its
138/// messages as that subject's type, and only `selecting` may pair them.
139/// Rebuilt by a program, an entry could read one subject's message as
140/// another's type.
141pub opaque type Selector<m>
142{ Selector(entries: List<(Pid, Int, fn(Dynamic) -> m)>) }
143
144pub fn new_selector :: -> Selector<m>
145{
146 Selector([])
147}
148
149/// Also receive from `from`, mapping its messages with `map`.
150pub fn selecting ::
151 selector: Selector<m>
152 from: Subject<a>
153 map: fn(a) -> m
154 -> Selector<m>
155{
156 // Sound: every message under `from`'s tag was sent as an `a`.
157 Selector( [ (owner from, tag from, fn message { map (coerce message) })
158 , ..selector.entries
159 ]
160 )
161}
162
163/// The oldest message on any of the selector's subjects, mapped, waiting
164/// up to `timeout` milliseconds (`0`: don't wait, negative: forever).
165pub fn select :: selector: Selector<m> timeout: Int -> Option<m> / <proc>
166{
167 let me = self ()
168 let entries = selector.entries
169 if list.any
170 entries
171 fn e
172 {
173 let (owner, _, _) = e
174 !pid_eq owner me
175 }
176 {
177 panic "select on a subject owned by another actor"
178 }
179 else
180 {
181 match receive_any (list.map entries fn e { e.1 }) timeout
182 { Some((tag, message)) -> Some(dispatch entries tag message), None -> None }
183 }
184}
185
186/// `select` waiting as long as it takes.
187pub fn select_forever :: selector: Selector<m> -> m / <proc>
188{
189 match select selector (-1)
190 { Some(message) -> message, None -> panic "select_forever: no message" }
191}
192
193fn dispatch ::
194 entries: List<(Pid, Int, fn(Dynamic) -> m)>
195 tag: Int
196 message: Dynamic
197 -> m
198{
199 match entries
200 { [ (_, t, map)
201 , ..rest
202 ] -> if t == tag { map message } else { dispatch rest tag message }
203 , [] -> panic "select: a message for no subject"
204 }
205}
206
207@extern c "tl_coerce"
208fn coerce :: x: a -> b
209
210@extern c "tl_pid_eq"
211fn pid_eq :: a: Pid b: Pid -> Bool
212
213impl Eq<Pid>
214{
215 fn eq :: a b
216 {
217 pid_eq a b
218 }
219}
220
221/// Wait for the next message on `from`, however long it takes.
222pub fn receive_forever :: from: Subject<m> -> m / <proc>
223{
224 match receive from (-1)
225 { Some(message) -> message, None -> panic "receive_forever: no message" }
226}