Skip to main content

dope_runtime/executor/raw/
park.rs

1use std::mem;
2
3use dope_core::driver::{
4    self,
5    schedule::{self, clock},
6};
7use o3::mem::quota;
8
9use crate::{control, executor};
10
11/// Pre-park authority for one exact retained root and scheduler turn.
12#[must_use]
13pub struct Park<'driver, 'turn, 'd> {
14    drive: executor::raw::Drive<'driver, 'turn, 'd>,
15}
16
17impl<'driver, 'turn, 'd> Park<'driver, 'turn, 'd> {
18    pub(crate) const fn new(
19        turn: schedule::Turn<'turn, 'd>,
20        driver: driver::Context<'driver, 'd>,
21    ) -> Self {
22        Self {
23            drive: executor::raw::Drive { turn, driver },
24        }
25    }
26
27    pub fn reborrow(&mut self) -> Park<'_, '_, 'd> {
28        Park {
29            drive: self.drive.reborrow(),
30        }
31    }
32
33    pub fn into_parts(self) -> (schedule::Turn<'turn, 'd>, driver::Context<'driver, 'd>) {
34        let executor::raw::Drive { turn, driver } = self.drive;
35        (turn, driver)
36    }
37
38    pub fn into_driver(self) -> driver::Context<'driver, 'd> {
39        self.drive.driver
40    }
41
42    pub fn app_cursor<const N: usize>(
43        &self,
44        cursor: &mut executor::raw::Cursor<N>,
45    ) -> executor::raw::Cursor<N> {
46        cursor.start(self.drive.turn.app().remaining())
47    }
48
49    pub fn maint_cursor<const N: usize>(
50        &self,
51        cursor: &mut executor::raw::Cursor<N>,
52    ) -> executor::raw::Cursor<N> {
53        cursor.start(self.drive.turn.maint().remaining())
54    }
55
56    pub fn coord_cursor<const N: usize>(
57        &self,
58        cursor: &mut executor::raw::Cursor<N>,
59    ) -> executor::raw::Cursor<N> {
60        cursor.start(self.drive.turn.coord().remaining())
61    }
62
63    pub fn coord_left(&self) -> usize {
64        self.drive.turn.coord().remaining()
65    }
66
67    pub fn share<const N: usize, R>(
68        &mut self,
69        ranks: [usize; 3],
70        weights: [usize; 2],
71        run: impl FnOnce(Park<'_, '_, 'd>) -> R,
72    ) -> R {
73        self.drive.turn.share::<N, _>(ranks, weights, |turn| {
74            let drive = executor::raw::Drive {
75                turn,
76                driver: self.drive.driver.reborrow(),
77            };
78            run(Park { drive })
79        })
80    }
81
82    pub fn share_coord<const N: usize, R>(
83        &mut self,
84        rank: usize,
85        weights: [usize; 2],
86        run: impl FnOnce(Park<'_, '_, 'd>) -> R,
87    ) -> R {
88        self.drive
89            .turn
90            .coord()
91            .share::<N, _>(rank, weights, || run(self.reborrow()))
92    }
93
94    /// Monotonic time captured once at the beginning of this driver turn.
95    pub fn now(&mut self) -> clock::Moment<'d> {
96        self.drive.driver.reborrow().driver_ref().clock().now()
97    }
98
99    /// Admits one step whose clock and region authorities share this borrow.
100    pub fn next_step(&mut self) -> Option<control::Step<'_, 'd>> {
101        match self.drive.turn.reborrow().coord().take() {
102            quota::Take::Taken => {}
103            quota::Take::Exhausted => return None,
104        }
105        let driver = self.drive.driver.reborrow();
106        Some(control::Step::new(driver))
107    }
108}
109
110const fn assert_layout() {
111    type Turn<'a> = Park<'a, 'a, 'a>;
112
113    assert!(mem::size_of::<Turn<'static>>() == 2 * mem::size_of::<usize>());
114    assert!(mem::align_of::<Turn<'static>>() == mem::align_of::<usize>());
115}
116
117const _: () = assert_layout();