Skip to main content

dope_runtime/topology/lending/raw/
owner.rs

1use std::{marker, mem, ops, pin};
2
3use dope_core::driver::{self, schedule, settings};
4use o3::base::cell::region;
5
6use super::forward;
7use crate::{executor, topology::lending};
8
9#[pin_project::pin_project]
10#[repr(transparent)]
11pub struct Owner<S, T> {
12    #[pin]
13    stored: S,
14    app: marker::PhantomData<T>,
15}
16
17impl<S, T> Owner<S, T> {
18    pub(super) const fn new(stored: S) -> Self {
19        Self {
20            stored,
21            app: marker::PhantomData,
22        }
23    }
24}
25
26// SAFETY: the pinned stored projection remains unchanged for the entire lifecycle.
27unsafe impl<S: lending::raw::Project<App = T>, T> lending::raw::Project for Owner<S, T> {
28    type App = T;
29
30    fn shared(self: pin::Pin<&Self>) -> pin::Pin<&Self::App> {
31        self.project_ref().stored.shared()
32    }
33
34    fn exclusive(self: pin::Pin<&mut Self>) -> pin::Pin<&mut Self::App> {
35        self.project().stored.exclusive()
36    }
37}
38
39impl<'d, S, T> executor::raw::Layout<'d> for Owner<S, T>
40where
41    T: executor::raw::Layout<'d>,
42{
43    type ParkPlan = <T as executor::raw::Layout<'d>>::ParkPlan;
44}
45
46// SAFETY: lifecycle traversal retains one exact pinned application. Its storage
47// is released once after final traversal, before its enclosing owner is finished.
48unsafe impl<'d, S, T> executor::raw::App<'d> for Owner<S, T>
49where
50    S: lending::raw::Project<App = T>,
51    T: executor::raw::App<'d>,
52    Self: executor::raw::Finishes<'d, Self>,
53{
54    const ROUTES: settings::Routes = T::ROUTES;
55    const DISPATCH_ROUTES: settings::Routes = T::DISPATCH_ROUTES;
56    const WAKE_ROUTES: settings::Routes = T::WAKE_ROUTES;
57    const COORDS: usize = <T as executor::raw::App<'d>>::COORDS;
58    const PARK: bool = T::PARK;
59
60    fn select_park() -> Self::ParkPlan {
61        <T as executor::raw::App<'d>>::select_park()
62    }
63
64    fn prepare_park(
65        app: pin::Pin<&Self>,
66        plan: &mut Self::ParkPlan,
67    ) -> Option<executor::raw::Count> {
68        <T as executor::raw::App<'d>>::prepare_park(lending::raw::Project::shared(app), plan)
69    }
70
71    unsafe fn park<'app, 'driver, 'turn>(
72        app: pin::Pin<&'app mut Self>,
73        plan: &mut Self::ParkPlan,
74        park: executor::raw::Park<'driver, 'turn, 'd>,
75    ) where
76        'd: 'driver + 'turn,
77    {
78        // SAFETY: this owner preserves the same prepared plan and pinned projection.
79        unsafe { forward::park(app, plan, park) }
80    }
81
82    unsafe fn activate<'app, 'driver, 'turn>(
83        app: pin::Pin<&'app mut Self>,
84        activate: executor::raw::Activate<'driver, 'turn, 'd>,
85    ) -> bool
86    where
87        'd: 'driver + 'turn,
88    {
89        // SAFETY: this owner retains the installed application and its wake routes.
90        unsafe { forward::activate(app, activate) }
91    }
92
93    fn admit(
94        app: pin::Pin<&Self>,
95        needs: &mut settings::claim::Needs,
96    ) -> Result<(), settings::errors::Claim> {
97        T::admit(lending::raw::Project::shared(app), needs)
98    }
99
100    fn released(released: executor::raw::Released<'_, Self>) -> executor::raw::Released<'_, Self> {
101        // SAFETY: this wrapper retains the exact original pinned application.
102        unsafe { released.visit(lending::raw::Project::exclusive) }
103    }
104
105    fn install<'app, 'owner>(
106        call: executor::raw::Install<'app, 'owner, 'd, Self>,
107    ) -> executor::raw::Install<'app, 'owner, 'd, Self> {
108        forward::install(call)
109    }
110
111    fn dispatch<'event>(
112        app: pin::Pin<&mut Self>,
113        event: executor::raw::Dispatch<'event, 'd>,
114        turn: schedule::Turn<'_, 'd>,
115        driver: driver::Context<'_, 'd>,
116    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd>> {
117        Self::dispatch_from::<false>(|| app, event, turn, driver)
118    }
119
120    fn dispatch_from<'app, 'event, const NESTED: bool>(
121        project: impl FnOnce() -> pin::Pin<&'app mut Self>,
122        event: executor::raw::Dispatch<'event, 'd>,
123        turn: schedule::Turn<'_, 'd>,
124        driver: driver::Context<'_, 'd>,
125    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd>>
126    where
127        Self: 'app,
128    {
129        T::dispatch_from::<NESTED>(
130            executor::raw::project_field(project, lending::raw::Project::exclusive),
131            event,
132            turn,
133            driver,
134        )
135    }
136
137    fn shutdown<'app, 'turn, 'driver>(
138        call: executor::raw::Shutdown<'app, 'turn, 'driver, 'd, Self>,
139    ) -> executor::raw::Shutdown<'app, 'turn, 'driver, 'd, Self> {
140        forward::shutdown(call)
141    }
142
143    fn finish<'app, 'finish, 'owner, 'epoch>(
144        call: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
145    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
146        <Self as executor::raw::Finishes<'d>>::call(call)
147    }
148
149    fn progress(
150        app: pin::Pin<&Self>,
151        view: executor::raw::Progress<'_, '_, 'd>,
152    ) -> schedule::Progress<'d> {
153        T::progress(lending::raw::Project::shared(app), view)
154    }
155
156    fn stopping(
157        app: pin::Pin<&Self>,
158        view: executor::raw::Progress<'_, '_, 'd>,
159    ) -> schedule::Progress<'d> {
160        T::stopping(lending::raw::Project::shared(app), view)
161    }
162}
163
164impl<'d, T> executor::raw::Finishes<'d, Self> for lending::raw::Borrowed<'_, T>
165where
166    T: executor::raw::App<'d>,
167{
168    fn call<'app, 'finish, 'owner, 'epoch>(
169        call: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
170    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
171        forward::finish(call)
172    }
173}
174
175impl<'d, T> executor::raw::Finishes<'d, Self> for Owner<mem::ManuallyDrop<T>, T>
176where
177    T: executor::raw::App<'d>,
178{
179    fn call<'app, 'finish, 'owner, 'epoch>(
180        call: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
181    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
182        let (mut app, install, finish) = forward::finish(call).into_parts();
183        // SAFETY: final traversal completed and this private lifecycle wrapper
184        // is never projected again before its enclosing application is destroyed.
185        unsafe { mem::ManuallyDrop::drop(app.as_mut().project().stored.get_unchecked_mut()) };
186        executor::raw::Finish::new(app, install, finish)
187    }
188}
189
190// SAFETY: every drain call projects the same pinned application; its Drain
191// contract preserves the admitted owners and route claims.
192unsafe impl<'d, S, T> executor::raw::Drain<'d> for Owner<S, T>
193where
194    S: lending::raw::Project<App = T>,
195    T: executor::raw::Drain<'d>,
196    Self: executor::raw::Finishes<'d, Self>,
197{
198    fn begin(
199        self: pin::Pin<&mut Self>,
200        turn: schedule::Turn<'_, 'd>,
201        driver: &mut driver::Context<'_, 'd>,
202    ) {
203        executor::raw::Drain::begin(lending::raw::Project::exclusive(self), turn, driver);
204    }
205
206    fn progress(
207        self: pin::Pin<&Self>,
208        region: &region::Token<region::Identity<'d>>,
209    ) -> schedule::Progress<'d> {
210        executor::raw::Drain::progress(lending::raw::Project::shared(self), region)
211    }
212
213    fn force(
214        self: pin::Pin<&mut Self>,
215        turn: schedule::Turn<'_, 'd>,
216        driver: &mut driver::Context<'_, 'd>,
217    ) {
218        executor::raw::Drain::force(lending::raw::Project::exclusive(self), turn, driver);
219    }
220}