dope-uring 0.76.0

Owned io_uring backend for dope
Documentation
use std::marker;

use dope_sys::table::permit;
use o3::{
    alloc,
    alloc::array,
    depot::{self, capacity, queue::links},
};
pub(crate) use support::Support;

mod 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::StagedNode<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();
        let Some(node) = self.slots.get(raw as usize) else {
            return State::Stale;
        };
        match node.phase.get() {
            depot::raw::StagedPhase::Vacant => State::Gone,
            depot::raw::StagedPhase::Queued => {
                // SAFETY: this phase owns the initialized key; Key cannot reenter.
                let key = unsafe { node.key_ptr().read() };
                if key.identity() != identity {
                    return State::Stale;
                }
                if CANCEL {
                    // SAFETY: this admitted index is linked in the sole queued chain.
                    let _ = unsafe {
                        let index = <links::Index as depot::raw::Word>::from_offset(raw as usize);
                        self.slots
                            .state()
                            .detach_hook::<false, 0, _>(self, index, &node.hook)
                            .0
                    };
                    node.phase.set(depot::raw::StagedPhase::Vacant);
                }
                State::Waiting
            }
            depot::raw::StagedPhase::Parked => {
                // SAFETY: a granted slot retains its initialized Copy key.
                let key = unsafe { node.key_ptr().read() };
                if key.identity() != identity {
                    return State::Stale;
                }
                node.phase.set(depot::raw::StagedPhase::Vacant);
                State::Granted(key)
            }
        }
    }
}

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();
        let node = self.slots.get(raw as usize).ok_or(key)?;
        if !matches!(node.phase.get(), depot::raw::StagedPhase::Vacant) {
            return Err(key);
        }
        // SAFETY: this represented vacancy owns its key and detached hook.
        unsafe {
            node.key_ptr().write(key);
            let index = <links::Index as depot::raw::Word>::from_offset(raw as usize);
            self.slots
                .state()
                .link_back::<0, _>(self, index, &node.hook);
        }
        node.phase.set(depot::raw::StagedPhase::Queued);
        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 original head is represented and owns its queued key.
        let node = unsafe { self.slots.get_unchecked(index.get()) };
        let position = unsafe { I::from_raw(index.raw()) };
        let key = unsafe { node.key_ptr().read() };
        let _ = unsafe {
            self.slots
                .state()
                .detach_hook::<true, 0, _>(self, index, &node.hook)
                .0
        };
        unsafe { node.set_parked(()) };
        Some((position, key))
    }
}

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 = depot::raw::StagedNode::init(array::raw::Block::try_new(
            capacity::Count::new(capacity as u32),
            links::raw::Chain::new(),
        )?);
        Ok(Self {
            slots,
            index: marker::PhantomData,
        })
    }
}

// SAFETY: the fixed backing owns one stable hook for each admitted index.
unsafe impl<Key: permit::raw::Key, I: depot::raw::Compact> depot::raw::Hooks for Fifo<Key, I> {
    unsafe fn hook_raw(&self, index: links::Index) -> &links::Hook {
        &unsafe { self.slots.get_unchecked(index.get()) }.hook
    }
}