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 WAKE_ROUTES: settings::Routes = T::WAKE_ROUTES;
56    const COORDS: usize = <T as executor::raw::App<'d>>::COORDS;
57    const PARK: bool = T::PARK;
58
59    fn select_park() -> Self::ParkPlan {
60        <T as executor::raw::App<'d>>::select_park()
61    }
62
63    fn prepare_park(
64        app: pin::Pin<&Self>,
65        plan: &mut Self::ParkPlan,
66    ) -> Option<executor::raw::Count> {
67        <T as executor::raw::App<'d>>::prepare_park(lending::raw::Project::shared(app), plan)
68    }
69
70    unsafe fn park<'app, 'driver, 'turn>(
71        app: pin::Pin<&'app mut Self>,
72        plan: &mut Self::ParkPlan,
73        park: executor::raw::Park<'driver, 'turn, 'd>,
74    ) where
75        'd: 'driver + 'turn,
76    {
77        // SAFETY: this owner preserves the same prepared plan and pinned projection.
78        unsafe { forward::park(app, plan, park) }
79    }
80
81    unsafe fn activate<'app, 'driver, 'turn>(
82        app: pin::Pin<&'app mut Self>,
83        activate: executor::raw::Activate<'driver, 'turn, 'd>,
84    ) -> bool
85    where
86        'd: 'driver + 'turn,
87    {
88        // SAFETY: this owner retains the installed application and its wake routes.
89        unsafe { forward::activate(app, activate) }
90    }
91
92    fn admit(
93        app: pin::Pin<&Self>,
94        needs: &mut settings::claim::Needs,
95    ) -> Result<(), settings::errors::Claim> {
96        T::admit(lending::raw::Project::shared(app), needs)
97    }
98
99    fn released(released: executor::raw::Released<'_, Self>) -> executor::raw::Released<'_, Self> {
100        // SAFETY: this wrapper retains the exact original pinned application.
101        unsafe { released.visit(lending::raw::Project::exclusive) }
102    }
103
104    fn install<'app, 'owner>(
105        call: executor::raw::Install<'app, 'owner, 'd, Self>,
106    ) -> executor::raw::Install<'app, 'owner, 'd, Self> {
107        forward::install(call)
108    }
109
110    fn dispatch<'event>(
111        app: pin::Pin<&mut Self>,
112        event: executor::raw::Dispatch<'event, 'd>,
113        turn: schedule::Turn<'_, 'd>,
114        driver: driver::Context<'_, 'd>,
115    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd>> {
116        forward::dispatch(app, event, turn, driver)
117    }
118
119    fn shutdown<'app, 'turn, 'driver>(
120        call: executor::raw::Shutdown<'app, 'turn, 'driver, 'd, Self>,
121    ) -> executor::raw::Shutdown<'app, 'turn, 'driver, 'd, Self> {
122        forward::shutdown(call)
123    }
124
125    fn finish<'app, 'finish, 'owner, 'epoch>(
126        call: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
127    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
128        <Self as executor::raw::Finishes<'d>>::call(call)
129    }
130
131    fn progress(
132        app: pin::Pin<&Self>,
133        view: executor::raw::Progress<'_, '_, 'd>,
134    ) -> schedule::Progress<'d> {
135        T::progress(lending::raw::Project::shared(app), view)
136    }
137
138    fn stopping(
139        app: pin::Pin<&Self>,
140        view: executor::raw::Progress<'_, '_, 'd>,
141    ) -> schedule::Progress<'d> {
142        T::stopping(lending::raw::Project::shared(app), view)
143    }
144}
145
146impl<'d, T> executor::raw::Finishes<'d, Self> for lending::raw::Borrowed<'_, T>
147where
148    T: executor::raw::App<'d>,
149{
150    fn call<'app, 'finish, 'owner, 'epoch>(
151        call: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
152    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
153        forward::finish(call)
154    }
155}
156
157impl<'d, T> executor::raw::Finishes<'d, Self> for Owner<mem::ManuallyDrop<T>, T>
158where
159    T: executor::raw::App<'d>,
160{
161    fn call<'app, 'finish, 'owner, 'epoch>(
162        call: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
163    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
164        let (mut app, install, finish) = forward::finish(call).into_parts();
165        // SAFETY: final traversal completed and this private lifecycle wrapper
166        // is never projected again before its enclosing application is destroyed.
167        unsafe { mem::ManuallyDrop::drop(app.as_mut().project().stored.get_unchecked_mut()) };
168        executor::raw::Finish::new(app, install, finish)
169    }
170}
171
172// SAFETY: every drain call projects the same pinned application; its Drain
173// contract preserves the admitted owners and route claims.
174unsafe impl<'d, S, T> executor::raw::Drain<'d> for Owner<S, T>
175where
176    S: lending::raw::Project<App = T>,
177    T: executor::raw::Drain<'d>,
178    Self: executor::raw::Finishes<'d, Self>,
179{
180    fn begin(
181        self: pin::Pin<&mut Self>,
182        turn: schedule::Turn<'_, 'd>,
183        driver: &mut driver::Context<'_, 'd>,
184    ) {
185        executor::raw::Drain::begin(lending::raw::Project::exclusive(self), turn, driver);
186    }
187
188    fn progress(
189        self: pin::Pin<&Self>,
190        region: &region::Token<region::Identity<'d>>,
191    ) -> schedule::Progress<'d> {
192        executor::raw::Drain::progress(lending::raw::Project::shared(self), region)
193    }
194
195    fn force(
196        self: pin::Pin<&mut Self>,
197        turn: schedule::Turn<'_, 'd>,
198        driver: &mut driver::Context<'_, 'd>,
199    ) {
200        executor::raw::Drain::force(lending::raw::Project::exclusive(self), turn, driver);
201    }
202}