tobogganSe connecter

louise

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

825 lignes · 25622 octetsBrut · Historique
1//! The `std/process.proc` effect for compiled programs (ADR 0004).
2//!
3//! Every actor receives a **root capability** for `proc`, a capability
4//! block like any handler's (ADR 0010). Its clauses are runtime closures,
5//! one per operation in `std/process.lse`'s declaration order, then the
6//! marker. So generated code performs `proc` operations exactly like other
7//! `fn` operations. Codegen checks the order against the declaration.
8//!
9//! Each clause consumes its arguments, as all closures do. It gives the
10//! actor a fresh reduction budget, because each operation is a scheduling
11//! point in the interpreter. If the operation stopped the running actor
12//! (killing itself, or a linked actor dying), the clause starts unwinding.
13//!
14//! Data built here follows the constructor order of `std/process.lse` and
15//! the prelude:
16//! - `None`, `Some(x)`;
17//! - `Subject(owner, tag)`, `Remote(owner, tag, wire)`;
18//! - `Normal`, `Killed`, `Crashed(m)`;
19//! - `Down(pid, reason)`, `ExitSignal(pid, reason)`: one shape
20//! ([`signal_value`]).
21//!
22//! The `process` run test checks the same constructor order for the
23//! interpreter.
24//!
25//! An operation on a remote pid (negative: `dist`) goes to the transport
26//! through `dist`'s hooks; on a local one, it costs one sign test.
27
28use std::sync::OnceLock;
29
30use crate::abi::{Block, Ctx, UNIT, Value, YIELD_KILL, YIELD_NONE, int, int_get, is_ptr};
31use crate::heap::drop_value;
32use crate::int::saturating;
33use crate::otp::{Answer, Received, Tags, Timeout, Wait};
34use crate::sched::{Actor, Exit, REDUCTIONS, Shared, Tie, exit_actor, park, this};
35use crate::send::NotSendable;
36use crate::values::{tl_borrowed0, tl_borrowed1, tl_borrowed2};
37
38/// The operations of `std/process.proc`, in declaration order. The list is
39/// shared with codegen, which refuses a `std/process` that differs.
40pub use louise_rt_prims::{PROC_OPS, RootCap};
41
42/// A clause: `(ctx, this_, args...)`, `this_` being the clause itself.
43pub(crate) type Code =
44 unsafe extern "C" fn(*mut Ctx, Value, Value, Value, Value, Value, Value) -> Value;
45
46/// The clause of each operation, in the order of [`PROC_OPS`], with the
47/// borrowed entry of its arity (ADR 0068; an operation with three or more
48/// arguments needs a `tl_borrowed3` — values.rs).
49const CLAUSES: [(Code, Code); PROC_OPS.len()] = [
50 (op_spawn, tl_borrowed1),
51 (op_self, tl_borrowed0),
52 (op_subject, tl_borrowed0),
53 (op_send, tl_borrowed2),
54 (op_receive, tl_borrowed2),
55 (op_link, tl_borrowed1),
56 (op_monitor, tl_borrowed1),
57 (op_kill, tl_borrowed1),
58 (op_trap_exits, tl_borrowed1),
59 (op_exits, tl_borrowed0),
60 (op_sleep, tl_borrowed1),
61 (op_receive_any, tl_borrowed2),
62 (op_spawn_link, tl_borrowed1),
63 (op_spawn_monitor, tl_borrowed1),
64 (op_stop, tl_borrowed1),
65 (op_demonitor, tl_borrowed2),
66 (op_unlink, tl_borrowed1),
67];
68
69/// A root capability of `kind` (`TL_ROOT_*`, owned), for a program
70/// compiled for `ops` operations of its effect. A mismatch means the
71/// program and the runtime come from different versions of std: stop
72/// before calling the wrong clauses.
73///
74/// # Safety
75/// `ctx` must be the running actor's context.
76#[unsafe(no_mangle)]
77pub unsafe extern "C" fn tl_root_cap(_ctx: *mut Ctx, kind: i64, ops: i64) -> Value {
78 let Some(kind) = u8::try_from(kind).ok().and_then(RootCap::from_u8) else {
79 eprintln!(
80 "louise: this program asks for root capability {kind}, which the runtime doesn't know: rebuild the runtime (cargo build -p louise_rt) with the same Louise version"
81 );
82 std::process::abort();
83 };
84 if ops != kind.ops().len() as i64 {
85 eprintln!(
86 "louise: this program expects {ops} operations of a root effect ({}), but the runtime has {}: rebuild the runtime (cargo build -p louise_rt) with the same Louise version",
87 kind.c_name(),
88 kind.ops().len()
89 );
90 std::process::abort();
91 }
92 root_cap(kind)
93}
94
95/// A root capability of `kind` (owned): the immortal one of each kind
96/// (`clock.rs`, ADR 0036).
97pub(crate) fn root_cap(kind: RootCap) -> Value {
98 match kind {
99 RootCap::Proc => proc_cap(),
100 RootCap::Clock => crate::clock::clock_cap(),
101 RootCap::Random => crate::clock::random_cap(),
102 }
103}
104
105/// The immortal root capability for `proc`. Its clauses capture nothing:
106/// each reads the running actor from its `ctx`. So, like `clock`'s, one
107/// block serves every actor, and an actor's start and exit allocate and
108/// free none of its 17 closures (ADR 0036, Revision 1).
109fn proc_cap() -> Value {
110 static CAP: OnceLock<Value> = OnceLock::new();
111 *CAP.get_or_init(|| crate::clock::immortal_cap(&CLAUSES))
112}
113
114// ---- data ------------------------------------------------------------------
115
116/// A constructor of `tag` with `fields` (owned): a small int if nullary.
117/// Made outside any actor's context (`tl_alloc` does not read it).
118fn data(tag: u16, fields: &[Value]) -> Value {
119 if fields.is_empty() {
120 return int(i64::from(tag));
121 }
122 crate::values::cell(std::ptr::null_mut(), tag, fields)
123}
124
125fn some(v: Value) -> Value {
126 data(1, &[v])
127}
128
129fn none() -> Value {
130 int(0)
131}
132
133impl Answer for Value {
134 fn received(message: Value, tag: Option<i64>) -> Value {
135 match tag {
136 Some(tag) => some(data(0, &[int(tag), message])),
137 None => some(message),
138 }
139 }
140
141 fn timed_out() -> Value {
142 none()
143 }
144
145 fn unit() -> Value {
146 UNIT
147 }
148}
149
150fn subject(owner: i64, tag: i64) -> Value {
151 data(0, &[int(owner), int(tag)])
152}
153
154fn reason_value(r: &Exit) -> Value {
155 match r {
156 Exit::Normal => int(0),
157 Exit::Killed => int(1),
158 Exit::Shutdown => int(3),
159 Exit::Crashed(m) => {
160 // SAFETY: `m` is valid for its length.
161 let s = unsafe {
162 crate::values::tl_string_from(std::ptr::null_mut(), m.as_ptr().cast(), m.len())
163 };
164 data(2, &[s])
165 }
166 }
167}
168
169/// `(owner, tag)` of a subject (borrowed): the first two fields of both
170/// constructors.
171///
172/// # Safety
173/// `s` must be a live subject.
174pub(crate) unsafe fn subject_parts(s: Value) -> (i64, i64) {
175 // SAFETY: caller guarantees a subject block.
176 unsafe {
177 let f = Block::fields_ptr(s as *mut Block);
178 (int_get(*f), int_get(*f.add(1)))
179 }
180}
181
182/// The `wire` of a `Remote` subject (borrowed), or `None` for a local one.
183///
184/// # Safety
185/// `s` must be a live subject.
186pub(crate) unsafe fn subject_wire(s: Value) -> Option<i64> {
187 // SAFETY: caller guarantees a subject block; `Remote` has 3 fields.
188 unsafe {
189 let b = s as *mut Block;
190 ((*b).header.tag == 1).then(|| int_get(*Block::fields_ptr(b).add(2)))
191 }
192}
193
194// ---- delivery ----------------------------------------------------------------
195
196/// Prepare `v` (owned) for another actor ([`crate::send`]). If it cannot be
197/// sent, drop it and panic: `Err` holds what the operation returns.
198///
199/// # Safety
200/// `ctx` must be the running actor's context.
201unsafe fn handover(ctx: *mut Ctx, v: Value) -> Result<Value, Value> {
202 // SAFETY: forwarded caller guarantee.
203 unsafe {
204 crate::send::transfer(ctx, v).map_err(|(NotSendable(reason), v)| {
205 drop_value(ctx, v);
206 crate::effects::tl_panic_str(ctx, reason.as_ptr())
207 })
208 }
209}
210
211/// Put `message` (owned) in `owner`'s inbox under `tag`, waking `owner`
212/// if it waits for a message. Returns it if `owner` is gone.
213fn deliver(sched: &Shared, owner: i64, tag: i64, message: Value) -> Option<Value> {
214 sched.deliver(owner, tag, message)
215}
216
217/// Deliver a message the runtime made (outside any actor's context): if
218/// the receiver is gone meanwhile, free it here.
219pub(crate) fn deliver_made(sched: &Shared, owner: i64, tag: i64, message: Value) {
220 if let Some(undelivered) = deliver(sched, owner, tag, message) {
221 // SAFETY: the message was made here and never delivered.
222 unsafe { crate::heap::drop_detached(undelivered) };
223 }
224}
225
226/// `Down(pid, reason)`, or `ExitSignal(pid, reason)`: both are
227/// constructor 0 with these fields. Made outside any actor's context.
228pub(crate) fn signal_value(pid: i64, reason: &Exit) -> Value {
229 data(0, &[int(pid), reason_value(reason)])
230}
231
232pub(crate) fn deliver_down(sched: &Shared, watcher: i64, tag: i64, pid: i64, reason: &Exit) {
233 if !sched.exists(watcher) {
234 return;
235 }
236 deliver_made(sched, watcher, tag, signal_value(pid, reason));
237}
238
239pub(crate) fn deliver_exit_signal(sched: &Shared, to: i64, pid: i64, reason: &Exit) {
240 let Some(tag) = exits_tag(sched, to) else {
241 return;
242 };
243 deliver_made(sched, to, tag, signal_value(pid, reason));
244}
245
246/// The tag of `pid`'s exit signals (made on first use), if it is alive.
247fn exits_tag(sched: &Shared, pid: i64) -> Option<i64> {
248 sched.with_alive(pid, |a| {
249 *a.ties().exits_tag.get_or_insert_with(|| sched.tag())
250 })
251}
252
253// ---- clauses -------------------------------------------------------------------
254
255/// Common end of every clause: a fresh budget; unwind if the operation
256/// stopped the running actor.
257///
258/// # Safety
259/// `ctx` must be the running actor's context.
260unsafe fn done(ctx: *mut Ctx, result: Value) -> Value {
261 // SAFETY: forwarded caller guarantee.
262 unsafe {
263 (*ctx).reductions = REDUCTIONS;
264 let (a, _) = this(ctx);
265 if a.kill_requested() && (*ctx).yielding == YIELD_NONE {
266 (*ctx).yielding = YIELD_KILL;
267 drop_value(ctx, result);
268 return UNIT;
269 }
270 result
271 }
272}
273
274unsafe extern "C" fn op_spawn(
275 ctx: *mut Ctx,
276 this_: Value,
277 body: Value,
278 _: Value,
279 _: Value,
280 _: Value,
281 _: Value,
282) -> Value {
283 // SAFETY: called as a closure by generated code on the running actor.
284 unsafe {
285 drop_value(ctx, this_);
286 let body = match handover(ctx, body) {
287 Ok(body) => body,
288 Err(panicked) => return panicked,
289 };
290 let (pid, _) = crate::sched::spawn_program(ctx, body, Tie::None);
291 done(ctx, int(pid))
292 }
293}
294
295unsafe extern "C" fn op_spawn_link(
296 ctx: *mut Ctx,
297 this_: Value,
298 body: Value,
299 _: Value,
300 _: Value,
301 _: Value,
302 _: Value,
303) -> Value {
304 // SAFETY: as above.
305 unsafe {
306 drop_value(ctx, this_);
307 let body = match handover(ctx, body) {
308 Ok(body) => body,
309 Err(panicked) => return panicked,
310 };
311 let (pid, _) = crate::sched::spawn_program(ctx, body, Tie::Link);
312 done(ctx, int(pid))
313 }
314}
315
316unsafe extern "C" fn op_spawn_monitor(
317 ctx: *mut Ctx,
318 this_: Value,
319 body: Value,
320 _: Value,
321 _: Value,
322 _: Value,
323 _: Value,
324) -> Value {
325 // SAFETY: as above.
326 unsafe {
327 drop_value(ctx, this_);
328 let body = match handover(ctx, body) {
329 Ok(body) => body,
330 Err(panicked) => return panicked,
331 };
332 let (pid, tag) = crate::sched::spawn_program(ctx, body, Tie::Monitor);
333 let (me, _) = this(ctx);
334 let down = subject(me.pid, tag.expect("monitored"));
335 done(ctx, data(0, &[int(pid), down]))
336 }
337}
338
339unsafe extern "C" fn op_self(
340 ctx: *mut Ctx,
341 this_: Value,
342 _: Value,
343 _: Value,
344 _: Value,
345 _: Value,
346 _: Value,
347) -> Value {
348 // SAFETY: as above.
349 unsafe {
350 drop_value(ctx, this_);
351 let (a, _) = this(ctx);
352 done(ctx, int(a.pid))
353 }
354}
355
356unsafe extern "C" fn op_subject(
357 ctx: *mut Ctx,
358 this_: Value,
359 _: Value,
360 _: Value,
361 _: Value,
362 _: Value,
363 _: Value,
364) -> Value {
365 // SAFETY: as above.
366 unsafe {
367 drop_value(ctx, this_);
368 let (a, sched) = this(ctx);
369 let tag = sched.tag();
370 done(ctx, subject(a.pid, tag))
371 }
372}
373
374unsafe extern "C" fn op_send(
375 ctx: *mut Ctx,
376 this_: Value,
377 to: Value,
378 message: Value,
379 _: Value,
380 _: Value,
381 _: Value,
382) -> Value {
383 // SAFETY: as above; `to` is a subject.
384 unsafe {
385 drop_value(ctx, this_);
386 let (_, sched) = this(ctx);
387 let (owner, tag) = subject_parts(to);
388 if crate::dist::is_remote(owner) {
389 return send_remote(ctx, to, owner, tag, message);
390 }
391 drop_value(ctx, to);
392 let message = match handover(ctx, message) {
393 Ok(message) => message,
394 Err(panicked) => return panicked,
395 };
396 if let Some(undelivered) = deliver(sched, owner, tag, message) {
397 drop_value(ctx, undelivered);
398 }
399 done(ctx, UNIT)
400 }
401}
402
403/// `send` to a subject on another node: the transport encodes `message`
404/// (borrowed) for `to`'s message type, parking the actor while the peer's
405/// queue is full (`dist::send_parking`). Out of line: local sends never
406/// carry it.
407///
408/// # Safety
409/// As `op_send`'s; `to` and `message` are owned.
410#[cold]
411#[inline(never)]
412unsafe fn send_remote(ctx: *mut Ctx, to: Value, owner: i64, tag: i64, message: Value) -> Value {
413 // SAFETY: forwarded caller guarantee.
414 unsafe {
415 // A `Remote` subject always (`dist`); a `Subject` with a remote
416 // owner has no message type to encode with, and the message is lost.
417 let sent = match subject_wire(to) {
418 Some(wire) => crate::dist::send(ctx, owner, tag, wire, message),
419 None => Ok(()),
420 };
421 drop_value(ctx, to);
422 drop_value(ctx, message);
423 match sent {
424 Ok(()) => done(ctx, UNIT),
425 // A message the program cannot send: as the send walk's
426 // backstop, a panic in the sender.
427 Err(why) => crate::effects::tl_panic_str(ctx, why.as_ptr()),
428 }
429 }
430}
431
432unsafe extern "C" fn op_receive(
433 ctx: *mut Ctx,
434 this_: Value,
435 from: Value,
436 timeout: Value,
437 _: Value,
438 _: Value,
439 _: Value,
440) -> Value {
441 // SAFETY: as above; `from` is a subject.
442 unsafe {
443 drop_value(ctx, this_);
444 let (a, sched) = this(ctx);
445 let (owner, tag) = subject_parts(from);
446 drop_value(ctx, from);
447 // A `Remote` subject's owner is never local (`dist`).
448 if owner != a.pid {
449 return crate::effects::tl_panic_str(
450 ctx,
451 c"receive on a subject owned by another actor".as_ptr(),
452 );
453 }
454 match take_or_wait(sched, a, Tags::One(tag), false, saturating(timeout)) {
455 Some(answer) => done(ctx, answer),
456 None => {
457 let answer = wait_for_message(ctx);
458 done(ctx, answer)
459 }
460 }
461 }
462}
463
464/// `receive_any(tags, timeout)`: the oldest message on any of the tags, as
465/// `(tag, message)` (the subjects' owners are checked by
466/// `std/process.select`).
467unsafe extern "C" fn op_receive_any(
468 ctx: *mut Ctx,
469 this_: Value,
470 tags: Value,
471 timeout: Value,
472 _: Value,
473 _: Value,
474 _: Value,
475) -> Value {
476 // SAFETY: as above; `tags` is a `List<Int>`.
477 unsafe {
478 drop_value(ctx, this_);
479 let mut wanted = Vec::new();
480 let mut cell = tags;
481 while is_ptr(cell) {
482 let f = Block::fields_ptr(cell as *mut Block);
483 wanted.push(int_get(*f));
484 cell = *f.add(1);
485 }
486 drop_value(ctx, tags);
487 let (a, sched) = this(ctx);
488 match take_or_wait(sched, a, Tags::Many(wanted), true, saturating(timeout)) {
489 Some(answer) => done(ctx, answer),
490 None => {
491 let answer = wait_for_message(ctx);
492 done(ctx, answer)
493 }
494 }
495 }
496}
497
498/// Under one lock: take the oldest message of `me` on one of `tags`, or
499/// answer `None` if `timeout` is 0, or else set `me` waiting for them
500/// (then the caller parks). `any`: the answer carries the tag.
501fn take_or_wait(sched: &Shared, me: &Actor, tags: Tags, any: bool, timeout: i64) -> Option<Value> {
502 let timeout = match timeout {
503 0 => Timeout::Zero,
504 t if t > 0 => Timeout::At(sched.clock().saturating_add(t as u64)),
505 _ => Timeout::Never,
506 };
507 match me.with_proc(|p| p.receive(me.inbox(), tags, any, timeout)) {
508 Received::Now(answer) => Some(answer),
509 Received::Parked(generation) => {
510 if let Timeout::At(d) = timeout {
511 sched.add_timer(d, me.pid, generation);
512 }
513 None
514 }
515 }
516}
517
518/// Park until the receive [`take_or_wait`] started ends: what it returns,
519/// or unit while unwinding (the actor exited). A message on a tag it does
520/// not wait for wakes it too; it then waits again.
521///
522/// # Safety
523/// `ctx` must be the running actor's context, waiting in a receive.
524unsafe fn wait_for_message(ctx: *mut Ctx) -> Value {
525 // SAFETY: forwarded caller guarantee.
526 unsafe {
527 loop {
528 crate::sched::suspend_waiting(ctx);
529 let (a, _) = this(ctx);
530 if (*ctx).yielding != YIELD_NONE {
531 // A message may have been handed over before the exit.
532 let handed = a.with_proc(|p| p.take_wake());
533 drop_value(ctx, handed);
534 return UNIT;
535 }
536 if let Some(answer) = a.with_proc(|p| p.receive_woken(a.inbox())) {
537 return answer;
538 }
539 }
540 }
541}
542
543/// `coerce` (std/process): the value as another type. Only
544/// `std/process.select` uses it, on a message whose type its subject
545/// guarantees.
546///
547/// # Safety
548/// `v` must be live.
549#[unsafe(no_mangle)]
550pub unsafe extern "C" fn tl_coerce(_ctx: *mut Ctx, v: Value) -> Value {
551 // SAFETY: caller guarantees a live value (borrowed; the result is a
552 // new reference).
553 unsafe { crate::heap::dup_value(v) }
554}
555
556unsafe extern "C" fn op_link(
557 ctx: *mut Ctx,
558 this_: Value,
559 pid: Value,
560 _: Value,
561 _: Value,
562 _: Value,
563 _: Value,
564) -> Value {
565 // SAFETY: as above.
566 unsafe {
567 drop_value(ctx, this_);
568 let (a, sched) = this(ctx);
569 let other = int_get(pid);
570 if crate::dist::is_remote(other) {
571 crate::dist::link(sched, a, other);
572 } else if other != a.pid {
573 link_local(sched, a, other);
574 }
575 done(ctx, UNIT)
576 }
577}
578
579/// The running actor `me` links the local `other`: its own half, then
580/// `other`'s, each under its actor's lock. An exit counts only through a
581/// half its receiver still has (`propagate_exit`), so `other`'s half is
582/// made only:
583/// - while `other` is alive: else there is no link, as with a dead pid,
584/// and `me`'s half goes again;
585/// - while `me` has not exited, which `other`'s lock decides: `me`'s exit
586/// sets its kill flag under its own lock, then signals `other` under
587/// `other`'s. With the flag not set, that signal comes after and finds
588/// the half; with it set, it may have come already, and the half would
589/// link `other` to an actor that is gone.
590fn link_local(sched: &Shared, me: &Actor, other: i64) {
591 if !me.with_proc(|p| p.link(other)) {
592 // Exited (killed while running): no link is due.
593 return;
594 }
595 let made = sched.with_alive(other, |o| !me.kill_requested() && o.link(me.pid));
596 if made.is_none() {
597 me.with_proc(|p| p.unlink(other));
598 }
599}
600
601unsafe extern "C" fn op_monitor(
602 ctx: *mut Ctx,
603 this_: Value,
604 pid: Value,
605 _: Value,
606 _: Value,
607 _: Value,
608 _: Value,
609) -> Value {
610 // SAFETY: as above.
611 unsafe {
612 drop_value(ctx, this_);
613 let (a, sched) = this(ctx);
614 let (me, watched) = (a.pid, int_get(pid));
615 let tag = sched.tag();
616 let watching = if crate::dist::is_remote(watched) {
617 crate::dist::monitor(sched, a, watched, tag)
618 } else {
619 sched
620 .with_alive(watched, |w| w.ties().monitors.push((me, tag)))
621 .is_some()
622 };
623 if !watching {
624 // Like Erlang's `noproc`: the actor is already gone.
625 deliver_down(sched, me, tag, watched, &Exit::Crashed("noproc".into()));
626 }
627 done(ctx, subject(me, tag))
628 }
629}
630
631/// `demonitor(pid, down)`: from now on, no `Down` of the monitor of `pid`
632/// that `down` came from is delivered; one already delivered stays
633/// (`std/process.demonitor_flush` drops it). Anything else (another pid,
634/// a subject of no monitor) is ignored. `down` must be the caller's.
635unsafe extern "C" fn op_demonitor(
636 ctx: *mut Ctx,
637 this_: Value,
638 pid: Value,
639 down: Value,
640 _: Value,
641 _: Value,
642 _: Value,
643) -> Value {
644 // SAFETY: as above; `down` is a subject.
645 unsafe {
646 drop_value(ctx, this_);
647 let (a, sched) = this(ctx);
648 let (owner, tag) = subject_parts(down);
649 drop_value(ctx, down);
650 let (me, watched) = (a.pid, int_get(pid));
651 if owner != me {
652 return crate::effects::tl_panic_str(
653 ctx,
654 c"demonitor on a subject owned by another actor".as_ptr(),
655 );
656 }
657 if crate::dist::is_remote(watched) {
658 crate::dist::demonitor(a, watched, tag);
659 } else {
660 // Under the watched actor's lock, which its exit puts its local
661 // watchers' `Down`s in their inboxes under (`exit_actor`): the
662 // monitor is removed first, or its `Down` is delivered already.
663 sched.with_alive(watched, |w| w.demonitor(me, tag));
664 }
665 done(ctx, UNIT)
666 }
667}
668
669/// `unlink(pid)`: from now on the link with `pid` has no effect on this
670/// actor, until a new `link`; an `ExitSignal` already in its mailbox
671/// stays. Its own half goes first, so an exit on its way finds none
672/// (`propagate_exit`); then `pid`'s, which `pid`'s node removes for a
673/// remote `pid` once the unlink reaches it (`dist`). No link, or a pid
674/// that is gone: nothing.
675unsafe extern "C" fn op_unlink(
676 ctx: *mut Ctx,
677 this_: Value,
678 pid: Value,
679 _: Value,
680 _: Value,
681 _: Value,
682 _: Value,
683) -> Value {
684 // SAFETY: as above.
685 unsafe {
686 drop_value(ctx, this_);
687 let (a, sched) = this(ctx);
688 let (me, other) = (a.pid, int_get(pid));
689 if crate::dist::is_remote(other) {
690 crate::dist::unlink(a, other);
691 } else if other != me {
692 a.with_proc(|p| p.unlink(other));
693 sched.with_alive(other, |o| o.unlink(me));
694 }
695 done(ctx, UNIT)
696 }
697}
698
699unsafe extern "C" fn op_kill(
700 ctx: *mut Ctx,
701 this_: Value,
702 pid: Value,
703 _: Value,
704 _: Value,
705 _: Value,
706 _: Value,
707) -> Value {
708 // SAFETY: as above.
709 unsafe {
710 drop_value(ctx, this_);
711 let (a, sched) = this(ctx);
712 let target = int_get(pid);
713 if crate::dist::is_remote(target) {
714 crate::dist::signal(a.pid, target, crate::dist::Signal::Kill);
715 } else {
716 exit_actor(sched, target, Exit::Killed);
717 }
718 done(ctx, UNIT)
719 }
720}
721
722/// `stop(pid)`: an actor that traps exits gets `ExitSignal(self,
723/// Shutdown)`, to clean up and return; any other stops with `Shutdown`
724/// (ADR 0060). Unknown or exited pids are ignored.
725unsafe extern "C" fn op_stop(
726 ctx: *mut Ctx,
727 this_: Value,
728 pid: Value,
729 _: Value,
730 _: Value,
731 _: Value,
732 _: Value,
733) -> Value {
734 // SAFETY: as above.
735 unsafe {
736 drop_value(ctx, this_);
737 let (a, sched) = this(ctx);
738 let target = int_get(pid);
739 if crate::dist::is_remote(target) {
740 crate::dist::signal(a.pid, target, crate::dist::Signal::Stop);
741 } else {
742 stop(sched, a.pid, target);
743 }
744 done(ctx, UNIT)
745 }
746}
747
748/// `from` asks the local `target` to stop (`op_stop`).
749pub(crate) fn stop(sched: &Shared, from: i64, target: i64) {
750 match sched.with_alive(target, |p| p.ties().trap) {
751 Some(true) => deliver_exit_signal(sched, target, from, &Exit::Shutdown),
752 Some(false) => exit_actor(sched, target, Exit::Shutdown),
753 None => {}
754 }
755}
756
757unsafe extern "C" fn op_trap_exits(
758 ctx: *mut Ctx,
759 this_: Value,
760 on: Value,
761 _: Value,
762 _: Value,
763 _: Value,
764 _: Value,
765) -> Value {
766 // SAFETY: as above.
767 unsafe {
768 drop_value(ctx, this_);
769 let (a, _) = this(ctx);
770 a.with_proc(|p| p.ties().trap = !is_ptr(on) && int_get(on) == 1);
771 done(ctx, UNIT)
772 }
773}
774
775unsafe extern "C" fn op_exits(
776 ctx: *mut Ctx,
777 this_: Value,
778 _: Value,
779 _: Value,
780 _: Value,
781 _: Value,
782 _: Value,
783) -> Value {
784 // SAFETY: as above.
785 unsafe {
786 drop_value(ctx, this_);
787 let (a, sched) = this(ctx);
788 let me = a.pid;
789 let tag = a.with_proc(|p| *p.ties().exits_tag.get_or_insert_with(|| sched.tag()));
790 done(ctx, subject(me, tag))
791 }
792}
793
794unsafe extern "C" fn op_sleep(
795 ctx: *mut Ctx,
796 this_: Value,
797 ms: Value,
798 _: Value,
799 _: Value,
800 _: Value,
801 _: Value,
802) -> Value {
803 // SAFETY: as above.
804 unsafe {
805 drop_value(ctx, this_);
806 let (a, sched) = this(ctx);
807 let ms = saturating(ms);
808 if ms <= 0 {
809 // Go to the back of the run queue.
810 crate::sched::tl_preempt(ctx);
811 return done(ctx, UNIT);
812 }
813 let deadline = sched.clock().saturating_add(ms as u64);
814 let generation = a.with_proc(|p| p.park_as(Wait::Sleep { deadline }));
815 sched.add_timer(deadline, a.pid, generation);
816 let answer = park(ctx);
817 done(ctx, answer)
818 }
819}
820
821/// `pid_eq` from `std/process`.
822#[unsafe(no_mangle)]
823pub extern "C" fn tl_pid_eq(_ctx: *mut Ctx, a: Value, b: Value) -> Value {
824 int(i64::from(a == b))
825}