Skip to main content

dope_core/topology/lending/raw/
mod.rs

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