o3 0.62.0

shared-nothing primitives
Documentation
use std::cell::Cell;

use crate::depot::{self, queue::links};

/// One-word head of a reciprocal ring in a caller-owned hook store.
#[derive(Debug)]
#[repr(transparent)]
pub struct Cycle(Cell<u32>);

impl Default for Cycle {
    fn default() -> Self {
        Self::new()
    }
}

impl Cycle {
    pub const fn new() -> Self {
        Self(Cell::new(links::Index::END))
    }

    pub fn first(&self) -> Option<links::Index> {
        links::Index::decode(self.0.get())
    }

    pub fn is_empty(&self) -> bool {
        self.0.get() == links::Index::END
    }

    /// # Safety
    /// `index` is the live head of this reciprocal ring in `hooks`.
    pub unsafe fn advance<const PLANE: usize>(
        &self,
        hooks: &(impl depot::raw::Hooks<PLANE> + ?Sized),
        index: links::Index,
    ) -> links::Index {
        let Some(next) = (unsafe { hooks.hook_raw(index) }).next_index() else {
            return index;
        };
        self.0.set(next.raw());
        next
    }

    /// # Safety
    /// The ring belongs to `hooks` and the hook at `index` is exclusively detached.
    pub unsafe fn push<const PLANE: usize>(
        &self,
        hooks: &(impl depot::raw::Hooks<PLANE> + ?Sized),
        index: links::Index,
    ) {
        let hook = unsafe { hooks.hook_raw(index) };
        let Some(head) = self.first() else {
            hook.previous.set(links::Index::END);
            hook.next.set(links::Index::END);
            self.0.set(index.raw());
            return;
        };
        let first = unsafe { hooks.hook_raw(head) };
        let back = match first.prev_index() {
            Some(tail) => (tail, unsafe { hooks.hook_raw(tail) }),
            None => (head, first),
        };
        Self::append(back, (index, hook), (head, first));
    }

    fn append(
        (tail, last): (links::Index, &links::Hook),
        (index, hook): (links::Index, &links::Hook),
        (head, first): (links::Index, &links::Hook),
    ) {
        hook.previous.set(tail.raw());
        hook.next.set(head.raw());
        last.next.set(index.raw());
        first.previous.set(index.raw());
    }

    /// # Safety
    /// `index` belongs to this reciprocal ring in `hooks`. FRONT proves it is the head.
    pub unsafe fn unlink<const FRONT: bool, const PLANE: usize>(
        &self,
        hooks: &(impl depot::raw::Hooks<PLANE> + ?Sized),
        index: links::Index,
    ) {
        let hook = unsafe { hooks.hook_raw(index) };
        if let Some(next) = hook.next_index() {
            let prev = unsafe { links::Index::new_raw(hook.previous.get()) };
            let before = unsafe { hooks.hook_raw(prev) };
            if prev == next {
                before.next.set(links::Index::END);
                before.previous.set(links::Index::END);
            } else {
                let after = unsafe { hooks.hook_raw(next) };
                before.next.set(next.raw());
                after.previous.set(prev.raw());
            }
            if FRONT || self.0.get() == index.raw() {
                self.0.set(next.raw());
            }
        } else {
            self.0.set(links::Index::END);
        }
        unsafe { links::Detached::new(hook, index) }.idle();
    }

    /// # Safety
    /// `index` belongs to this reciprocal ring in `hooks`. FRONT proves it is the head.
    pub(in crate::depot) unsafe fn requeue<const FRONT: bool, const PLANE: usize>(
        &self,
        hooks: &(impl depot::raw::Hooks<PLANE> + ?Sized),
        index: links::Index,
    ) {
        let head = if FRONT {
            index
        } else {
            // SAFETY: the retained member proves this ring is nonempty.
            unsafe { links::Index::new_raw(self.0.get()) }
        };
        if head == index {
            unsafe { self.advance(hooks, index) };
            return;
        }
        // SAFETY: a head distinct from this member has a live predecessor.
        let first = unsafe { hooks.hook_raw(head) };
        let tail = unsafe { links::Index::new_raw(first.previous.get()) };
        if tail == index {
            return;
        }
        // SAFETY: an interior member and both its neighbors are live in the same store.
        let hook = unsafe { hooks.hook_raw(index) };
        let prev = unsafe { links::Index::new_raw(hook.previous.get()) };
        let next = unsafe { links::Index::new_raw(hook.next.get()) };
        let before = unsafe { hooks.hook_raw(prev) };
        let after = unsafe { hooks.hook_raw(next) };
        let last = unsafe { hooks.hook_raw(tail) };
        before.next.set(next.raw());
        after.previous.set(prev.raw());
        Self::append((tail, last), (index, hook), (head, first));
    }
}

const _: () = assert!(std::mem::size_of::<Cycle>() == std::mem::size_of::<u32>());