dope-core 0.77.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
use std::{marker, mem, num, os::fd};

use dope_sys::{
    Errno, engine,
    engine::{
        admit,
        register::Interest,
        ring::{
            Context,
            config::{Build, Config},
        },
        shutdown::Shutdown,
    },
    io::{
        file,
        reactor::{
            complete::{self, flags::Flow},
            event::{fault, kind},
            read, ready,
            ready::{
                delivery::{Decoded, entry, entry::Event as _},
                store,
                store::{Exec, config, submit},
            },
            retry,
        },
        request::{self, Source},
        socket::{Prepare, option::tuning},
    },
    memory::{buffer, buffer::provided},
    probe, ring, table,
    table::{change, permit, token, token::host},
};
use o3::{base::num::bounded, buffer::bytes::bounded::MAX, depot::map::Plan};

use crate::{
    driver::{
        life::native::{self, Receive},
        receive,
        result::poll::Cause,
        retained::flight,
        route::{self, indices::SlotIndex, kind::Class, table::Capacity},
        schedule::ready::Stored,
        settings,
    },
    io::{
        event::net::accept,
        fd::handles,
        socket::{option, pending},
        transfer,
    },
};

mod files;

type Done<N> = <N as entry::Source>::Event<Host<N>, Received<N>>;
type ReadyPoll<N> = <N as ready::Source>::Poll<files::Slots, Cause>;
type PollState<N> = <N as ready::Source>::State<Host<N>>;
type DoneQueue<N> = <N as complete::Source>::Queue<Done<N>, settings::Completion>;
type Vacancy<'a, N> = <DoneQueue<N> as complete::Queue>::Vacancy<'a>;
type Trace<N> =
    <N as complete::Observe<fault::Echo<flight::Echo>, handles::PhysicalSlot, Flow>>::Observation;
type Files<N> = <N as table::Source>::Files<files::Slots>;
type Reader<'a, N> = <N as read::Source>::Arm<'a, files::Slots, Done<N>, ReadyPoll<N>>;
type ReadSlot<N> = <N as read::Source>::Entry<files::Slots>;
type Registry<N> = <N as read::Source>::Registry<files::Slots>;
type Data<N> = <N as retry::Source>::Send<files::Slots>;
type Pending<N> = <N as retry::Source>::Pending<files::Slots>;
type Writes<N> = <N as retry::Source>::State<Pending<N>>;
type Retry<'a, N> = <N as retry::Source>::Retry<'a, files::Slots, Done<N>, ReadyPoll<N>>;
type Pool<N> = <N as buffer::Source>::Pool<Receive>;
type Received<N> = <Pool<N> as buffer::Pool>::Buffer;
type Permits<N> = <N as permit::Source>::Queue<Stored, receive::Index>;

/// Binds a selected native owner to this driver's typed completion contracts.
#[derive(Debug)]
pub(crate) struct Host<N>(marker::PhantomData<N>);

impl<N: Prepare> entry::Context for Host<N> {
    type Reader = files::Slots;
    type Writer = files::Slots;
    type Outbound = handles::OutboundSlot;
    type Handle = N::Handle;
    type Failure = accept::Failure;

    const LAST: Flow = Flow::Last;
    const MORE: Flow = Flow::More;
}

impl<N: ready::Source + read::Source + retry::Source> store::Context for Host<N> {
    type Slots = files::Slots;
    type Files = Files<N>;
    type Read = ReadSlot<N>;
    type Reads = Registry<N>;
    type Pending = Pending<N>;
    type Writes = Writes<N>;
    type Poll = ReadyPoll<N>;
    type Pool = Pool<N>;
}

impl<N> store::Exec for Host<N>
where
    N: ready::Source + read::Source + retry::Source + entry::Source + Prepare,
{
    type Key = flight::Echo;
    type Reader = files::Slots;
    type Writer = files::Slots;
    type Event = Done<N>;
    type Send = Data<N>;
    type Arm<'a>
        = Reader<'a, N>
    where
        Self: 'a;
    type Retry<'a>
        = Retry<'a, N>
    where
        Self: 'a;
    type ReadPoll<'a>
        = <N as read::Source>::Poll<'a, files::Slots, Receive, Done<N>, ReadyPoll<N>>
    where
        Self: 'a;
}

impl<N> ready::raw::Decode for Host<N>
where
    N: ready::Source + read::Source + retry::Source + entry::Source + Prepare,
{
    unsafe fn decode(raw: usize) -> Decoded<flight::Echo> {
        if raw == Udata::READABLE.0 {
            return Decoded::Shutdown(Interest::Readable);
        }
        if raw == Udata::CLOSED.0 {
            return Decoded::Shutdown(Interest::Closed);
        }
        match <native::Context as host::Context>::read_key(raw as u64) {
            Some(key) => Decoded::Public(key),
            None => Decoded::Empty,
        }
    }

    const SHUTDOWN: Done<N> = Done::<N>::SHUTDOWN;
}

#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub(crate) struct Udata(usize);

impl Udata {
    const READABLE: Self = Self(usize::MAX);
    const CLOSED: Self = Self(usize::MAX - 1);
}

impl From<flight::Echo> for Udata {
    fn from(key: flight::Echo) -> Self {
        const { assert!(flight::Echo::BITS < usize::BITS) };
        Self(usize::from(key))
    }
}

impl From<Udata> for usize {
    fn from(data: Udata) -> Self {
        data.0
    }
}

#[derive(Debug)]
pub(crate) struct File<N>(flight::Echo, marker::PhantomData<N>);

impl<N: buffer::Source + entry::Source + Prepare> request::Complete for File<N> {
    type Output = Done<N>;

    fn opened(self, result: Result<fd::OwnedFd, Errno>) -> Self::Output {
        <Done<N> as entry::Event<Host<N>>>::opened(self.0, result)
    }

    fn write(self, result: Result<bounded::raw::U32<0, { MAX as u32 }>, Errno>) -> Self::Output {
        let result = result.map(transfer::Len::from_bound);
        Done::<N>::written(self.0, result)
    }

    fn read(self, result: Result<bounded::raw::U32<0, { MAX as u32 }>, Errno>) -> Self::Output {
        let result = result.map(transfer::Len::from_bound);
        Done::<N>::read(self.0, result)
    }

    fn sync(self, result: Result<(), Errno>) -> Self::Output {
        Done::<N>::synced(self.0, result)
    }
}

impl<N> admit::Context for Host<N>
where
    N: file::File + Source<native::Context> + admit::Native,
{
    type Native = N;
    type Inputs = native::Context;
    type Key = flight::Echo;
    type Failure = Cause;
}

impl<N> submit::Context for Host<N>
where
    N: buffer::Source + entry::Source + Prepare,
    Self: ready::Driver<Context: Exec<Key = flight::Echo, Event = Done<N>>>,
{
    type Outbound = handles::OutboundSlot;
    type Handle = N::Handle;
    type Created = Done<N>;
    type File = File<N>;

    fn file(key: flight::Echo) -> Self::File {
        File(key, marker::PhantomData)
    }
}

impl<N> ready::Driver for Host<N>
where
    N: ready::Source
        + read::Source
        + retry::Source
        + entry::Source
        + complete::Source
        + permit::Source
        + Source<native::Context>
        + Prepare
        + complete::Observe<fault::Echo<flight::Echo>, handles::PhysicalSlot, Flow>,
{
    type Observation = Trace<N>;
    type Context = Host<N>;
    type State = PollState<N>;
    type Queue = DoneQueue<N>;

    fn shutdown(interest: Interest) -> Udata {
        let () = Self::LAYOUT;
        match interest {
            Interest::Readable => Udata::READABLE,
            Interest::Closed => Udata::CLOSED,
        }
    }
}

impl<N> config::Config<settings::Config> for Host<N>
where
    Self: ready::Driver<
            Context: store::Context<
                Pool: buffer::Pool<Layout = Receive>,
                Slots: table::raw::Slots<Layout = settings::FileSlots>,
            >,
            Queue: complete::Queue<Capacity = settings::Completion>,
        >,
{
    type Key = Stored;
    type Index = receive::Index;

    fn config(config: &settings::Config) -> config::Build<Self> {
        let slots = config.file_slots();
        config::Build {
            receive: Receive(config.receive()),
            slots,
            plan: Plan::from_bounded(slots.bound()),
            capacity: config.queue_layout().completion(),
        }
    }
}

impl<N> Context for Host<N>
where
    N: tuning::Source + provided::Source + ring::Source + token::Source,
    N: complete::Observe<<N as token::Source>::Token<native::Context>>,
{
    type Observation = <N as complete::Observe<Self::Token>>::Observation;
    type Token = <N as token::Source>::Token<native::Context>;
    type Layout = Receive;
    type Tuning = N::Table<flight::Echo, { option::MAX_STREAM_OPTIONS }>;
}

impl<N> Config<settings::Config> for Host<N>
where
    N: probe::Source
        + provided::Source
        + permit::Source
        + token::Source
        + engine::Source<Self, Backend: engine::Buffer>,
    Self: Context<Layout = Receive>,
{
    type Key = Stored;
    type Index = receive::Index;

    fn config(config: &settings::Config) -> Build<Self::Layout> {
        let () = Self::RING;
        let queues = config.queue_layout();
        let files = config.file_slots();
        Build {
            submit: num::NonZero::<u32>::MIN.saturating_add(queues.submission().get() - 1),
            complete: num::NonZero::<u32>::MIN.saturating_add(queues.completion().get() - 1),
            defer: matches!(config.progress(), settings::Progress::BatchedWhenSupported),
            files: files.bound(),
            range: files.accept_slot().map(SlotIndex::position),
            receive: Receive(config.receive()),
        }
    }
}

impl<N> Host<N>
where
    N: probe::Source
        + provided::Source
        + permit::Source
        + token::Source
        + engine::Source<Self, Backend: engine::Buffer>,
{
    const RING: () = {
        type Received<N> = <N as provided::Source>::Context<Receive>;
        type Producer<N> = <N as provided::Source>::Producer<Receive>;
        type Canary<N> = <N as provided::Source>::Canary<Receive>;
        /// Registered close retains one fd, authority, and two mappings through unregister.
        /// Its exclusive phases preserve the exact unregister or unmap failure.
        /// Producer storage, tuning, and receive context remain owned until release.
        type Closing<N> = <<N as engine::Source<Host<N>>>::Backend as Shutdown>::State;
        let word = mem::size_of::<usize>();
        assert!(usize::BITS >= u32::BITS);
        assert!(mem::size_of::<Received<N>>() == 2 * word + mem::size_of::<Receive>());
        assert!(mem::size_of::<Producer<N>>() == 5 * word);
        assert!(mem::size_of::<(N::Index, settings::Buffers)>() == mem::size_of::<u32>());
        assert!(mem::align_of::<(N::Index, settings::Buffers)>() == mem::align_of::<u16>());
        assert!(
            mem::size_of::<Canary<N>>()
                == mem::size_of::<Received<N>>() + mem::size_of::<Producer<N>>()
        );
        assert!(
            mem::size_of::<Permits<N>>()
                == (3 * mem::size_of::<u32>()).next_multiple_of(mem::align_of::<usize>()) + word
        );
        assert!(mem::align_of::<Permits<N>>() == mem::align_of::<usize>());
        assert!(mem::size_of::<Closing<N>>() == 13 * word);
        assert!(mem::align_of::<Closing<N>>() == mem::align_of::<usize>());
        assert!(mem::size_of::<Result<(), Closing<N>>>() == mem::size_of::<Closing<N>>());
    };
}

impl<N> Host<N>
where
    N: ready::Source
        + read::Source
        + retry::Source
        + entry::Source
        + complete::Source
        + permit::Source
        + Source<native::Context>
        + Prepare
        + complete::Observe<fault::Echo<flight::Echo>, handles::PhysicalSlot, Flow>,
{
    const LAYOUT: () = {
        type Changes<N> = <Files<N> as table::Files<files::Slots>>::Changes;
        let word = mem::size_of::<usize>();
        assert!(mem::size_of::<Permits<N>>() == 0);
        assert!(mem::align_of::<Permits<N>>() == 1);
        assert!(mem::size_of::<DoneQueue<N>>() == 3 * word);
        assert!(mem::size_of::<Vacancy<'_, N>>() == word);
        assert!(mem::size_of::<Done<N>>() == 4 * word);
        assert!(mem::size_of::<complete::Dequeued<Done<N>>>() == 5 * word);
        assert!(mem::size_of::<Option<complete::Dequeued<Done<N>>>>() == 5 * word);
        assert!(
            mem::size_of::<PollState<N>>()
                == mem::size_of::<(Writes<N>, Files<N>, ReadyPoll<N>, Registry<N>, Pool<N>)>()
        );
        assert!(mem::size_of::<Data<N>>() == 2 * word);
        assert!(mem::size_of::<<Changes<N> as change::Changes>::Id>() == mem::size_of::<u64>());
        assert!(mem::size_of::<<Changes<N> as change::Changes>::Tail<'_>>() == word);
        assert!(
            mem::size_of::<Files<N>>()
                == mem::size_of::<(
                    Box<[Option<<Files<N> as table::Files<files::Slots>>::Handle>]>,
                    Capacity,
                    o3::depot::index::set::State,
                    Capacity,
                    Changes<N>
                )>()
        );
        assert!(mem::size_of::<Trace<N>>() == 3 * word);
    };
}

const _: () = {
    assert!(
        mem::size_of::<Option<std::os::fd::OwnedFd>>()
            <= mem::size_of::<Option<std::os::fd::RawFd>>()
    );
    assert!(mem::size_of::<usize>() == mem::size_of::<u64>());
};

impl<'slot, 'd, N, Tag, C>
    admit::raw::Flight<(
        &'slot flight::raw::Slots<'d, Tag>,
        route::Operation<'d, Tag, C>,
    )> for Host<N>
where
    Tag: route::Tag + route::Select<C>,
    C: Class + kind::Flight,
{
    type Front = flight::raw::Front<'slot, 'd, Tag, C>;
    type Reserved = flight::Reserved<'d, C>;
    type Output = flight::Flight<'d, C>;
    type Error = pending::Cause;

    unsafe fn front(
        (slots, target): (
            &'slot flight::raw::Slots<'d, Tag>,
            route::Operation<'d, Tag, C>,
        ),
    ) -> Result<Self::Front, Self::Error> {
        // SAFETY: native submission keeps this group's free-list loan exclusive.
        unsafe { slots.front(target) }.map_err(|cause| pending::Cause::Submit(cause.into()))
    }
}