//! The scheduler's clock (virtual until the first socket opens, real //! after — unless `LOUISE_VIRTUAL_TIME` keeps it virtual, ADR 0055) and //! the timers it fires (`timers`, ADR 0004 Revision 5). use std::sync::atomic::Ordering; use crate::timers::{Firing, Timer}; use super::local::local_index; use super::{Shared, Wakes}; impl Shared { /// The clock, in milliseconds. In real time, it may lag by the /// slice running when it was last counted. pub(crate) fn clock(&self) -> u64 { self.clock.load(Ordering::Acquire) } /// The clock in nanoseconds, for `std/time.monotonic_ns` (ADR 0036): /// virtual time in whole milliseconds, then, once real, the clock when /// it switched plus the real time since, to the nanosecond. It never /// goes back: the millisecond clock follows the same sum, rounded /// down. pub(crate) fn monotonic_ns(&self) -> u64 { match self.real_start.get() { Some((at, base)) => base .saturating_mul(1_000_000) .saturating_add(u64::try_from(at.elapsed().as_nanos()).unwrap_or(u64::MAX)), None => self.clock().saturating_mul(1_000_000), } } /// Wake `pid` with a timeout at `deadline` if it is still in wait /// number `generation` then. pub(crate) fn add_timer(&self, deadline: u64, pid: i64, generation: u64) { let timer = Timer { deadline, pid, wait: generation, }; self.timers.add(local_index(self), timer); // In real time, the idle thread may be waiting until a later // deadline. (In virtual time it never waits for the clock, and a // nudge per timer would cost a system call each.) if self.real_time() { self.nudge_idler(); } } /// The clock follows real time. pub(super) fn real_time(&self) -> bool { self.real_start.get().is_some() } } /// Whether `timer` still applies: its actor is in that wait, with that /// deadline. pub(super) fn timer_live(sched: &Shared, timer: &Timer) -> bool { sched .with_proc(timer.pid, |p| p.waits_until(timer.wait, timer.deadline)) .unwrap_or(false) } /// In real time, count the clock up to now, in whole milliseconds (the /// remainder is counted next time), and return it. pub(super) fn sync_clock(sched: &Shared) -> u64 { let Some((at, base)) = sched.real_start.get() else { return sched.clock(); }; let now = base + at.elapsed().as_millis() as u64; // Every thread calls this between slices: write only when the clock // moves, so its cache line is not bounced between cores for nothing. let clock = sched.clock(); if now <= clock { return clock; } sched.clock.fetch_max(now, Ordering::AcqRel).max(now) } /// In real time, wake the actors whose deadline the clock has reached: /// between slices, so they wake even if no thread is ever idle. Skipped if /// another thread is firing timers. pub(super) fn fire_due(sched: &Shared) { let now = sync_clock(sched); if !sched.timers.maybe_due(now) { return; } if let Some(firing) = sched.timers.try_firing() { fire(sched, &firing, now); } } /// Wake the actors whose timers are due at `now`, in (deadline, pid) /// order, if they are still in that wait, each on its home thread. /// /// They are all queued before threads are woken, each home once: waking /// one per actor made sleepers wake, steal one actor, and sleep again, /// thousands of times per tick (Revision 5). pub(super) fn fire(sched: &Shared, firing: &Firing<'_>, now: u64) { let mut wakes = Wakes::default(); for timer in firing.take_due(now) { let woke = sched.with_proc_home(timer.pid, |p| p.time_out(timer.wait, timer.deadline)); if let Some((true, home)) = woke { wakes.add(sched.queue(timer.pid, home)); } } sched.wake(wakes); }