tobogganSe connecter

louise

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

108 lignes · 3962 octetsBrut · Historique
1//! The scheduler's clock (virtual until the first socket opens, real
2//! after — unless `LOUISE_VIRTUAL_TIME` keeps it virtual, ADR 0055) and
3//! the timers it fires (`timers`, ADR 0004 Revision 5).
4
5use std::sync::atomic::Ordering;
6
7use crate::timers::{Firing, Timer};
8
9use super::local::local_index;
10use super::{Shared, Wakes};
11
12impl Shared {
13 /// The clock, in milliseconds. In real time, it may lag by the
14 /// slice running when it was last counted.
15 pub(crate) fn clock(&self) -> u64 {
16 self.clock.load(Ordering::Acquire)
17 }
18
19 /// The clock in nanoseconds, for `std/time.monotonic_ns` (ADR 0036):
20 /// virtual time in whole milliseconds, then, once real, the clock when
21 /// it switched plus the real time since, to the nanosecond. It never
22 /// goes back: the millisecond clock follows the same sum, rounded
23 /// down.
24 pub(crate) fn monotonic_ns(&self) -> u64 {
25 match self.real_start.get() {
26 Some((at, base)) => base
27 .saturating_mul(1_000_000)
28 .saturating_add(u64::try_from(at.elapsed().as_nanos()).unwrap_or(u64::MAX)),
29 None => self.clock().saturating_mul(1_000_000),
30 }
31 }
32
33 /// Wake `pid` with a timeout at `deadline` if it is still in wait
34 /// number `generation` then.
35 pub(crate) fn add_timer(&self, deadline: u64, pid: i64, generation: u64) {
36 let timer = Timer {
37 deadline,
38 pid,
39 wait: generation,
40 };
41 self.timers.add(local_index(self), timer);
42 // In real time, the idle thread may be waiting until a later
43 // deadline. (In virtual time it never waits for the clock, and a
44 // nudge per timer would cost a system call each.)
45 if self.real_time() {
46 self.nudge_idler();
47 }
48 }
49
50 /// The clock follows real time.
51 pub(super) fn real_time(&self) -> bool {
52 self.real_start.get().is_some()
53 }
54}
55
56/// Whether `timer` still applies: its actor is in that wait, with that
57/// deadline.
58pub(super) fn timer_live(sched: &Shared, timer: &Timer) -> bool {
59 sched
60 .with_proc(timer.pid, |p| p.waits_until(timer.wait, timer.deadline))
61 .unwrap_or(false)
62}
63
64/// In real time, count the clock up to now, in whole milliseconds (the
65/// remainder is counted next time), and return it.
66pub(super) fn sync_clock(sched: &Shared) -> u64 {
67 let Some((at, base)) = sched.real_start.get() else {
68 return sched.clock();
69 };
70 let now = base + at.elapsed().as_millis() as u64;
71 // Every thread calls this between slices: write only when the clock
72 // moves, so its cache line is not bounced between cores for nothing.
73 let clock = sched.clock();
74 if now <= clock {
75 return clock;
76 }
77 sched.clock.fetch_max(now, Ordering::AcqRel).max(now)
78}
79
80/// In real time, wake the actors whose deadline the clock has reached:
81/// between slices, so they wake even if no thread is ever idle. Skipped if
82/// another thread is firing timers.
83pub(super) fn fire_due(sched: &Shared) {
84 let now = sync_clock(sched);
85 if !sched.timers.maybe_due(now) {
86 return;
87 }
88 if let Some(firing) = sched.timers.try_firing() {
89 fire(sched, &firing, now);
90 }
91}
92
93/// Wake the actors whose timers are due at `now`, in (deadline, pid)
94/// order, if they are still in that wait, each on its home thread.
95///
96/// They are all queued before threads are woken, each home once: waking
97/// one per actor made sleepers wake, steal one actor, and sleep again,
98/// thousands of times per tick (Revision 5).
99pub(super) fn fire(sched: &Shared, firing: &Firing<'_>, now: u64) {
100 let mut wakes = Wakes::default();
101 for timer in firing.take_due(now) {
102 let woke = sched.with_proc_home(timer.pid, |p| p.time_out(timer.wait, timer.deadline));
103 if let Some((true, home)) = woke {
104 wakes.add(sched.queue(timer.pid, home));
105 }
106 }
107 sched.wake(wakes);
108}