dope-core 0.76.0

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

use o3::{
    alloc::BoxSliceExt,
    base::marker::ThreadBound,
    depot::{
        self, capacity,
        queue::{links, resident},
    },
};

use crate::driver::{
    self,
    schedule::{
        app,
        ready::{self, wake},
    },
};

pub mod raw;

type Entry<'d> = depot::raw::Node<resident::raw::Entry<ready::Retained<'d>>>;

/// One wake per exact identity with an active-only FIFO.
pub struct Table<'d> {
    driver: driver::Reference<'d>,
    entries: Box<[Entry<'d>]>,
    thread: marker::PhantomData<ThreadBound>,
    links: links::raw::Chain,
    draining: Cell<bool>,
}

/// Linear ownership of one exact table slot.
pub struct Registration<'target, 'd>(raw::Slot<'target, 'd>);

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[must_use = "a rejected wake registration must be handled"]
pub enum Error {
    Active { position: wake::Position },
    Draining { position: wake::Position },
    Occupied { position: wake::Position },
}

impl<'d> Table<'d> {
    pub fn try_new(
        driver: driver::Reference<'d>,
        capacity: usize,
    ) -> Result<Self, resident::Error> {
        let count = capacity::Count::try_from(capacity).map_err(resident::Error::Capacity)?;
        let entries = Box::try_from_fn(count.get(), |index| {
            Entry::vacant(resident::raw::Entry::default(), index as u32, u32::MAX)
        })?;
        Ok(Self {
            driver,
            entries,
            thread: marker::PhantomData,
            links: links::raw::Chain::new(),
            draining: Cell::new(false),
        })
    }

    pub fn try_slot(&self, position: usize) -> Result<raw::Slot<'_, 'd>, resident::Bounds<usize>> {
        raw::Slot::try_new(self, position)
    }

    pub fn drain_one(&self, work: app::Work<'_, 'd>) -> wake::Drain {
        if self.draining.get() {
            raw::Slot::drain_one(self, work)
        } else {
            wake::Drain::Empty
        }
    }

    pub fn has_waiters(&self) -> bool {
        !self.links.is_empty()
    }

    pub fn begin_drain(&self) {
        self.draining.set(true);
    }

    pub fn is_draining(&self) -> bool {
        self.draining.get()
    }
}

impl Drop for Registration<'_, '_> {
    fn drop(&mut self) {
        raw::Slot::cancel(self);
    }
}

const _: () = {
    assert!(
        mem::size_of::<Option<Registration<'static, 'static>>>() == 2 * mem::size_of::<usize>()
    );
    assert!(mem::align_of::<Registration<'static, 'static>>() == mem::align_of::<usize>());
};