Skip to main content

dope_core/executor/
root.rs

1use std::{pin, task::Poll};
2
3use crate::executor::task::{self, context};
4
5use crate::driver::{
6    self,
7    schedule::{app, ready},
8};
9
10/// Runtime poll capability tying one exact root and driver to a poll.
11pub struct Context<'poll, 'd: 'poll, R> {
12    root: pin::Pin<&'poll mut R>,
13    wake: ready::Target<'d>,
14    work: app::Work<'poll, 'd>,
15    driver: &'poll mut driver::Local<'d>,
16}
17
18/// Remaining application-lane credits for one root poll.
19#[derive(Clone, Copy)]
20pub struct Budget<'poll, 'd> {
21    wake: ready::Target<'d>,
22    work: app::Work<'poll, 'd>,
23}
24
25impl<'poll, 'd, R> Context<'poll, 'd, R> {
26    pub(crate) fn new(
27        root: pin::Pin<&'poll mut R>,
28        wake: ready::Target<'d>,
29        work: app::Work<'poll, 'd>,
30        driver: &'poll mut driver::Local<'d>,
31    ) -> Self {
32        Self {
33            root,
34            wake,
35            work,
36            driver,
37        }
38    }
39
40    pub fn into_parts(
41        self,
42    ) -> (
43        pin::Pin<&'poll mut R>,
44        ready::Target<'d>,
45        app::Work<'poll, 'd>,
46        &'poll mut driver::Local<'d>,
47    ) {
48        (self.root, self.wake, self.work, self.driver)
49    }
50
51    pub fn into_poll(
52        self,
53    ) -> (
54        pin::Pin<&'poll mut R>,
55        Budget<'poll, 'd>,
56        &'poll mut driver::Local<'d>,
57    ) {
58        (self.root, Budget::new(self.wake, self.work), self.driver)
59    }
60}
61
62impl<'poll, 'd> Budget<'poll, 'd> {
63    pub(crate) const fn new(wake: ready::Target<'d>, work: app::Work<'poll, 'd>) -> Self {
64        Self { wake, work }
65    }
66
67    pub fn target(self) -> ready::Target<'d> {
68        self.wake
69    }
70
71    /// Reserves one more application transition, requeueing the root when its
72    /// current application-lane share is exhausted.
73    pub fn try_continue(self) -> Option<app::Permit<'poll, 'd>> {
74        let permit = self.work.permit();
75        if permit.is_none() {
76            self.wake.wake();
77        }
78        permit
79    }
80}
81
82/// Transient pinned control-flow root.
83/// Driver authority retains the application through terminal quiescence.
84pub trait Root<'d>: Sized {
85    const ADMITTED: bool = false;
86    type Output;
87    fn poll(context: Context<'_, 'd, Self>) -> Poll<Self::Output>;
88}
89
90impl<'d, F: task::Fiber<'d>> Root<'d> for F {
91    type Output = F::Output;
92    const ADMITTED: bool = true;
93
94    fn poll(call: Context<'_, 'd, Self>) -> Poll<Self::Output> {
95        let (fiber, target, work, driver) = call.into_parts();
96        let mut context = context::Context::new(target, work, driver);
97        F::poll(context::PollCall::from_root(fiber, &mut context))
98    }
99}