dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::io::reactor::{
    complete::flags::Flow,
    event::{self, kind},
};
use std::{marker, mem};

use dope_sys::Errno;
use o3::base::cell::region;

use crate::{
    Reported,
    driver::{self, life::Lifecycle, receive::credit, route, schedule::clock},
    io::transfer,
};

/// Post-quiescence access to driver resource finalization.
#[must_use]
pub struct Finish<'a, 'd, Report> {
    lifecycle: &'a mut Lifecycle<'d, Report>,
    forced: bool,
    disarm: Option<Errno>,
}

impl<'a, 'd, Report: event::Report<Event = Reported>> Finish<'a, 'd, Report> {
    pub(super) fn new(life: &'a mut Lifecycle<'d, Report>, forced: bool) -> Self {
        life.finish::<true, true>();
        Self {
            lifecycle: life,
            forced,
            disarm: None,
        }
    }

    /// Returns whether cooperative draining ended before owner quiescence. The
    /// backend is already fully quiesced in either case.
    pub fn is_forced(&self) -> bool {
        self.forced
    }

    /// Records the first native timer-disarm failure while finalization continues.
    pub fn fail_disarm(&mut self, cause: Errno) {
        if self.disarm.is_none() {
            self.disarm = Some(cause);
        }
    }

    /// Returns the first timer-disarm failure; this does not prove timer deletion.
    pub fn disarm_error(&self) -> Option<Errno> {
        self.disarm
    }

    /// Monotonic authority captured for the exact turn that reached finalization.
    pub fn clock<'f>(&'f self) -> clock::Clock<'f, 'd> {
        self.lifecycle.driver_ref().clock()
    }

    pub fn received(
        &mut self,
        target: route::Erased<'d, kind::Recv, impl route::Selection>,
        flow: Flow,
        result: Result<transfer::Len, Errno>,
    ) {
        self.lifecycle.received(target, flow, result);
    }

    /// Region authority paired with this exact post-quiescence proof.
    pub fn region_token<'f>(&'f mut self) -> &'f mut region::Token<region::Identity<'d>> {
        &mut self.lifecycle.local.region.0
    }

    pub fn regions(
        &mut self,
    ) -> &mut (
        region::Token<region::Identity<'d>>,
        region::Token<region::Identity<'d, 1>>,
    ) {
        &mut self.lifecycle.local.region
    }

    pub fn state_token<'f>(&'f mut self) -> &'f mut region::Token<region::Identity<'d, 1>> {
        &mut self.lifecycle.local.region.1
    }

    pub(in crate::driver) fn brand(&self) -> driver::Invariant<'d> {
        self.lifecycle.driver_ref().brand
    }

    /// Binds retained-credit finalization to this exact quiescent turn.
    pub fn credit<'f>(&'f mut self) -> Credit<'f, 'd> {
        Credit::new(self.brand())
    }
}

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

/// Quiescent authority over one retained-credit owner.
#[repr(transparent)]
pub struct Credit<'a, 'd> {
    borrow: marker::PhantomData<(&'a mut (), driver::Invariant<'d>)>,
}

impl<'d> Credit<'_, 'd> {
    const fn new(_brand: driver::Invariant<'d>) -> Self {
        Self {
            borrow: marker::PhantomData,
        }
    }

    /// Detaches every slot proved active by the quiescent storage borrow.
    pub fn detach(self, storage: &credit::Storage<'d>) {
        for slot in storage.entries.iter() {
            slot.detach();
        }
    }
}

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