tobogganSe connecter

louise

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

115 lignes · 3467 octetsBrut · Historique
1//! A hand-off between scheduler threads (ADR 0004, Revision 2), as a small
2//! type that the scheduler uses and loom models check
3//! (`RUSTFLAGS="--cfg loom" cargo test -p louise_rt --release --lib handoff`).
4//!
5//! [`RunState`]: an actor is woken while another thread runs it. A
6//! wake-up queues the actor only if it is suspended and not queued yet;
7//! otherwise the thread running it queues it when it yields. No wake-up is
8//! lost, none is doubled. Its races with messages, timers and kills are
9//! modelled in `otp`, with the mailbox.
10//!
11//! The other hand-off, a thread going to sleep while another pushes work,
12//! is in `park` (Revision 8).
13
14#[cfg(loom)]
15pub(crate) use loom::sync::atomic::{AtomicUsize, Ordering};
16
17#[cfg(not(loom))]
18pub(crate) use std::sync::atomic::{AtomicUsize, Ordering};
19
20/// Where an actor is, as the scheduler sees it. Kept under the actor's
21/// lock, next to its wait state.
22pub(crate) enum Run<F> {
23 /// Suspended (or not started): its fiber.
24 Suspended(F),
25 /// A thread is running it.
26 Running,
27 /// Its fiber returned.
28 Finished,
29}
30
31pub(crate) struct RunState<F> {
32 run: Run<F>,
33 /// In a run queue (at most once).
34 queued: bool,
35}
36
37impl<F> RunState<F> {
38 pub(crate) fn new(fiber: F) -> Self {
39 RunState {
40 run: Run::Suspended(fiber),
41 queued: false,
42 }
43 }
44
45 /// The actor became ready: whether the caller must queue it.
46 pub(crate) fn wake(&mut self) -> bool {
47 if matches!(self.run, Run::Suspended(_)) && !self.queued {
48 self.queued = true;
49 return true;
50 }
51 false
52 }
53
54 /// Mark a new actor as queued (it is pushed right after).
55 pub(crate) fn queue_new(&mut self) {
56 debug_assert!(!self.queued);
57 self.queued = true;
58 }
59
60 /// Taken from a run queue: its fiber, to resume.
61 pub(crate) fn start(&mut self) -> Option<F> {
62 self.queued = false;
63 match std::mem::replace(&mut self.run, Run::Running) {
64 Run::Suspended(f) => Some(f),
65 other => {
66 self.run = other;
67 None
68 }
69 }
70 }
71
72 /// It yielded, `ready` or waiting: whether the caller must queue it
73 /// again (it was preempted, or woken while it ran).
74 pub(crate) fn stop(&mut self, fiber: F, ready: bool) -> bool {
75 self.run = Run::Suspended(fiber);
76 self.wake_if(ready)
77 }
78
79 fn wake_if(&mut self, ready: bool) -> bool {
80 ready && self.wake()
81 }
82
83 /// The fiber of an actor that is suspended and not queued: nothing
84 /// resumes it while the caller holds the actor's lock.
85 pub(crate) fn idle_fiber(&mut self) -> Option<&mut F> {
86 match &mut self.run {
87 Run::Suspended(f) if !self.queued => Some(f),
88 _ => None,
89 }
90 }
91
92 pub(crate) fn finish(&mut self) {
93 self.run = Run::Finished;
94 self.queued = false;
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn run_state_transitions() {
104 let mut r = RunState::new(1);
105 assert!(r.wake());
106 assert!(!r.wake(), "queued at most once");
107 assert_eq!(r.start(), Some(1));
108 assert!(!r.wake(), "running: its thread requeues it");
109 assert!(r.stop(1, true), "woken while running");
110 assert_eq!(r.start(), Some(1));
111 assert!(!r.stop(1, false), "parked");
112 r.finish();
113 assert!(!r.wake(), "finished");
114 }
115}