| @@ -428,9 +428,25 @@ impl Sched { |
| 428 | 428 | } |
| 429 | 429 | } |
| 430 | 430 | |
| 431 | | −/// Start an actor, linked to `link` if given: before it can run, so even |
| 432 | | −/// one that stops at once is linked. |
| 433 | | −fn spawn_actor(sched: &Shared, body: Body, stack_size: usize, link: Option<i64>) -> i64 { |
| 431 | +/// How a new actor is tied to the one spawning it. |
| 432 | +#[derive(Clone, Copy, PartialEq)] |
| 433 | +pub(crate) enum Tie { |
| 434 | + None, |
| 435 | + Link, |
| 436 | + Monitor, |
| 437 | +} |
| 438 | + |
| 439 | +/// Start an actor, tied to `parent` as `tie` says before it can run, so |
| 440 | +/// even one that stops at once is linked or monitored. Returns its pid, |
| 441 | +/// and the monitor's tag. |
| 442 | +fn spawn_actor( |
| 443 | + sched: &Shared, |
| 444 | + body: Body, |
| 445 | + stack_size: usize, |
| 446 | + parent: Option<i64>, |
| 447 | + tie: Tie, |
| 448 | +) -> (i64, Option<i64>) { |
| 449 | + let link = parent.filter(|_| tie == Tie::Link); |
| 434 | 450 | let mut s = sched.lock(); |
| 435 | 451 | let pid = s.next_pid; |
| 436 | 452 | s.next_pid += 1; |
| @@ -488,22 +504,35 @@ fn spawn_actor(sched: &Shared, body: Body, stack_size: usize, link: Option<i64>) |
| 488 | 504 | if let Some(parent) = link.and_then(|p| s.proc(p)) { |
| 489 | 505 | parent.links.push(pid); |
| 490 | 506 | } |
| 507 | + let monitor = match (parent, tie) { |
| 508 | + (Some(watcher), Tie::Monitor) => { |
| 509 | + let tag = s.tag(); |
| 510 | + s.monitors.push((pid, watcher, tag)); |
| 511 | + Some(tag) |
| 512 | + } |
| 513 | + _ => None, |
| 514 | + }; |
| 491 | 515 | s.run_queue.push_back(pid); |
| 492 | 516 | s.notify = true; |
| 493 | 517 | LIVE_ACTORS.fetch_add(1, Ordering::Relaxed); |
| 494 | | − pid |
| 518 | + (pid, monitor) |
| 495 | 519 | } |
| 496 | 520 | |
| 497 | | −/// Spawn a compiled program's actor running `body` (consumed), linked to |
| 498 | | −/// the running actor if `link`. |
| 521 | +/// Spawn a compiled program's actor running `body` (consumed), tied to |
| 522 | +/// the running actor. Returns its pid, and the monitor's tag. |
| 499 | 523 | /// |
| 500 | 524 | /// # Safety |
| 501 | 525 | /// `ctx` must be the running actor's context. |
| 502 | | −pub(crate) unsafe fn spawn_program(ctx: *mut Ctx, body: Value, link: bool) -> i64 { |
| 526 | +pub(crate) unsafe fn spawn_program(ctx: *mut Ctx, body: Value, tie: Tie) -> (i64, Option<i64>) { |
| 503 | 527 | // SAFETY: forwarded caller guarantee. |
| 504 | 528 | let (a, sched) = unsafe { this(ctx) }; |
| 505 | | − let link = link.then_some(a.pid); |
| 506 | | − spawn_actor(sched, Body::Program(body), PROGRAM_STACK_SIZE, link) |
| 529 | + spawn_actor( |
| 530 | + sched, |
| 531 | + Body::Program(body), |
| 532 | + PROGRAM_STACK_SIZE, |
| 533 | + Some(a.pid), |
| 534 | + tie, |
| 535 | + ) |
| 507 | 536 | } |
| 508 | 537 | |
| 509 | 538 | /// Run an actor's body to completion and record why it exited. |
| @@ -975,7 +1004,7 @@ pub(crate) unsafe fn park(ctx: *mut Ctx) -> Value { |
| 975 | 1004 | pub unsafe extern "C" fn tl_spawn(ctx: *mut Ctx, closure: Value) -> Value { |
| 976 | 1005 | // SAFETY: caller guarantees a running actor. |
| 977 | 1006 | let (_, sched) = unsafe { this(ctx) }; |
| 978 | | − int(spawn_actor(sched, Body::Closure(closure), STACK_SIZE, None)) |
| 1007 | + int(spawn_actor(sched, Body::Closure(closure), STACK_SIZE, None, Tie::None).0) |
| 979 | 1008 | } |
| 980 | 1009 | |
| 981 | 1010 | /// Ask actor `pid` to exit. It unwinds at its next safe point. Unknown or |
| @@ -1002,7 +1031,7 @@ pub extern "C" fn tl_actor_count() -> i64 { |
| 1002 | 1031 | #[unsafe(no_mangle)] |
| 1003 | 1032 | pub unsafe extern "C" fn tl_main(entry: EntryFn) -> i32 { |
| 1004 | 1033 | let sched = Shared::new(); |
| 1005 | | − let root = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None); |
| 1034 | + let (root, _) = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None, Tie::None); |
| 1006 | 1035 | match run(&sched, root, thread_count()) { |
| 1007 | 1036 | Exit::Normal => 0, |
| 1008 | 1037 | _ => 1, |
| @@ -1019,7 +1048,7 @@ pub unsafe extern "C" fn tl_main(entry: EntryFn) -> i32 { |
| 1019 | 1048 | pub unsafe extern "C" fn tl_main_program(entry: EntryFn) -> i32 { |
| 1020 | 1049 | PROGRAM_MODE.store(true, Ordering::Relaxed); |
| 1021 | 1050 | let sched = Shared::new(); |
| 1022 | | − let root = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None); |
| 1051 | + let (root, _) = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None, Tie::None); |
| 1023 | 1052 | match run(&sched, root, thread_count()) { |
| 1024 | 1053 | Exit::Normal => 0, |
| 1025 | 1054 | Exit::Crashed(message) => { |