//! Hibernating the stacks of parked actors (ADR 0028): each thread lists //! the actors that parked on it ([`Parked`]) and, two epochs later, //! hibernates those still in the same wait. use std::collections::VecDeque; use std::sync::atomic::Ordering; use std::time::Duration; use crate::otp::Wait; use super::Shared; use super::actor::Proc; use super::local::LOCAL; /// Actors that parked on this thread (`run_slice`), oldest first, each at /// most once (`Cell::listed`), with the epoch it was listed in. Two epochs /// later the thread looks at it again: hibernated if it is still in a wait /// that began two epochs ago, listed again if it parked since, dropped if /// it is running or gone. pub(super) struct Parked { pub(super) list: VecDeque<(i64, u32)>, /// Calls since the clock was last read. pub(super) count: u32, } impl Parked { /// The entries two epochs old, at `epoch`. fn due(&mut self, epoch: u32, out: &mut Vec) { while let Some(&(pid, listed)) = self.list.front() { if listed.wrapping_add(2) > epoch { break; } self.list.pop_front(); out.push(pid); } } } /// Whether the actor waits in a way that nothing else reads its stack /// in: a receive or a sleep. Not a blocking call (it runs on the actor's /// values), nor a socket wait (short by design). pub(super) fn hibernatable(p: &Proc) -> bool { matches!(p.wait, Wait::Receive { .. } | Wait::Sleep { .. }) && !p.dead } /// The epoch now: counts one more if [`PARKED_EPOCH`](super::PARKED_EPOCH) /// has passed. fn epoch(sched: &Shared) -> u32 { let now = sched.started.elapsed().as_nanos() as u64; let at = sched.epoch_at.load(Ordering::Relaxed); if now.saturating_sub(at) >= sched.epoch_ns && sched .epoch_at .compare_exchange(at, now, Ordering::Relaxed, Ordering::Relaxed) .is_ok() { sched.epoch.fetch_add(1, Ordering::Relaxed); } sched.epoch.load(Ordering::Relaxed) } /// `pid` parked on this thread, and was not listed: list it. pub(super) fn parked_here(sched: &Shared, pid: i64) { let epoch = sched.epoch.load(Ordering::Relaxed); LOCAL.with(|l| { if let Some(local) = l.borrow_mut().as_mut() { local.parked.list.push_back((pid, epoch)); } }); } /// Look again at this thread's listed actors that are due, and hibernate /// those still parked since then (`check`: read the clock now, as after a /// sleep; else every 256 calls). pub(super) fn hibernate_due(sched: &Shared, check: bool) { let mut due = Vec::new(); LOCAL.with(|l| { if let Some(local) = l.borrow_mut().as_mut() && !local.parked.list.is_empty() { let parked = &mut local.parked; parked.count = parked.count.wrapping_add(1); if check || sched.epoch_ns == 0 || parked.count.is_multiple_of(256) { parked.due(epoch(sched), &mut due); } } }); for pid in due { hibernate(sched, pid); } } /// How long this thread may sleep before it must hibernate parked actors: /// an epoch if it has any, else as long as it likes. pub(super) fn parked_timeout(sched: &Shared) -> Option { LOCAL.with(|l| { l.borrow() .as_ref() .is_some_and(|local| !local.parked.list.is_empty()) .then(|| Duration::from_nanos(sched.epoch_ns.max(1_000_000))) }) } /// Hibernate `pid`'s stack if it has been parked for two epochs: under /// its lock, suspended and not queued, nothing can resume it, and in a /// receive or a sleep nothing else reads its stack. `run_slice` thaws it /// before it runs again, whoever wakes it (a message, a timer, a kill). /// Parked since less long: listed again. Running or gone: dropped (it is /// listed again when it parks). fn hibernate(sched: &Shared, pid: i64) { let Some(cell) = sched.cell(pid) else { return; }; let mut p = cell.lock(); let since = cell.parked_epoch.load(Ordering::Relaxed); let now = sched.epoch.load(Ordering::Relaxed); let parked = hibernatable(&p); match p.run.idle_fiber() { Some(fiber) if parked && since.wrapping_add(2) <= now => fiber.hibernate(), Some(_) if parked => { drop(p); LOCAL.with(|l| { if let Some(local) = l.borrow_mut().as_mut() { local.parked.list.push_back((pid, since)); } }); return; } _ => {} } cell.listed.store(false, Ordering::Relaxed); }