dope-core 0.76.0

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

use o3::{alloc, base::cell::region, mem::quota};

use crate::{
    driver,
    driver::{
        receive::{credit, credit::storage},
        route::table::conn,
        settings,
    },
};

pub mod clock;
pub mod timing;

pub mod app;
pub mod maint;
pub mod raw;
pub mod ready;
pub mod sequence;
#[doc(hidden)]
pub mod state;

pub const LIMIT: usize = 256;

pub struct Domain<'d, Lane = ()>(
    marker::PhantomData<&'d mut &'d ()>,
    marker::PhantomData<fn(Lane) -> Lane>,
);

/// Exact result of acquiring scheduler capacity from a non-empty source.
#[must_use = "capacity reports whether the scheduler granted the operation"]
pub enum Capacity<T> {
    Available(T),
    Exhausted,
}

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Turn<'turn, 'd> {
    state: &'turn state::Core<'d>,
}

impl<'turn, 'd> Turn<'turn, 'd> {
    pub(in crate::driver::schedule) const fn new(state: &'turn state::Core<'d>) -> Self {
        Self { state }
    }

    pub fn reborrow(&self) -> Turn<'_, 'd> {
        Turn::new(self.state)
    }

    pub fn clock(self) -> clock::Clock<'turn, 'd> {
        self.state.driver.scheduler().clock()
    }

    pub fn app(self) -> app::Work<'turn, 'd> {
        app::Work { state: self.state }
    }

    pub fn maint(self) -> maint::Work<'turn, 'd> {
        maint::Work { state: self.state }
    }

    pub fn next_step<'step>(
        self,
        driver: &'step mut driver::Local<'d>,
    ) -> Option<crate::control::Step<'step, 'd>> {
        self.coord().take().ok()?;
        Some(crate::control::Step::new(driver))
    }

    #[doc(hidden)]
    pub fn coord(self) -> state::Coord<'turn, 'd> {
        state::Coord::new(self.state)
    }
}

const _: () = {
    let word = mem::size_of::<usize>();
    assert!(mem::size_of::<Turn<'static, 'static>>() == word);
    assert!(mem::size_of::<quota::Admission<usize>>() == 2 * word);
    assert!(mem::size_of::<Capacity<&'static u8>>() == word);
};

/// Aggregate liveness of retained work owned by one driver.
#[derive(Default)]
#[must_use]
pub struct Waits<'d>(Option<Wait<'d>>);

impl<'d> Waits<'d> {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn as_ref(&self) -> Option<&Wait<'d>> {
        self.0.as_ref()
    }

    pub fn event(&mut self, proof: region::Identity<'d>) {
        if self.0.is_none() {
            self.0 = Some(Wait::Event(proof));
        }
    }

    pub fn until(&mut self, deadline: clock::Deadline<'d>) {
        match &mut self.0 {
            Some(Wait::Until(prior)) => {
                if deadline < *prior {
                    *prior = deadline;
                }
            }
            wait => *wait = Some(Wait::Until(deadline)),
        }
    }
}

const _: () = assert!(mem::size_of::<Waits<'static>>() <= 24);

pub(in crate::driver) struct State {
    pub(in crate::driver) owner: storage::Owner<ready::Stored, credit::Wake, ready::Arena>,
    clock: cell::Cell<time::Instant>,
}

impl State {
    pub(in crate::driver) fn try_new(plan: settings::Plan<'_>) -> Result<Self, alloc::Error> {
        Ok(Self {
            owner: storage::Owner::try_new(plan)?,
            clock: cell::Cell::new(time::Instant::now()),
        })
    }
}

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Context<'d>(driver::Reference<'d>);

impl<'d> Context<'d> {
    pub(in crate::driver) const fn new(driver: driver::Reference<'d>) -> Self {
        Self(driver)
    }

    pub(in crate::driver) fn clock(self) -> clock::Clock<'d, 'd> {
        clock::Clock::new(&self.0.shared.context().schedule.clock, self.0.brand)
    }

    pub(in crate::driver) fn set_turn_now(self, now: time::Instant) {
        self.0.shared.context().schedule.clock.set(now);
    }

    /// Allocates fixed retained-credit storage in this driver generation.
    pub fn try_storage(
        self,
        capacity: conn::Capacity,
    ) -> Result<credit::Storage<'d>, alloc::Error> {
        let shared: &'d driver::Shared = self.0.shared.context();
        let owner = &shared.schedule.owner;
        owner.try_storage(capacity, self.0.brand)
    }
}

const _: () = assert!(
    std::mem::size_of::<Context<'static>>() == std::mem::size_of::<driver::Reference<'static>>()
);

/// Driver-scoped evidence that a retained owner is waiting for an external
/// wake or a monotonic deadline.
#[must_use]
pub enum Wait<'d> {
    Event(region::Identity<'d>),
    Until(clock::Deadline<'d>),
}

const _: () = {
    assert!(mem::size_of::<Wait<'static>>() == mem::size_of::<Option<clock::Deadline<'static>>>());
    assert!(
        mem::align_of::<Wait<'static>>() == mem::align_of::<Option<clock::Deadline<'static>>>()
    );
    assert!(mem::size_of::<Option<Wait<'static>>>() == mem::size_of::<Wait<'static>>());
    assert!(!mem::needs_drop::<Wait<'static>>());
};