Skip to main content

dope_core/executor/raw/
plan.rs

1use std::pin;
2
3use crate::driver::{self, schedule};
4
5use super::{App, Count};
6
7#[derive(Default)]
8pub struct Census {
9    parks: usize,
10    groups: usize,
11    coords: usize,
12}
13
14impl Census {
15    pub fn coordinate(&mut self) -> Option<()> {
16        self.groups = self.groups.checked_add(1)?;
17        self.coords = self.coords.checked_add(1)?;
18        Some(())
19    }
20
21    pub(super) fn include_leaf(&mut self, coords: usize) -> Option<()> {
22        self.parks = self.parks.checked_add(1)?;
23        self.groups = self.groups.checked_add(usize::from(coords != 0))?;
24        self.coords = self.coords.checked_add(coords)?;
25        Some(())
26    }
27}
28
29#[derive(Default)]
30pub struct Flat {
31    parks: usize,
32    next: [usize; 2],
33    groups: u32,
34    coord: u32,
35}
36
37mod sealed {
38    pub trait Plan {}
39    impl Plan for () {}
40    impl Plan for super::Flat {}
41}
42
43pub trait Plan: sealed::Plan + Default {
44    fn prepare<'d, D: App<'d, ParkPlan = Self>>(&mut self, app: pin::Pin<&D>) -> Option<Count>;
45
46    /// # Safety
47    /// Run the same admitted pinned root with its unchanged census and turn.
48    unsafe fn run<'app, 'driver, 'turn, 'd, D>(
49        &mut self,
50        app: pin::Pin<&'app mut D>,
51        turn: schedule::Turn<'turn, 'd>,
52        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
53    ) where
54        'd: 'driver + 'turn,
55        D: App<'d, ParkPlan = Self>;
56
57    /// # Safety
58    /// Resume the unchanged head after its joined root has finished natively.
59    /// An unadmitted joined root must finish without running this plan.
60    unsafe fn resume<'d, D: App<'d, ParkPlan = Self>>(app: pin::Pin<&D>, prior: &Flat) -> Self;
61}
62
63impl Plan for () {
64    fn prepare<'d, D: App<'d, ParkPlan = Self>>(&mut self, app: pin::Pin<&D>) -> Option<Count> {
65        let mut count = Census::default();
66        D::census(app, &mut count)?;
67        Some(Count::new(count.coords))
68    }
69
70    unsafe fn run<'app, 'driver, 'turn, 'd, D>(
71        &mut self,
72        app: pin::Pin<&'app mut D>,
73        turn: schedule::Turn<'turn, 'd>,
74        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
75    ) where
76        'd: 'driver + 'turn,
77        D: App<'d, ParkPlan = Self>,
78    {
79        // SAFETY: the installed owner carries this root's admitted turn.
80        unsafe { D::park(app, turn, driver) };
81    }
82
83    unsafe fn resume<'d, D: App<'d, ParkPlan = Self>>(_: pin::Pin<&D>, _: &Flat) -> Self {}
84}
85
86impl Plan for Flat {
87    fn prepare<'d, D: App<'d, ParkPlan = Self>>(&mut self, app: pin::Pin<&D>) -> Option<Count> {
88        let mut count = Census::default();
89        D::census(app, &mut count)?;
90        self.parks = count.parks;
91        // Admission bounds every coordinator group by the same total count.
92        self.groups = count.groups as u32;
93        Some(Count::new(count.coords))
94    }
95
96    unsafe fn run<'app, 'driver, 'turn, 'd, D>(
97        &mut self,
98        app: pin::Pin<&'app mut D>,
99        turn: schedule::Turn<'turn, 'd>,
100        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
101    ) where
102        'd: 'driver + 'turn,
103        D: App<'d, ParkPlan = Self>,
104    {
105        let ahead = [
106            start(&mut self.next[0], turn.app().remaining(), self.parks),
107            start(&mut self.next[1], turn.maint().remaining(), self.parks),
108        ];
109        let mut next = self.coord as usize;
110        let coord = start(&mut next, turn.coord().remaining(), self.groups as usize);
111        self.coord = next as u32;
112        let mut sweep = Sweep {
113            turn,
114            driver,
115            parks: self.parks,
116            ahead,
117            groups: self.groups,
118            coord: coord as u32,
119        };
120        // SAFETY: preparation admitted the same pinned traversal and its counts.
121        unsafe { D::sweep(app, &mut sweep) };
122    }
123
124    unsafe fn resume<'d, D: App<'d, ParkPlan = Self>>(app: pin::Pin<&D>, prior: &Flat) -> Self {
125        let mut plan = Self::default();
126        // A live head is an unchanged subset of the admitted joined root.
127        let _ = plan.prepare(app);
128        plan.next = [
129            if prior.next[0] < plan.parks {
130                prior.next[0]
131            } else {
132                0
133            },
134            if prior.next[1] < plan.parks {
135                prior.next[1]
136            } else {
137                0
138            },
139        ];
140        plan.coord = if prior.coord < plan.groups {
141            prior.coord
142        } else {
143            0
144        };
145        plan
146    }
147}
148
149fn start(next: &mut usize, credits: usize, count: usize) -> usize {
150    if credits == 0 || count <= 1 {
151        return 0;
152    }
153    let at = *next;
154    let following = at.wrapping_add(1);
155    *next = if following == count { 0 } else { following };
156    at
157}
158
159pub struct Sweep<'driver, 'turn, 'd, Report> {
160    pub(crate) turn: schedule::Turn<'turn, 'd>,
161    pub(crate) driver: &'driver mut driver::life::Lifecycle<'d, Report>,
162    pub(crate) parks: usize,
163    pub(crate) ahead: [usize; 2],
164    pub(crate) groups: u32,
165    pub(crate) coord: u32,
166}