dope-uring 0.77.0

Owned io_uring backend for dope
Documentation
use std::{cell::Cell, marker, mem::MaybeUninit};

use dope_sys::table::permit;
use o3::{
    alloc,
    alloc::array,
    depot::{self, capacity, queue::links},
};

mod support;
pub(crate) use support::Support;

/// Central generation-exact FIFO permit state.
///
/// The key lifetime is invariant and cannot be widened independently of the
/// storage that owns it.
#[repr(transparent)]
pub struct Fifo<Key: permit::raw::Key, I: depot::raw::Compact> {
    slots: array::raw::Block<
        depot::raw::Node<Cell<MaybeUninit<Key>>>,
        capacity::Count,
        links::raw::Chain,
    >,
    index: marker::PhantomData<fn(I) -> I>,
}

impl<Key: permit::raw::Key, I: depot::raw::Compact> Fifo<Key, I> {
    fn take_inner<const CANCEL: bool>(&self, index: I, key: Key) -> permit::State<Key> {
        use permit::State;

        let identity = key.identity();
        let raw = index.into_raw() as usize;
        let Some(node) = self.slots.get(raw) else {
            return State::Stale;
        };
        let hook = node.hook();
        if hook.is_vacant(raw) {
            return State::Gone;
        }
        // SAFETY: both the linked and parked hook retain the key written at admission.
        let resident = unsafe { node.get().assume_init() };
        if resident.identity() != identity {
            return State::Stale;
        }
        // SAFETY: this slot is within the admitted compact capacity.
        let index = unsafe { <links::Index as depot::raw::Word>::from_offset(raw) };
        if hook.try_unpark(index).is_some() {
            return State::Granted(resident);
        }
        if CANCEL {
            // SAFETY: this non-vacant, non-parked hook belongs to the sole FIFO.
            unsafe {
                self.slots
                    .state()
                    .detach_hook::<false, 0, _>(&*self.slots, index, hook)
            }
            .0
            .idle();
        }
        State::Waiting
    }
}

impl<Key: permit::raw::Key, I: depot::raw::Compact> permit::Queue for Fifo<Key, I> {
    type Key = Key;
    type Index = I;
    type Capability = Support;

    fn try_acquire() -> Result<Self::Capability, permit::Absent> {
        Ok(Support::Available)
    }

    fn try_arm(
        &self,
        &Support::Available: &Self::Capability,
        index: I,
        key: Key,
    ) -> Result<(), Key> {
        let raw = index.into_raw() as usize;
        let node = self.slots.get(raw).ok_or(key)?;
        let hook = node.hook();
        if !hook.is_vacant(raw) {
            return Err(key);
        }
        node.set(MaybeUninit::new(key));
        // SAFETY: this represented vacancy owns its key and detached hook.
        unsafe {
            let index = <links::Index as depot::raw::Word>::from_offset(raw);
            self.slots
                .state()
                .link_back::<0, _>(&*self.slots, index, hook);
        }
        Ok(())
    }

    fn cancel(
        &self,
        &Support::Available: &Self::Capability,
        index: I,
        key: Key,
    ) -> permit::State<Key> {
        self.take_inner::<true>(index, key)
    }

    fn take(
        &self,
        &Support::Available: &Self::Capability,
        index: I,
        key: Key,
    ) -> permit::State<Key> {
        self.take_inner::<false>(index, key)
    }

    fn grant_one(&self, &Support::Available: &Self::Capability) -> Option<(I, Key)> {
        let index = self.slots.state().first()?;
        // SAFETY: the front is in the admitted index domain and owns its initialized key.
        unsafe {
            let node = self.slots.get_unchecked(index.get());
            self.slots
                .state()
                .detach_hook::<true, 0, _>(&*self.slots, index, node.hook())
                .0
                .park();
            Some((I::from_raw(index.raw()), node.get().assume_init()))
        }
    }
}

impl<Key: permit::raw::Key, I: depot::raw::Compact> permit::Create for Fifo<Key, I> {
    fn try_new(capacity: usize) -> Result<Self, alloc::Error> {
        if capacity > u32::MAX as usize || (capacity != 0 && capacity - 1 > I::MAX as usize) {
            return Err(alloc::Error::Overflow);
        }
        let slots = array::raw::Block::try_new(
            capacity::Count::new(capacity as u32),
            links::raw::Chain::new(),
        )?
        .init(|index| {
            depot::raw::Node::vacant(Cell::new(MaybeUninit::uninit()), index as u32, u32::MAX)
        });
        Ok(Self {
            slots,
            index: marker::PhantomData,
        })
    }
}