Skip to main content

dope_core/executor/
mod.rs

1use std::{convert, marker, num::NonZero, ops::ControlFlow};
2
3use crate::driver::{
4    life,
5    life::{creation, domain},
6    settings, storage,
7};
8
9use o3::base::marker::ThreadBound;
10
11pub mod boot;
12pub mod create;
13pub mod drain;
14pub mod drive;
15pub mod raw;
16pub mod root;
17pub mod session;
18#[doc(hidden)]
19pub mod work;
20pub trait Op<S: storage::Plan, Q: Startup, Report: crate::io::Report>: Sized {
21    type Output;
22
23    fn run<'scope, 'life, 'p, 'd>(
24        self,
25        storage: storage::Loan<'life, 'p, 'd, S>,
26        session: session::Session<'scope, 'life, 'd, Q, Report>,
27    ) -> Self::Output
28    where
29        'd: 'p,
30        'p: 'life,
31        'life: 'scope,
32        Self: 'd,
33        S: 'd,
34        Q: 'd;
35}
36
37struct Entry<O>(O);
38
39impl<S, Q, O, Report: crate::io::Report> life::Operation<S, Q, Report> for Entry<O>
40where
41    S: storage::Plan,
42    Q: Startup,
43    O: Op<S, Q, Report>,
44{
45    type Output = O::Output;
46
47    fn rejected(&self, source: &Q) {
48        source.failed();
49    }
50
51    fn run<'life, 'p, 'd>(
52        self,
53        scope: life::Scope<'life, 'd, Report>,
54        storage: storage::Loan<'life, 'p, 'd, S>,
55        source: &'life Q,
56    ) -> Self::Output
57    where
58        'd: 'p,
59        'p: 'life,
60        Self: 'd,
61        S: 'd,
62        Q: 'd,
63    {
64        let mut core = session::Core::new(scope, source);
65        self.0.run(storage, session::Session::new(&mut core))
66    }
67}
68
69pub fn enter<'owner, S, Q, O, Report: crate::io::Report>(
70    generation: domain::Generation<Q>,
71    teardown: &'owner mut domain::Teardown,
72    storage: S,
73    operation: O,
74    report: &mut Report,
75) -> Result<O::Output, domain::Error<'owner, convert::Infallible, O::Output, Q>>
76where
77    S: storage::Direct,
78    Q: Startup,
79    O: Op<S, Q, Report>,
80{
81    generation
82        .start(teardown)
83        .enter(storage, Entry(operation), report)
84}
85
86pub fn scope<'owner, A, Q, O, Report: crate::io::Report>(
87    generation: domain::Generation<Q>,
88    teardown: &'owner mut domain::Teardown,
89    storage: storage::Scope<A>,
90    operation: O,
91    report: &mut Report,
92) -> Result<O::Output, domain::Error<'owner, convert::Infallible, O::Output, Q>>
93where
94    A: o3::buffer::raw::Arenas,
95    Q: Startup,
96    O: Op<storage::Scope<A>, Q, Report>,
97{
98    generation
99        .start(teardown)
100        .scope(storage, Entry(operation), report)
101}
102
103/// Lifecycle behavior selected by an executor's shutdown source.
104pub trait Startup<Q = Self>: Sized {
105    type Error;
106    const INTEREST: crate::dope_sys::engine::register::Interest =
107        crate::dope_sys::engine::register::Interest::Readable;
108
109    /// Publishes installation readiness or preserves its exact notification failure.
110    fn installed(&self) -> Result<(), Self::Error> {
111        Ok(())
112    }
113
114    /// Observes the registered shutdown source becoming ready.
115    fn observed(&self, _request: Requested<Q>) -> ControlFlow<()> {
116        ControlFlow::Break(())
117    }
118
119    /// Records installation failure before native cleanup.
120    fn failed(&self) {}
121}
122
123impl Startup for () {
124    type Error = convert::Infallible;
125}
126
127#[must_use]
128pub struct Requested<Q>(marker::PhantomData<(fn(Q) -> Q, ThreadBound)>);
129
130impl<Q> Requested<Q> {
131    pub(crate) const fn new() -> Self {
132        Self(marker::PhantomData)
133    }
134}
135
136#[derive(Clone, Copy, Debug, Eq, PartialEq)]
137pub enum InstallError<E = convert::Infallible> {
138    Claim(settings::errors::Claim),
139    Coordinators { count: NonZero<usize>, limit: usize },
140    CoordinatorCountOverflow,
141    Startup(E),
142}
143
144impl<E> From<settings::errors::Claim> for InstallError<E> {
145    fn from(error: settings::errors::Claim) -> Self {
146        Self::Claim(error)
147    }
148}
149
150pub trait Build: Sized {
151    type Shutdown: Startup;
152
153    fn try_build(
154        self,
155        attempt: &mut creation::Attempt,
156        config: settings::Config,
157    ) -> Result<domain::Generation<Self::Shutdown>, create::Error<'_>>;
158}
159
160impl Build for () {
161    type Shutdown = ();
162
163    fn try_build(
164        self,
165        attempt: &mut creation::Attempt,
166        config: settings::Config,
167    ) -> Result<domain::Generation, create::Error<'_>> {
168        Ok(domain::Generation::new(attempt.try_driver(config)?))
169    }
170}
171
172pub fn try_enter<'owner, S, Q, O, Report: crate::io::Report>(
173    generation: domain::Generation<Q>,
174    teardown: &'owner mut domain::Teardown,
175    storage: S,
176    operation: O,
177    report: &mut Report,
178) -> Result<O::Output, domain::Error<'owner, domain::Rejected<S::Error, O>, O::Output, Q>>
179where
180    S: storage::Build,
181    Q: Startup,
182    O: Op<S, Q, Report>,
183{
184    generation
185        .start(teardown)
186        .try_enter(storage, Entry(operation), report)
187        .map_err(|error| error.map_storage(|rejected| rejected.map_op(|entry| entry.0)))
188}