Skip to main content

dope_core/driver/schedule/
mod.rs

1use std::{cell, marker, mem, time};
2
3use o3::{alloc, base::cell::region, mem::quota};
4
5use crate::{
6    driver,
7    driver::{
8        receive::{credit, credit::storage},
9        route::table::conn,
10        settings,
11    },
12};
13
14pub mod clock;
15pub mod timing;
16
17pub mod app;
18pub mod maint;
19pub mod raw;
20pub mod ready;
21pub mod sequence;
22#[doc(hidden)]
23pub mod state;
24
25pub const LIMIT: usize = 256;
26
27pub struct Domain<'d, Lane = ()>(
28    marker::PhantomData<&'d mut &'d ()>,
29    marker::PhantomData<fn(Lane) -> Lane>,
30);
31
32/// Exact result of acquiring scheduler capacity from a non-empty source.
33#[must_use = "capacity reports whether the scheduler granted the operation"]
34pub enum Capacity<T> {
35    Available(T),
36    Exhausted,
37}
38
39#[derive(Clone, Copy)]
40#[repr(transparent)]
41pub struct Turn<'turn, 'd> {
42    state: &'turn state::Core<'d>,
43}
44
45impl<'turn, 'd> Turn<'turn, 'd> {
46    pub(in crate::driver::schedule) const fn new(state: &'turn state::Core<'d>) -> Self {
47        Self { state }
48    }
49
50    pub fn reborrow(&self) -> Turn<'_, 'd> {
51        Turn::new(self.state)
52    }
53
54    pub fn clock(self) -> clock::Clock<'turn, 'd> {
55        self.state.driver.scheduler().clock()
56    }
57
58    pub fn app(self) -> app::Work<'turn, 'd> {
59        app::Work { state: self.state }
60    }
61
62    pub fn maint(self) -> maint::Work<'turn, 'd> {
63        maint::Work { state: self.state }
64    }
65
66    pub fn next_step<'step>(
67        self,
68        driver: &'step mut driver::Local<'d>,
69    ) -> Option<crate::control::Step<'step, 'd>> {
70        self.coord().take().ok()?;
71        Some(crate::control::Step::new(driver))
72    }
73
74    #[doc(hidden)]
75    pub fn coord(self) -> state::Coord<'turn, 'd> {
76        state::Coord::new(self.state)
77    }
78}
79
80const _: () = {
81    let word = mem::size_of::<usize>();
82    assert!(mem::size_of::<Turn<'static, 'static>>() == word);
83    assert!(mem::size_of::<quota::Admission<usize>>() == 2 * word);
84    assert!(mem::size_of::<Capacity<&'static u8>>() == word);
85};
86
87/// Aggregate liveness of retained work owned by one driver.
88#[derive(Default)]
89#[must_use]
90pub struct Waits<'d>(Option<Wait<'d>>);
91
92impl<'d> Waits<'d> {
93    pub fn new() -> Self {
94        Self::default()
95    }
96
97    pub fn as_ref(&self) -> Option<&Wait<'d>> {
98        self.0.as_ref()
99    }
100
101    pub fn event(&mut self, proof: region::Identity<'d>) {
102        if self.0.is_none() {
103            self.0 = Some(Wait::Event(proof));
104        }
105    }
106
107    pub fn until(&mut self, deadline: clock::Deadline<'d>) {
108        match &mut self.0 {
109            Some(Wait::Until(prior)) => {
110                if deadline < *prior {
111                    *prior = deadline;
112                }
113            }
114            wait => *wait = Some(Wait::Until(deadline)),
115        }
116    }
117}
118
119const _: () = assert!(mem::size_of::<Waits<'static>>() <= 24);
120
121pub(in crate::driver) struct State {
122    pub(in crate::driver) owner: storage::Owner<ready::Stored, credit::Wake, ready::Arena>,
123    clock: cell::Cell<time::Instant>,
124}
125
126impl State {
127    pub(in crate::driver) fn try_new(plan: settings::Plan<'_>) -> Result<Self, alloc::Error> {
128        Ok(Self {
129            owner: storage::Owner::try_new(plan)?,
130            clock: cell::Cell::new(time::Instant::now()),
131        })
132    }
133}
134
135#[derive(Clone, Copy)]
136#[repr(transparent)]
137pub struct Context<'d>(driver::Reference<'d>);
138
139impl<'d> Context<'d> {
140    pub(in crate::driver) const fn new(driver: driver::Reference<'d>) -> Self {
141        Self(driver)
142    }
143
144    pub(in crate::driver) fn clock(self) -> clock::Clock<'d, 'd> {
145        clock::Clock::new(&self.0.shared.context().schedule.clock, self.0.brand)
146    }
147
148    pub(in crate::driver) fn set_turn_now(self, now: time::Instant) {
149        self.0.shared.context().schedule.clock.set(now);
150    }
151
152    /// Allocates fixed retained-credit storage in this driver generation.
153    pub fn try_storage(
154        self,
155        capacity: conn::Capacity,
156    ) -> Result<credit::Storage<'d>, alloc::Error> {
157        let shared: &'d driver::Shared = self.0.shared.context();
158        let owner = &shared.schedule.owner;
159        owner.try_storage(capacity, self.0.brand)
160    }
161}
162
163const _: () = assert!(
164    std::mem::size_of::<Context<'static>>() == std::mem::size_of::<driver::Reference<'static>>()
165);
166
167/// Driver-scoped evidence that a retained owner is waiting for an external
168/// wake or a monotonic deadline.
169#[must_use]
170pub enum Wait<'d> {
171    Event(region::Identity<'d>),
172    Until(clock::Deadline<'d>),
173}
174
175const _: () = {
176    assert!(mem::size_of::<Wait<'static>>() == mem::size_of::<Option<clock::Deadline<'static>>>());
177    assert!(
178        mem::align_of::<Wait<'static>>() == mem::align_of::<Option<clock::Deadline<'static>>>()
179    );
180    assert!(mem::size_of::<Option<Wait<'static>>>() == mem::size_of::<Wait<'static>>());
181    assert!(!mem::needs_drop::<Wait<'static>>());
182};