| @@ -86,13 +86,31 @@ type ActorClosureFn = unsafe extern "C" fn(*mut Ctx, Value) -> Value; |
| 86 | 86 | struct Fiber(Coroutine<(), (), ()>, usize); |
| 87 | 87 | |
| 88 | 88 | // SAFETY: a suspended fiber is its stack and saved registers (`fiber`), |
| 89 | | −// so it can resume on any thread, provided the code on it keeps no |
| 90 | | −// references to thread-local storage across a suspension. Code on actor |
| 91 | | −// fibers never does: generated C uses no thread-locals (its state is in |
| 92 | | −// `tl_ctx`), and the runtime keeps all actor state in `Actor`/`Sched`; |
| 93 | | −// Rust's own thread-locals (stdout locks, panic counts) are never held |
| 94 | | −// across a suspension, which only happens in `preempt_root` and |
| 95 | | −// `handlers` (ADR 0011). |
| 89 | +// so it can resume on any thread, provided no frame on it keeps the |
| 90 | +// address of a thread-local across a suspension (`park`, `tl_preempt`, |
| 91 | +// `handlers`; ADR 0011). Not only references: the optimiser treats a |
| 92 | +// thread-local's address as constant within a function (LLVM's |
| 93 | +// `llvm.threadlocal.address` reads no memory), so after inlining it may |
| 94 | +// compute the address once and reuse it after a call that switched |
| 95 | +// threads. |
| 96 | +// - Generated C uses no thread-locals: its state is in `tl_ctx`. |
| 97 | +// - The scheduler's `LOCAL`, `IN_SLICE` and `DEFERRED` are read on actor |
| 98 | +// fibers only in `#[inline(never)]` functions that do not suspend |
| 99 | +// (`push_local`, `local_index`, `in_slice_deferral`). `run_slice`, |
| 100 | +// `find_work` and `worker` run on the scheduler thread's own stack. |
| 101 | +// - `callback`'s `FOREIGN_THREAD` and `THREAD_TOKEN` are read while a C |
| 102 | +// call is in progress, during which the actor never suspends. |
| 103 | +// - The leak counter (`heap::stats`, `MINE`) is read inline on every |
| 104 | +// allocation and free: out of line, it cost 12% on |
| 105 | +// `tests/bench/floats.til`. No runtime function that suspends reads it |
| 106 | +// (checked on the release build's disassembly), but an inlining |
| 107 | +// decision could change that. The effect would be to update another |
| 108 | +// thread's count (a wrong leak-oracle total) or, if that thread never |
| 109 | +// allocated, to register into its thread-local (a data race). Open for |
| 110 | +// engineering review: `docs/code-health.md`, M7. |
| 111 | +// - Rust's own thread-locals (stdout locks, panic counts, `HashMap` keys |
| 112 | +// in `send`, channels in `blocking`) and mimalloc's are read in code |
| 113 | +// that does not suspend. |
| 96 | 114 | unsafe impl Send for Fiber {} |
| 97 | 115 | |
| 98 | 116 | enum Body { |
| @@ -442,12 +460,22 @@ fn push_local(sched: &Shared, pid: i64) -> bool { |
| 442 | 460 | }) |
| 443 | 461 | } |
| 444 | 462 | |
| 463 | +/// During a slice, whether this is the first actor this thread queued |
| 464 | +/// (it then wakes nobody: `Shared::push`). Not inlined (see `Fiber`). |
| 465 | +#[inline(never)] |
| 466 | +fn in_slice_deferral() -> bool { |
| 467 | + IN_SLICE.get() && !DEFERRED.replace(true) |
| 468 | +} |
| 469 | + |
| 445 | 470 | #[inline(never)] |
| 446 | 471 | fn pop_local() -> Option<i64> { |
| 447 | 472 | LOCAL.with(|l| l.borrow().as_ref().and_then(|local| local.queue.pop())) |
| 448 | 473 | } |
| 449 | 474 | |
| 450 | 475 | /// This thread's index among `sched`'s threads (0 for other threads). |
| 476 | +/// Not inlined, like every thread-local access on an actor's fiber (see |
| 477 | +/// `Fiber`). |
| 478 | +#[inline(never)] |
| 451 | 479 | fn local_index(sched: &Shared) -> usize { |
| 452 | 480 | LOCAL.with(|l| match &*l.borrow() { |
| 453 | 481 | Some(local) if local.owner == sched as *const Shared as usize => local.index, |
| @@ -617,7 +645,7 @@ impl Shared { |
| 617 | 645 | fn push(&self, pid: i64) { |
| 618 | 646 | self.pending.fetch_add(1, Ordering::SeqCst); |
| 619 | 647 | if push_local(self, pid) { |
| 620 | | − if IN_SLICE.get() && !DEFERRED.replace(true) { |
| 648 | + if in_slice_deferral() { |
| 621 | 649 | return; |
| 622 | 650 | } |
| 623 | 651 | } else { |