dope_core/driver/schedule/
mod.rs1use std::{cell, mem, time};
2
3use o3::{alloc, base::cell::region, mem::quota};
4use state::{lanes, scope};
5
6use crate::{
7 driver,
8 driver::{
9 receive::{credit, credit::storage},
10 route::table::conn,
11 settings,
12 },
13};
14
15pub mod clock;
16pub mod timing;
17
18pub mod app;
19pub mod maint;
20pub mod ready;
21pub mod sequence;
22#[doc(hidden)]
23pub mod state;
24
25pub const LIMIT: usize = 256;
26
27#[must_use = "capacity reports whether the scheduler granted the operation"]
29pub enum Capacity<T> {
30 Available(T),
31 Exhausted,
32}
33
34#[derive(Clone, Copy)]
35#[repr(transparent)]
36pub struct Turn<'turn, 'd> {
37 state: &'turn state::Core<'d>,
38}
39
40impl<'turn, 'd> Turn<'turn, 'd> {
41 pub(in crate::driver::schedule) const fn new(state: &'turn state::Core<'d>) -> Self {
42 Self { state }
43 }
44
45 pub fn reborrow(&self) -> Turn<'_, 'd> {
46 Turn::new(self.state)
47 }
48
49 pub fn clock(self) -> clock::Clock<'turn, 'd> {
50 self.state.driver.scheduler().clock()
51 }
52
53 pub fn app(self) -> app::Work<'turn, 'd> {
54 app::Work { state: self.state }
55 }
56
57 pub fn maint(self) -> maint::Work<'turn, 'd> {
58 maint::Work { state: self.state }
59 }
60
61 pub fn next_step<'step>(
62 self,
63 driver: &'step mut driver::Local<'d>,
64 ) -> Option<crate::control::Step<'step, 'd>> {
65 self.coord().take()?;
66 Some(crate::control::Step::new(driver))
67 }
68
69 #[doc(hidden)]
70 pub fn coord(self) -> scope::Coord<'turn, 'd> {
71 scope::Coord::new(self.state)
72 }
73}
74
75impl<'driver, 'turn, 'd: 'driver + 'turn, Report: crate::io::Report>
76 crate::executor::raw::Sweep<'driver, 'turn, 'd, Report>
77{
78 pub(crate) unsafe fn leaf<A: crate::executor::raw::App<'d>>(
79 &mut self,
80 app: std::pin::Pin<&mut A>,
81 ) {
82 let peers = self.parks;
83 let ahead = self.ahead;
84 self.parks = peers.saturating_sub(1);
85 self.ahead = [ahead[0].saturating_sub(1), ahead[1].saturating_sub(1)];
86 let turn = self.turn;
87 let ledgers = &turn.state.lanes;
88 let _app = lanes::split(&ledgers.app, peers, ahead[0]);
89 let _maint = lanes::split(&ledgers.maint, peers, ahead[1]);
90 let _coord = if A::COORDS != 0 {
91 let peers = self.groups as usize;
92 let ahead = self.coord as usize;
93 self.groups = self.groups.saturating_sub(1);
94 self.coord = self.coord.saturating_sub(1);
95 Some(lanes::split(&ledgers.coord, peers, ahead))
96 } else {
97 None
98 };
99 unsafe { A::park(app, turn, &mut *self.driver) };
101 }
102
103 pub unsafe fn coordinate<A: crate::executor::raw::App<'d>>(
106 &mut self,
107 app: std::pin::Pin<&mut A>,
108 ) {
109 let peers = self.groups as usize;
110 let ahead = self.coord as usize;
111 self.groups = self.groups.saturating_sub(1);
112 self.coord = self.coord.saturating_sub(1);
113 let turn = self.turn;
114 let _quota = lanes::split(&turn.state.lanes.coord, peers, ahead);
115 unsafe { A::park(app, turn, &mut *self.driver) };
117 }
118}
119
120const _: () = {
121 let word = mem::size_of::<usize>();
122 assert!(mem::size_of::<Turn<'static, 'static>>() == word);
123 assert!(mem::size_of::<quota::Admission<usize>>() == 2 * word);
124 assert!(mem::size_of::<Capacity<&'static u8>>() == word);
125};
126
127#[must_use]
129pub struct Waits<'d>(Option<Wait<'d>>);
130
131impl<'d> Waits<'d> {
132 pub fn new() -> Self {
133 Self(None)
134 }
135
136 pub fn as_ref(&self) -> Option<&Wait<'d>> {
137 self.0.as_ref()
138 }
139
140 pub fn event(&mut self, proof: region::Identity<'d>) {
141 if self.0.is_none() {
142 self.0 = Some(Wait::Event(proof));
143 }
144 }
145
146 pub fn until(&mut self, deadline: clock::Deadline<'d>) {
147 match &mut self.0 {
148 Some(Wait::Until(prior)) => {
149 if deadline < *prior {
150 *prior = deadline;
151 }
152 }
153 wait => *wait = Some(Wait::Until(deadline)),
154 }
155 }
156}
157
158const _: () = assert!(mem::size_of::<Waits<'static>>() <= 24);
159
160pub(in crate::driver) struct State {
161 pub(in crate::driver) owner: storage::Owner<ready::Stored, credit::Wake, ready::Arena>,
162 clock: cell::Cell<time::Instant>,
163}
164
165impl State {
166 pub(in crate::driver) fn try_new(plan: settings::Plan<'_>) -> Result<Self, alloc::Error> {
167 Ok(Self {
168 owner: storage::Owner::try_new(plan)?,
169 clock: cell::Cell::new(time::Instant::now()),
170 })
171 }
172}
173
174#[derive(Clone, Copy)]
175#[repr(transparent)]
176pub struct Context<'d>(driver::Reference<'d>);
177
178impl<'d> Context<'d> {
179 pub(in crate::driver) const fn new(driver: driver::Reference<'d>) -> Self {
180 Self(driver)
181 }
182
183 pub(in crate::driver) fn clock(self) -> clock::Clock<'d, 'd> {
184 clock::Clock::new(&self.0.shared.context().schedule.clock, self.0.brand)
185 }
186
187 pub(in crate::driver) fn set_turn_now(self, now: time::Instant) {
188 self.0.shared.context().schedule.clock.set(now);
189 }
190
191 pub fn try_storage(
193 self,
194 capacity: conn::Capacity,
195 ) -> Result<credit::Storage<'d>, alloc::Error> {
196 let shared: &'d driver::Shared = self.0.shared.context();
197 let owner = &shared.schedule.owner;
198 owner.try_storage(capacity, self.0.brand)
199 }
200}
201
202const _: () = assert!(
203 std::mem::size_of::<Context<'static>>() == std::mem::size_of::<driver::Reference<'static>>()
204);
205
206#[must_use]
209pub enum Wait<'d> {
210 Event(region::Identity<'d>),
211 Until(clock::Deadline<'d>),
212}
213
214const _: () = {
215 assert!(mem::size_of::<Wait<'static>>() == mem::size_of::<Option<clock::Deadline<'static>>>());
216 assert!(
217 mem::align_of::<Wait<'static>>() == mem::align_of::<Option<clock::Deadline<'static>>>()
218 );
219 assert!(mem::size_of::<Option<Wait<'static>>>() == mem::size_of::<Wait<'static>>());
220 assert!(!mem::needs_drop::<Wait<'static>>());
221};