dope-core 0.25.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::{driver::life::finish, io::fd::handles};
pub mod cancel;
mod complete;
pub(crate) mod raw;
mod request;
mod slots;
pub mod storage;
use std::{marker, mem};

pub(crate) use complete::Complete;
use dope_sys::{engine::admit::Cancelled, table::token::host};
use o3::{alloc, base::marker::ThreadBound};
pub(crate) use request::Request;
use route::table;
pub use slots::Slots;
use table::conn;

use crate::{
    driver::{
        self,
        route::{self, kind},
    },
    io::fd::domain::retire,
};

type Invariant<'d> = (driver::Invariant<'d>, marker::PhantomData<ThreadBound>);

const INDEX_BITS: u32 = route::SLOT_BITS;
const GENERATION_BITS: u32 = 32;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
struct Target {
    epoch: route::Epoch,
    slot: route::indices::SlotIndex,
    kind: kind::Native,
    route: route::Route,
}

impl Target {
    fn new<Tag: route::Tag, C: kind::Flight>(target: route::Operation<'_, Tag, C>) -> Self {
        let parts = target.parts();
        Self {
            epoch: parts.epoch(),
            slot: parts.slot(),
            kind: C::FLIGHT,
            route: Tag::ROUTE,
        }
    }

    fn token(self) -> route::Token {
        route::Token::from_flight(self.epoch, self.slot, self.kind, self.route)
    }

    fn bind<'d, C: kind::Class>(
        self,
        driver: driver::Invariant<'d>,
        flight: raw::Echo,
    ) -> route::Erased<'d, C> {
        route::Erased {
            token: self.token(),
            flight: Some(flight),
            driver,
            class: marker::PhantomData,
        }
    }
}

#[repr(transparent)]
pub(crate) struct Arena(storage::Flights<Target, INDEX_BITS, GENERATION_BITS>);

/// Exact kernel-flight capacity derived from an existing typed resource bound.
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(transparent)]
pub struct Capacity(usize);

/// Typed flight-group requirements retained until their shared preparation.
pub struct Need<Tags, const GROUPS: usize = 1> {
    caps: [Capacity; GROUPS],
    tags: marker::PhantomData<Tags>,
}

/// Exact connection scaling rejected by the kernel-flight slot bound.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum ScaleError {
    Product {
        capacity: conn::Capacity,
        factor: usize,
    },
    Limit {
        actual: usize,
        maximum: usize,
    },
}

impl Capacity {
    pub const fn fixed<const FLIGHTS: usize>() -> Self {
        const { assert!(FLIGHTS > 0 && FLIGHTS <= route::SLOT_MASK as usize + 1) };
        Self(FLIGHTS)
    }

    pub const fn table(capacity: table::Capacity) -> Self {
        Self(capacity.count().get())
    }

    pub const fn connections<const PER_CONNECTION: usize>(
        capacity: conn::Capacity,
    ) -> Result<Self, ScaleError> {
        const { assert!(PER_CONNECTION > 0) };
        let Some(total) = (capacity.raw() as usize).checked_mul(PER_CONNECTION) else {
            return Err(ScaleError::Product {
                capacity,
                factor: PER_CONNECTION,
            });
        };
        let maximum = route::SLOT_MASK as usize + 1;
        if total > maximum {
            return Err(ScaleError::Limit {
                actual: total,
                maximum,
            });
        }
        Ok(Self(total))
    }

    pub const fn capped<const MAX: usize>(capacity: table::Capacity) -> Self {
        const { assert!(MAX > 0) };
        let capacity = capacity.count().get();
        Self(if capacity < MAX { capacity } else { MAX })
    }

    pub(crate) const fn get(self) -> usize {
        self.0
    }
}

impl<Tag: route::Tag> Need<Tag> {
    pub const fn new(cap: Capacity) -> Self {
        Self {
            caps: [cap],
            tags: marker::PhantomData,
        }
    }
}

impl<A: route::Tag> Need<A> {
    pub fn pair<B: route::Tag>(self, other: Need<B>) -> Need<(A, B), 2> {
        let Self {
            caps: [first],
            tags: marker::PhantomData,
        } = self;
        let Need {
            caps: [second],
            tags: marker::PhantomData,
        } = other;
        Need {
            caps: [first, second],
            tags: marker::PhantomData,
        }
    }
}

impl<A: route::Tag, B: route::Tag> Need<(A, B), 2> {
    pub fn push<C: route::Tag>(self, other: Need<C>) -> Need<(A, B, C), 3> {
        let Self {
            caps: [first, second],
            tags: marker::PhantomData,
        } = self;
        let Need {
            caps: [third],
            tags: marker::PhantomData,
        } = other;
        Need {
            caps: [first, second, third],
            tags: marker::PhantomData,
        }
    }
}

impl<Tags, const GROUPS: usize> Need<Tags, GROUPS> {
    const fn into_caps(self) -> [Capacity; GROUPS] {
        self.caps
    }
}

#[must_use = "prepared flight slots must be committed"]
pub struct Prepared<'a, 'd, Tags, const GROUPS: usize = 1> {
    prepared: storage::Prepared<'a, 'd, Target, INDEX_BITS, GENERATION_BITS, GROUPS>,
    driver: driver::Invariant<'d>,
    tags: marker::PhantomData<Tags>,
}

#[must_use = "a validated flight vacancy must be reserved or released"]
pub(crate) struct Vacant<'slot, 'd, Tag: route::Tag, C: kind::Class = <Tag as route::Tag>::Class> {
    vacancy: storage::Vacant<'slot, 'd, Target, INDEX_BITS, GENERATION_BITS>,
    target: Target,
    driver: Invariant<'d>,
    class: marker::PhantomData<(Tag, C)>,
}

#[must_use = "a reserved flight must be committed or released"]
#[repr(transparent)]
pub(crate) struct Reserved<'d, C: kind::Class = kind::Any> {
    reservation: storage::Reserved<'d, Target, INDEX_BITS, GENERATION_BITS>,
    driver: Invariant<'d>,
    class: marker::PhantomData<C>,
}

/// Exact kernel-flight authority bound invariantly to its driver.
/// Dropping this owner preserves any pending native completion.
#[must_use = "a live flight must reach terminal completion or quiescence"]
#[repr(transparent)]
pub struct Flight<'d, C: kind::Class = kind::Any> {
    flight: storage::Lease<'d, Target, INDEX_BITS, GENERATION_BITS>,
    driver: Invariant<'d>,
    class: marker::PhantomData<C>,
}

pub(crate) struct Drain<'q, 'd> {
    drain: storage::Drain<'q, Target, INDEX_BITS, GENERATION_BITS>,
    driver: driver::Reference<'d>,
}

impl Arena {
    pub(crate) fn new() -> Self {
        Self(storage::Flights::new())
    }

    pub(in crate::driver) fn is_pending(&self) -> bool {
        self.0.is_pending()
    }

    pub(in crate::driver) fn finish(&self) {
        self.0.finish();
    }

    pub(crate) fn try_prepare<'a, 'd, Tags, const GROUPS: usize>(
        &'a mut self,
        need: Need<Tags, GROUPS>,
        driver: driver::Reference<'d>,
    ) -> Result<Prepared<'a, 'd, Tags, GROUPS>, alloc::Error> {
        raw::prepare(&mut self.0, need, driver)
    }
}

impl<'d, Tags, const GROUPS: usize> Prepared<'_, 'd, Tags, GROUPS> {
    fn commit(
        self,
    ) -> (
        [storage::Slots<'d, Target, INDEX_BITS, GENERATION_BITS>; GROUPS],
        Invariant<'d>,
    ) {
        let Self {
            prepared,
            driver,
            tags: marker::PhantomData,
        } = self;
        let slots = prepared.groups();
        (slots, (driver, marker::PhantomData))
    }
}

impl<'d, Tag: route::Tag> Prepared<'_, 'd, Tag> {
    pub fn slots(self) -> Slots<'d, Tag> {
        let ([slots], driver) = self.commit();
        Slots::new(slots, driver)
    }
}

impl<'d, A: route::Tag, B: route::Tag> Prepared<'_, 'd, (A, B), 2> {
    pub fn slots(self) -> (Slots<'d, A>, Slots<'d, B>) {
        let ([first, second], driver) = self.commit();
        (Slots::new(first, driver), Slots::new(second, driver))
    }
}

impl<'d, A: route::Tag, B: route::Tag, C: route::Tag> Prepared<'_, 'd, (A, B, C), 3> {
    pub fn slots(self) -> (Slots<'d, A>, Slots<'d, B>, Slots<'d, C>) {
        let ([first, second, third], driver) = self.commit();
        (
            Slots::new(first, driver),
            Slots::new(second, driver),
            Slots::new(third, driver),
        )
    }
}

impl<'d, Tag: route::Tag, C: kind::Class> Vacant<'_, 'd, Tag, C> {
    pub(crate) fn reserve(self) -> Reserved<'d, C> {
        Reserved {
            reservation: self.vacancy.reserve(self.target),
            driver: self.driver,
            class: marker::PhantomData,
        }
    }
}

impl<'d, C: kind::Class> Reserved<'d, C> {
    pub(crate) fn key(&self) -> raw::Echo {
        raw::Echo::from_inner(self.reservation.echo())
    }

    pub(crate) fn commit(self) -> Flight<'d, C> {
        Flight {
            flight: self.reservation.commit(),
            driver: self.driver,
            class: marker::PhantomData,
        }
    }
}

impl<'q, 'd> Drain<'q, 'd> {
    pub(crate) fn new(arena: &'q Arena, driver: driver::Reference<'d>) -> Self {
        Self {
            drain: arena.0.drain(),
            driver,
        }
    }
}

impl<'q, 'd> host::Drain for Drain<'q, 'd> {
    type Driver = driver::Reference<'d>;

    fn driver(&self) -> Self::Driver {
        self.driver
    }

    type Key = raw::Echo;
    type Returned = retire::Returned;
    type Flight = Complete<'q, 'd>;
    type Echo = retire::Echo<'d>;
    type Class = handles::Retirement;

    fn bind(&self, key: raw::Echo) -> Option<Complete<'q, 'd>> {
        self.drain.bind(key.into_inner()).map(|bound| Complete {
            completion: bound,
            driver: self.driver,
            class: marker::PhantomData,
        })
    }

    fn retired(&self, returned: Self::Returned) -> host::Closed<Self::Echo, Self::Class> {
        use host::Closed;
        match self.driver.descriptors().retirement().bind(returned) {
            retire::Bind::Ready(echo) => Closed::Ready(echo),
            retire::Bind::Class {
                echo,
                expected,
                observed,
            } => Closed::Class {
                echo,
                expected,
                observed,
            },
            retire::Bind::Missing => Closed::Missing,
        }
    }
}

impl<'d, C: kind::Class> Flight<'d, C> {
    pub(crate) fn is_pending(&self) -> bool {
        self.flight.is_pending()
    }

    pub(crate) fn key(&self) -> raw::Echo {
        raw::Echo::from_inner(self.flight.echo())
    }

    pub fn target(&self) -> route::Erased<'d, C> {
        self.flight.value().bind(self.driver.0, self.key())
    }

    pub fn matches(&self, target: route::Erased<'d, C>) -> bool {
        self.target().matches_flight(target)
    }

    pub(in crate::driver) fn finish(mut self, proof: &mut finish::Finish<'_, 'd>) {
        self.driver.0 = proof.brand();
    }
}

const _: () = {
    assert!(mem::size_of::<Arena>() == 4 * mem::size_of::<usize>());
    assert!(mem::align_of::<Arena>() == mem::align_of::<usize>());
    assert!(mem::size_of::<Target>() == mem::size_of::<route::Token>());
    assert!(mem::align_of::<Target>() == mem::align_of::<route::Token>());
    assert!(mem::size_of::<raw::Echo>() == mem::size_of::<usize>());
    assert!(mem::size_of::<Flight<'static>>() == mem::size_of::<usize>());
    assert!(mem::size_of::<Option<Flight<'static>>>() == mem::size_of::<usize>());
    assert!(mem::size_of::<Reserved<'static>>() == mem::size_of::<usize>());
    assert!(
        mem::size_of::<Vacant<'static, 'static, route::KeyTag<1>>>()
            == mem::size_of::<storage::Vacant<'static, 'static, Target, INDEX_BITS, GENERATION_BITS>>(
            ) + mem::size_of::<route::Token>()
    );
    assert!(mem::size_of::<Drain<'static, 'static>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<Complete<'static, 'static>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<Slots<'static, route::KeyTag<1>>>() == mem::size_of::<usize>());
};

pub(crate) struct Mismatch<'q, 'd> {
    pub(crate) bound: Complete<'q, 'd>,
    pub(crate) expected: kind::Native,
    pub(crate) observed: kind::Native,
}

impl<'d, C: kind::Class> Flight<'d, C> {
    /// Submits cancellation for this exact flight without releasing its owners.
    pub fn try_cancel(
        &mut self,
        driver: &mut driver::Context<'_, 'd>,
    ) -> Result<Cancelled, cancel::Error> {
        driver.native().try_cancel(self)
    }
}

impl<'d, C: kind::Class> Flight<'d, C> {
    /// Cancellation reserves no new flight, so capacity refusal is queue fullness.
    pub fn try_stop(
        &mut self,
        driver: &mut driver::Context<'_, 'd>,
    ) -> Result<cancel::Outcome, cancel::Failure> {
        let status = match self.try_cancel(driver) {
            Ok(status) => status,
            Err(cancel::Error::Full(_)) => {
                return Ok(cancel::Outcome::Deferred);
            }
            Err(cancel::Error::Rejected(cause)) => return Err(cause),
        };
        Ok(match status {
            Cancelled::Submitted => cancel::Outcome::Submitted,
            Cancelled::Queued => cancel::Outcome::Queued,
            Cancelled::Completed => cancel::Outcome::Completed,
        })
    }
}