dope-core 0.22.0

Owned native I/O and completion driver for dope
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::{marker, mem, os::fd, pin};

use dope_sys::memory::Context as _;
pub(in crate::driver) use epochs::Final;
use finish::Finish;
use o3::{
    base::{
        cell::{brand, region},
        marker::ThreadBound,
    },
    buffer,
    buffer::storage::{provided, provided::fixed},
};
use plans::{Build, Create, Direct, Provided};
use schedule::state::Core;
use state::scope;

use crate::{
    Engine, driver,
    driver::{
        life::recovery::{Observe, Recover},
        retained::flight,
        route::lease::Registry,
        schedule,
        schedule::state,
        storage,
        storage::{resident, resident::Alloc},
    },
    io,
};

pub mod creation;
pub mod domain;
pub mod finish;
pub(crate) mod native;
mod plans;
pub mod recovery;

/// Application completions still require ordinary driver dispatch before startup.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Busy;

/// Owner-bound primitives for generated installation roots.
pub mod raw;

/// Runs once inside the generation which owns a built storage output and source.
pub trait Operation<S: storage::Plan, Q: ?Sized>: Sized {
    type Output;

    fn run<'a, 'p, 'd>(
        self,
        scope: Scope<'a, 'd>,
        storage: storage::Loan<'a, 'p, 'd, S>,
        source: &'a Q,
    ) -> Self::Output
    where
        'd: 'p,
        'p: 'a,
        'd: 'a,
        Self: 'd,
        S: 'd,
        Q: 'd;
}

/// Mutable driver-local authority owned by one generative scope.
/// `driver::Context` lends this root as one pointer.
pub(crate) struct Local<'d> {
    pub(crate) driver: driver::Reference<'d>,
    pub(crate) backend: fixed::Loan<'d, Engine>,
    pub(in crate::driver) retry: bool,
    pub(crate) flights: &'d mut flight::Arena,
    pub(in crate::driver) resident: Alloc<'d>,
    pub(in crate::driver) routes: Registry,
    pub(in crate::driver) region: (
        region::Token<region::Identity<'d>>,
        region::Token<region::Identity<'d, 1>>,
    ),
    pub(crate) lifecycle: Lifecycle<'d>,
}

/// Generation-owned source and the policy responsible for its transitions.
pub(crate) struct Lifecycle<'d> {
    pub(crate) source: Option<fd::BorrowedFd<'d>>,
    pub(crate) resuming: bool,
    pub(crate) pending: bool,
    pub(crate) report: &'d mut dyn FnMut(io::Reclaimed),
    pub(in crate::driver) recover: &'d dyn recovery::Recover,
}

impl Lifecycle<'_> {
    pub(crate) fn report(&mut self, event: io::Reclaimed) {
        if matches!(event, io::Reclaimed::Shutdown) {
            let owned = self.source.take().is_some();
            if self.resuming && owned {
                self.pending = true;
                return;
            }
        }
        (self.report)(event);
    }
}

pub(in crate::driver) struct State<'d> {
    local: Local<'d>,
    work: Core<'d>,
    token: brand::Token<brand::Identity<'d>>,
}

impl<'d> State<'d> {
    fn new(local: Local<'d>, token: brand::Token<brand::Identity<'d>>) -> Self {
        Self {
            work: Core::new(local.driver),
            local,
            token,
        }
    }
}

mod epochs;

pub(in crate::driver) struct Lease<'a> {
    driver: pin::Pin<&'a mut driver::Driver>,
}

/// One lexical view of the hidden owner for an exact driver generation.
///
#[repr(transparent)]
pub struct Scope<'a, 'd> {
    state: &'a mut State<'d>,
}

impl<'a, 'd> Scope<'a, 'd> {
    fn new(state: &'a mut State<'d>) -> Self {
        Self { state }
    }

    pub fn context(&mut self) -> driver::Context<'_, 'd> {
        let state = &mut *self.state;
        driver::Context::new(&mut state.local)
    }

    pub fn token(&mut self) -> &mut brand::Token<brand::Identity<'d>> {
        &mut self.state.token
    }

    /// Runs with the sole schedule authority for this driver scope.
    pub fn with_turn<R>(
        &mut self,
        run: impl for<'scope> FnOnce(
            &'scope mut brand::Token<brand::Identity<'d>>,
            driver::Context<'scope, 'd>,
            scope::Scope<'scope, 'd>,
        ) -> R,
    ) -> R {
        let state = &mut *self.state;
        let ctx = driver::Context::new(&mut state.local);
        run(&mut state.token, ctx, scope::Scope::new(&mut state.work))
    }

    /// Runs a driver turn that performs no branded application projection.
    pub fn with_driver<R>(
        &mut self,
        run: impl for<'scope> FnOnce(driver::Context<'scope, 'd>, scope::Scope<'scope, 'd>) -> R,
    ) -> R {
        let state = &mut *self.state;
        let ctx = driver::Context::new(&mut state.local);
        run(ctx, scope::Scope::new(&mut state.work))
    }

    pub fn driver_ref(&self) -> driver::Reference<'d> {
        self.state.local.driver
    }

    /// Admits startup polling before any application completion can be emitted.
    /// Keep exclusive scope authority and do not submit operations during startup.
    pub fn try_boot(&self) -> Result<(), Busy> {
        if self.state.local.flights.is_pending() {
            Err(Busy)
        } else {
            Ok(())
        }
    }

    /// Drains every retained operation before captured owners may be released.
    /// A failed native transition retains its owner and invokes the fatal policy.
    /// Only successful native quiescence permits the borrowed scope to return.
    pub fn quiesce(&mut self) {
        let state = &mut *self.state;
        let ctx = driver::Context::new(&mut state.local);
        Final::suspend(ctx).resume()
    }

    /// Quiesces before lending finish authority, then reaps the released owner.
    /// A failed transition invokes the fatal policy with the exact native owner.
    #[doc(hidden)]
    pub fn finish<R>(
        &mut self,
        forced: bool,
        run: impl for<'finish> FnOnce(
            &mut brand::Token<brand::Identity<'d>>,
            &mut Finish<'finish, 'd>,
        ) -> R,
    ) -> R {
        let state = &mut *self.state;
        let ctx = driver::Context::new(&mut state.local);
        let last = Final::suspend(ctx);
        let mut finish = Finish::new(last, forced);
        let output = run(&mut state.token, &mut finish);
        finish.resume();
        output
    }
}

impl<'a> Lease<'a> {
    pub(crate) fn take(driver: pin::Pin<&'a mut driver::Driver>) -> Self {
        Self { driver }
    }

    fn run_with<S, Q: ?Sized, O>(
        self,
        plan: S,
        source: &domain::Source<Q>,
        op: O,
        report: &mut dyn Observe,
    ) -> O::Output
    where
        S: storage::Direct,
        O: Operation<S, Q>,
    {
        self.scope(
            source,
            Direct {
                plan,
                operation: op,
            },
            report,
        )
    }

    fn try_run_with<S, Q: ?Sized, O>(
        self,
        plan: S,
        source: &domain::Source<Q>,
        op: O,
        report: &mut dyn Observe,
    ) -> Result<O::Output, domain::Rejected<S::Error, O>>
    where
        S: storage::Build,
        O: Operation<S, Q>,
    {
        self.scope(
            source,
            Build {
                plan,
                operation: op,
            },
            report,
        )
    }

    fn try_create<S, Q: ?Sized, O>(
        self,
        plan: S,
        source: &domain::Source<Q>,
        op: O,
        report: &mut dyn Observe,
    ) -> Result<O::Output, domain::Rejected<S::Error, O>>
    where
        S: storage::Create,
        O: Operation<S, Q>,
    {
        self.scope(
            source,
            Create {
                plan,
                operation: op,
            },
            report,
        )
    }

    fn run_scope<A, Q: ?Sized, O>(
        self,
        arenas: A,
        source: &domain::Source<Q>,
        op: O,
        report: &mut dyn Observe,
    ) -> O::Output
    where
        A: buffer::raw::Arenas,
        O: Operation<storage::Scope<A>, Q>,
    {
        let runtime = &mut self.driver.get_mut().runtime;
        let (report, recover) = report.split();
        let root = Root {
            flights: &mut runtime.flights,
            resident: &mut runtime.resident,
            source,
            report,
            recover,
        };
        runtime.shared.run(
            pin::pin!((root, arenas)),
            runtime.shared.context().buffer().layout().0.accounts(),
            &mut runtime.backend,
            Provided(op),
        )
    }

    fn scope<'b, Q: ?Sized, O>(
        self,
        source: &'b domain::Source<Q>,
        op: O,
        report: &'b mut dyn Observe,
    ) -> O::Output
    where
        'a: 'b,
        O: fixed::Operation<Root<'b, Q>, driver::Buffer, driver::Shared, Terminal = crate::Engine>,
    {
        let runtime = &mut self.driver.get_mut().runtime;
        let (report, recover) = report.split();
        let root = Root {
            flights: &mut runtime.flights,
            resident: &mut runtime.resident,
            source,
            report,
            recover,
        };
        runtime.shared.run(
            pin::pin!(root),
            runtime.shared.context().buffer().layout().0.accounts(),
            &mut runtime.backend,
            op,
        )
    }
}

const _: () = {
    assert!(mem::size_of::<Scope<'static, 'static>>() == mem::size_of::<&mut ()>());
    assert!(mem::align_of::<Scope<'static, 'static>>() == mem::align_of::<&mut ()>());
};

struct Root<'a, Q: ?Sized> {
    flights: &'a mut flight::Arena,
    resident: &'a mut resident::Arena,
    source: &'a domain::Source<Q>,
    report: &'a mut dyn FnMut(io::Reclaimed),
    recover: &'a dyn Recover,
}

impl<Q: ?Sized> Root<'_, Q> {
    fn enter<'d>(
        &'d mut self,
        shared: provided::Root<'d, driver::Buffer, driver::Shared, ()>,
        token: brand::Token<brand::Identity<'d>>,
        regions: (
            region::Token<region::Identity<'d>>,
            region::Token<region::Identity<'d, 1>>,
        ),
        backend: fixed::Loan<'d, Engine>,
    ) -> (State<'d>, &'d Q) {
        let alloc = self.resident.enter(&regions.0);
        let local = Local {
            driver: driver::Reference::new(shared),
            backend,
            retry: false,
            flights: &mut *self.flights,
            resident: alloc,
            routes: Registry::new(),
            region: regions,
            lifecycle: Lifecycle {
                source: self.source.descriptor(),
                resuming: false,
                pending: false,
                report: &mut *self.report,
                recover: self.recover,
            },
        };
        let state = State::new(local, token);
        (state, &self.source.value)
    }
}

/// Proof that nested resources are installed beneath their pinned owner.
#[must_use]
pub struct Install<'a, 'd> {
    owner: marker::PhantomData<(&'a mut raw::Anchor<'a>, ThreadBound)>,
    _driver: driver::Invariant<'d>,
}

impl<'a, 'd> Install<'a, 'd> {
    /// Binds installation to `root` and its driver generation.
    pub fn new<T: raw::Root<'a>>(root: T, driver: driver::Reference<'d>) -> Self {
        Self {
            owner: root.anchor().owner(),
            _driver: driver.brand,
        }
    }
}

const _: () = {
    assert!(mem::size_of::<Install<'static, 'static>>() == 0);
    assert!(mem::align_of::<Install<'static, 'static>>() == 1);
};