Skip to main content

dope_core/topology/lending/raw/
mod.rs

1use dope_sys::io::reactor::event::Report;
2use std::{marker, mem, ops::ControlFlow, pin};
3
4use crate::driver::{self, life, life::finish::Finish, schedule};
5use crate::{client, executor, topology::lending};
6use o3::base::cell::region;
7
8mod project;
9mod resume;
10
11/// Lifecycle storage borrowing the exact pinned provider.
12#[repr(transparent)]
13pub struct Borrowed<'a, T>(pin::Pin<&'a mut T>);
14
15impl<'loan, O> Borrowed<'loan, O> {
16    pub(crate) const fn new(owner: pin::Pin<&'loan mut O>) -> Self {
17        Self(owner)
18    }
19
20    pub fn root_client<'d>(&self) -> <O::Provider as client::Provider<'d>>::Client<'loan>
21    where
22        'd: 'loan,
23        O: lending::Root<'d>,
24        O::Provider: 'loan,
25    {
26        let provider = lending::Root::provider(self.0.as_ref());
27        client::Provider::provide(client::Context::<'_, 'loan, 'd, _>::new(provider))
28    }
29}
30pub use project::Project;
31pub use resume::Resume;
32
33// SAFETY: the owned pair forwards each lifecycle operation to both exact pinned
34// roots. Each root independently permits resume after stopped native quiescence;
35// the pair adds no operations or cleanup awaiting dispatch between them.
36unsafe impl<'d, A, B> Resume<'d> for crate::client::Pair<'d, A, B>
37where
38    A: lending::Root<'d> + Resume<'d>,
39    B: lending::Root<'d> + Resume<'d>,
40{
41}
42
43/// A dependent application retained by its pinned provider owner.
44#[pin_project::pin_project]
45#[derive(dope_gen::Application)]
46#[dispatcher(core = crate, driver = 'd, lending = true)]
47pub struct Bound<'app, 'd, H, T> {
48    #[dispatcher(state)]
49    driver: marker::PhantomData<driver::Reference<'d>>,
50    #[pin]
51    #[app]
52    head: pin::Pin<&'app mut H>,
53    #[pin]
54    #[app]
55    tail: mem::ManuallyDrop<T>,
56}
57
58impl<'app, H, T> Bound<'app, '_, H, T> {
59    pub(crate) const fn new(head: Borrowed<'app, H>, tail: T) -> Self {
60        const {
61            assert!(mem::size_of::<Self>() == mem::size_of::<(pin::Pin<&'app mut H>, T)>());
62            assert!(mem::align_of::<Self>() == mem::align_of::<(pin::Pin<&'app mut H>, T)>());
63            assert!(!mem::needs_drop::<Self>());
64        }
65        Self {
66            driver: marker::PhantomData,
67            head: head.0,
68            tail: mem::ManuallyDrop::new(tail),
69        }
70    }
71
72    pub fn tail(self: pin::Pin<&Self>) -> pin::Pin<&T> {
73        Project::shared(self.project_ref().tail)
74    }
75}
76
77impl<'d, H, T> lending::Root<'d> for Bound<'_, 'd, H, T>
78where
79    T: lending::Root<'d>,
80{
81    type Provider = T::Provider;
82
83    fn provider(self: pin::Pin<&Self>) -> pin::Pin<&Self::Provider> {
84        lending::Root::provider(Project::shared(self.project_ref().tail))
85    }
86}
87
88// SAFETY: cooperative drain projects the same pinned dependent each time.
89// Both applications remain installed through native quiescence and finish.
90unsafe impl<'d, H, T> executor::raw::Drain<'d> for Bound<'_, 'd, H, T>
91where
92    Self: executor::raw::App<'d>,
93    T: executor::raw::Drain<'d>,
94{
95    fn begin(
96        self: pin::Pin<&mut Self>,
97        turn: schedule::Turn<'_, 'd>,
98        driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
99    ) {
100        executor::raw::Drain::begin(Project::exclusive(self.project().tail), turn, driver);
101    }
102
103    fn progress(
104        self: pin::Pin<&Self>,
105        region: &region::Token<region::Identity<'d>>,
106        state: Option<&mut schedule::Waits<'d>>,
107    ) -> ControlFlow<()> {
108        executor::raw::Drain::progress(self.tail(), region, state)
109    }
110
111    fn force(
112        self: pin::Pin<&mut Self>,
113        turn: schedule::Turn<'_, 'd>,
114        driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
115    ) {
116        executor::raw::Drain::force(Project::exclusive(self.project().tail), turn, driver);
117    }
118}
119
120impl<'d, H, T> Bound<'_, 'd, H, T>
121where
122    H: executor::raw::App<'d>,
123    T: executor::raw::App<'d>,
124{
125    /// # Safety
126    /// Consume this exact native-quiesced owner; never project or finish its backing again.
127    pub(crate) unsafe fn finish_tail(
128        app: pin::Pin<&mut Self>,
129        install: life::Install<'_, 'd>,
130        finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
131        plan: &executor::raw::Flat,
132    ) -> <H as executor::raw::Layout<'d>>::ParkPlan {
133        let app = app.project();
134        let mut tail = app.tail;
135        T::finish(Project::exclusive(tail.as_mut()), install, finish);
136        // SAFETY: the dependent finished natively before releasing this exact storage.
137        unsafe { Project::release(tail) };
138        let head = Project::shared(app.head.as_ref());
139        // SAFETY: only the dependent was destroyed; the same pinned head is ready to resume.
140        unsafe { plan.resume(head) }
141    }
142}
143
144const _: () = {
145    assert!(mem::size_of::<Bound<'_, '_, (), ()>>() == mem::size_of::<&mut ()>());
146    assert!(mem::align_of::<Bound<'_, '_, (), ()>>() == mem::align_of::<&mut ()>());
147    assert!(!mem::needs_drop::<Bound<'_, '_, (), ()>>());
148};
149
150/// # Safety
151/// Finish once after native completion, preserve the pinned head, and leave outer storage release to the caller.
152pub unsafe trait Head<'d>: Sized {
153    type Joined<T: executor::raw::App<'d>>: executor::raw::App<'d>;
154    type Retired;
155    type Time<C: Copy>;
156
157    fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T>;
158
159    /// # Safety
160    /// Retire this installed root once after native quiescence, with its own install and park plan.
161    unsafe fn retire<T, C: Copy>(
162        app: pin::Pin<&mut Self::Joined<T>>,
163        install: life::Install<'_, 'd>,
164        finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
165        plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
166        source: C,
167    ) -> (Self::Retired, Self::Time<C>)
168    where
169        T: executor::raw::App<'d>;
170}
171
172unsafe impl<'d> Head<'d> for () {
173    type Joined<T: executor::raw::App<'d>> = T;
174    type Retired = ();
175    type Time<C: Copy> = ();
176
177    fn join<T: executor::raw::App<'d>>(self, tail: T) -> T {
178        tail
179    }
180
181    unsafe fn retire<T, C: Copy>(
182        app: pin::Pin<&mut T>,
183        install: life::Install<'_, 'd>,
184        finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
185        _: &mut T::ParkPlan,
186        _: C,
187    ) -> ((), ())
188    where
189        T: executor::raw::App<'d>,
190    {
191        T::finish(app, install, finish);
192        ((), ())
193    }
194}
195
196unsafe impl<'parent, 'd: 'parent, H> Head<'d> for Borrowed<'parent, H>
197where
198    H: Resume<'d>,
199{
200    type Joined<T: executor::raw::App<'d>> = Bound<'parent, 'd, H, T>;
201    type Retired = H::ParkPlan;
202    type Time<C: Copy> = C;
203
204    fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T> {
205        Bound::new(self, tail)
206    }
207
208    unsafe fn retire<T, C: Copy>(
209        app: pin::Pin<&mut Self::Joined<T>>,
210        install: life::Install<'_, 'd>,
211        finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
212        plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
213        source: C,
214    ) -> (Self::Retired, C)
215    where
216        T: executor::raw::App<'d>,
217    {
218        (
219            unsafe { Bound::finish_tail(app, install, finish, plan) },
220            source,
221        )
222    }
223}