dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use std::{cell::Cell, num, time};

use dope_sys::engine;
use o3::{
    base::num::Epoch,
    buffer::{self, storage::provided::native},
};

use crate::{
    Engine,
    driver::{
        Seed,
        route::{self, queues},
        schedule::{
            self,
            clock::{timer, timer::heap},
            ready::wake::resident::slot,
            sequence,
        },
    },
};

pub(crate) struct Shared<B = <Engine as engine::Buffer>::Context> {
    pub(in crate::driver) random_seed: Seed,
    pub(in crate::driver) schedule: schedule::State,
    pub(in crate::driver) receive: crate::Permits,
    pub(in crate::driver) buffer: B,
    pub(in crate::driver) retire: queues::Pending,
    pub(in crate::driver) generations: [Cell<Option<Epoch>>; route::CAPACITY],
    pub(in crate::driver) sequence: sequence::Owner,
    pub(in crate::driver) timer: heap::raw::Store<time::Instant, slot::Slot, timer::Tag>,
}

impl<B> Shared<B> {
    pub(in crate::driver) const fn random_seed(&self) -> &Seed {
        &self.random_seed
    }

    pub(crate) fn buffer(&self) -> &B {
        &self.buffer
    }

    pub(in crate::driver) fn timer(
        &self,
    ) -> &heap::raw::Store<time::Instant, slot::Slot, timer::Tag> {
        &self.timer
    }
}

impl<T, B: buffer::raw::Fixed<T>> buffer::raw::Fixed<T> for Shared<B> {
    fn resident(&self) -> num::NonZero<u32> {
        buffer::raw::Fixed::resident(&self.buffer)
    }

    unsafe fn project<'a, 'id>(
        &'a self,
        item: &'a mut T,
        limit: buffer::raw::Limit<'id>,
    ) -> buffer::raw::Projection<'a, 'id> {
        // SAFETY: Shared retains this exact native context beside its arena;
        // the caller supplies its uniquely owned completed item and resident limit.
        unsafe { buffer::raw::Fixed::project(&self.buffer, item, limit) }
    }
}

// SAFETY: Shared keeps this exact native mapping and admitted receive geometry
// alive until the driver has ended every native and issued buffer access.
unsafe impl<B: native::raw::Context> native::raw::Context for Shared<B> {
    fn resident(&self) -> num::NonZero<u32> {
        self.buffer.resident()
    }

    fn buffer_ptr(&self, bid: u16) -> *mut u8 {
        self.buffer.buffer_ptr(bid)
    }
}