1use dope_sys::io::reactor::event::Report;
4use std::{
5 convert::Infallible,
6 num::NonZero,
7 ops::{self, ControlFlow},
8 pin,
9};
10
11use crate::{
12 driver::{
13 life::{self, finish},
14 route::{self, kind::Any},
15 schedule,
16 settings::{self, claim, errors},
17 },
18 executor,
19 io::event::Dispatch,
20};
21use o3::{
22 alloc,
23 base::{cell::region, num::bounded},
24};
25
26mod progress;
27mod sealed;
28mod stored;
29
30pub(in crate::executor::raw) use sealed::Sealed;
31
32pub use progress::Progress;
33pub(crate) use stored::{Backing, Stored};
34
35pub trait Layout<'d> {
37 type Scope: route::Selection;
38 type ParkPlan: Plan;
40}
41
42pub unsafe trait App<'d>: Sized + Layout<'d> {
46 const ROUTES: settings::Routes;
47
48 const DISPATCH_ROUTES: settings::Routes = Self::ROUTES;
50
51 const WAKE_ROUTES: settings::Routes = settings::Routes::EMPTY;
53
54 const COORDS: usize = 0;
56
57 const PARK: bool = false;
58
59 fn child(_: pin::Pin<&mut Self>) -> Option<pin::Pin<&mut impl App<'d>>> {
61 None::<pin::Pin<&mut ()>>
62 }
63
64 fn census(_: pin::Pin<&Self>, counts: &mut Census) -> Result<(), alloc::Overflow> {
65 if Self::PARK {
66 counts.include_leaf(Self::COORDS)?;
67 }
68 Ok(())
69 }
70
71 unsafe fn sweep<'app, 'driver, 'turn>(
74 app: pin::Pin<&'app mut Self>,
75 sweep: &mut schedule::raw::Sweep<'driver, 'turn, 'd, impl Report<Event = crate::Reported>>,
76 ) where
77 'd: 'driver + 'turn,
78 {
79 if Self::PARK {
80 unsafe { sweep.leaf(app) };
82 }
83 }
84
85 unsafe fn park<'app, 'driver, 'turn>(
89 app: pin::Pin<&'app mut Self>,
90 turn: schedule::Turn<'turn, 'd>,
91 driver: &'driver mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
92 ) where
93 'd: 'driver + 'turn,
94 {
95 if let Some(child) = Self::child(app) {
96 unsafe { App::park(child, turn, driver) };
98 }
99 }
100
101 fn admit(_: pin::Pin<&Self>, needs: &mut claim::Needs) -> Result<(), errors::Claim> {
103 *needs = needs.include(claim::Claim::new(Self::ROUTES))?;
104 Ok(())
105 }
106
107 unsafe fn released(app: pin::Pin<&mut Self>) {
110 if let Some(child) = Self::child(app) {
111 unsafe { App::<'d>::released(child) };
113 }
114 }
115
116 fn install(app: pin::Pin<&mut Self>, install: life::Install<'_, 'd>) {
117 if let Some(child) = Self::child(app) {
118 App::install(child, install);
119 }
120 }
121
122 unsafe fn activate<'app, 'driver, 'turn>(
126 _: pin::Pin<&'app mut Self>,
127 _: route::Erased<'d, Any>,
128 _: schedule::Turn<'turn, 'd>,
129 _: &'driver mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
130 ) -> ControlFlow<()>
131 where
132 'd: 'driver + 'turn,
133 {
134 ControlFlow::Continue(())
135 }
136
137 fn has_receiver(_: pin::Pin<&Self>, route: route::Route) -> bool {
139 Self::DISPATCH_ROUTES.has_route(route)
140 }
141
142 fn dispatch<'event>(
144 _: pin::Pin<&mut Self>,
145 event: Dispatch<'event, 'd, Self::Scope>,
146 _: schedule::Turn<'_, 'd>,
147 _: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
148 ) -> ops::ControlFlow<Dispatch<'event, 'd, Self::Scope>> {
149 ops::ControlFlow::Break(event)
150 }
151
152 fn progress<const STOP: bool>(
154 _: pin::Pin<&Self>,
155 _: Progress<'_, '_, 'd>,
156 _: Option<&mut schedule::Waits<'d>>,
157 ) -> ops::ControlFlow<()> {
158 ops::ControlFlow::Continue(())
159 }
160
161 fn shutdown(
162 app: pin::Pin<&mut Self>,
163 turn: schedule::Turn<'_, 'd>,
164 driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
165 ) {
166 if let Some(child) = Self::child(app) {
167 App::shutdown(child, turn, driver);
168 }
169 }
170
171 fn finish(
172 app: pin::Pin<&mut Self>,
173 install: life::Install<'_, 'd>,
174 finish: &mut finish::Finish<'_, 'd, impl Report<Event = crate::Reported>>,
175 ) {
176 if let Some(child) = Self::child(app) {
177 App::finish(child, install, finish);
178 }
179 }
180}
181
182impl<'d> Layout<'d> for () {
183 type Scope = Infallible;
184 type ParkPlan = ();
185}
186
187unsafe impl<'d> App<'d> for () {
189 const ROUTES: settings::Routes = settings::Routes::EMPTY;
190}
191
192#[derive(Default)]
193pub struct Census {
194 parks: usize,
195 groups: usize,
196 coords: usize,
197}
198
199impl Census {
200 pub fn coordinate(&mut self) -> Result<(), alloc::Overflow> {
201 self.groups = self.groups.checked_add(1).ok_or(alloc::Overflow)?;
202 self.coords = self.coords.checked_add(1).ok_or(alloc::Overflow)?;
203 Ok(())
204 }
205
206 fn include_leaf(&mut self, coords: usize) -> Result<(), alloc::Overflow> {
207 self.parks = self.parks.checked_add(1).ok_or(alloc::Overflow)?;
208 self.groups = self
209 .groups
210 .checked_add(usize::from(coords != 0))
211 .ok_or(alloc::Overflow)?;
212 self.coords = self.coords.checked_add(coords).ok_or(alloc::Overflow)?;
213 Ok(())
214 }
215}
216
217#[derive(Default)]
218pub struct Flat {
219 parks: usize,
220 next: [usize; 2],
221 groups: u32,
222 coord: u32,
223}
224
225pub trait Plan: Sealed + Default {
226 fn flat(&mut self) -> Option<&mut Flat>;
227
228 fn prepare<'d, D: App<'d, ParkPlan = Self>>(
229 &mut self,
230 app: pin::Pin<&D>,
231 ) -> Result<Count, alloc::Overflow> {
232 Flat::census(self.flat(), app)
233 }
234}
235
236impl Plan for () {
237 fn flat(&mut self) -> Option<&mut Flat> {
238 None
239 }
240}
241
242impl Plan for Flat {
243 fn flat(&mut self) -> Option<&mut Flat> {
244 Some(self)
245 }
246}
247
248impl Flat {
249 fn census<'d, D: App<'d>>(
250 flat: Option<&mut Self>,
251 app: pin::Pin<&D>,
252 ) -> Result<Count, alloc::Overflow> {
253 let mut count = Census::default();
254 D::census(app, &mut count)?;
255 if let Some(flat) = flat {
256 flat.parks = count.parks;
257 flat.groups = count.groups as u32;
258 }
259 Ok(Count::new(count.coords))
260 }
261
262 pub(crate) unsafe fn run<'app, 'driver, 'turn, 'd: 'driver + 'turn, D: App<'d>>(
265 stored: &mut D::ParkPlan,
266 app: pin::Pin<&'app mut D>,
267 turn: schedule::Turn<'turn, 'd>,
268 driver: &'driver mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
269 ) {
270 let Some(plan) = stored.flat() else {
271 unsafe { D::park(app, turn, driver) };
273 return;
274 };
275 let ahead = [
276 start(&mut plan.next[0], turn.app().remaining(), plan.parks),
277 start(&mut plan.next[1], turn.maint().remaining(), plan.parks),
278 ];
279 let mut next = plan.coord as usize;
280 let coord = start(&mut next, turn.coord().remaining(), plan.groups as usize);
281 plan.coord = next as u32;
282 let mut sweep = schedule::raw::Sweep {
283 turn,
284 driver,
285 parks: plan.parks,
286 ahead,
287 groups: plan.groups,
288 coord: coord as u32,
289 };
290 unsafe { D::sweep(app, &mut sweep) };
292 }
293
294 pub(crate) unsafe fn resume<'d, D: App<'d>>(&self, app: pin::Pin<&D>) -> D::ParkPlan {
297 let mut resumed = D::ParkPlan::default();
298 if let Some(plan) = resumed.flat() {
299 let _ = Self::census(Some(plan), app);
300 for (next, prior) in plan.next.iter_mut().zip(self.next) {
301 if prior < plan.parks {
302 *next = prior;
303 }
304 }
305 if self.coord < plan.groups {
306 plan.coord = self.coord;
307 }
308 }
309 resumed
310 }
311}
312
313fn start(next: &mut usize, credits: usize, count: usize) -> usize {
314 if credits == 0 || count <= 1 {
315 return 0;
316 }
317 let at = *next;
318 let next_at = at.wrapping_add(1);
319 *next = if next_at == count { 0 } else { next_at };
320 at
321}
322
323pub unsafe trait Drain<'d>: App<'d> {
327 fn begin(
328 self: pin::Pin<&mut Self>,
329 turn: schedule::Turn<'_, 'd>,
330 driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
331 );
332
333 fn progress(
336 self: pin::Pin<&Self>,
337 region: ®ion::Token<region::Identity<'d>>,
338 state: Option<&mut schedule::Waits<'d>>,
339 ) -> ControlFlow<()>;
340
341 fn force(
342 self: pin::Pin<&mut Self>,
343 turn: schedule::Turn<'_, 'd>,
344 driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
345 );
346}
347
348#[derive(Clone, Copy, Debug)]
350pub enum Count {
351 Bounded(bounded::raw::U32<0, { schedule::LIMIT as u32 }>),
352 Excess(NonZero<usize>),
353}
354
355impl Count {
356 pub const fn new(value: usize) -> Self {
357 match bounded::raw::U32::from_usize(value) {
358 Some(value) => Self::Bounded(value),
359 None => Self::Excess(NonZero::<usize>::MIN.saturating_add(value - 1)),
360 }
361 }
362
363 pub const fn get(self) -> usize {
364 match self {
365 Self::Bounded(value) => value.into_usize(),
366 Self::Excess(value) => value.get(),
367 }
368 }
369
370 pub(in crate::executor) fn admit<E>(self) -> Result<(), executor::InstallError<E>> {
371 match self {
372 Self::Bounded(_) => Ok(()),
373 Self::Excess(count) => Err(executor::InstallError::Coordinators {
374 count,
375 limit: schedule::LIMIT,
376 }),
377 }
378 }
379}