o3 0.62.0

shared-nothing primitives
Documentation
use std::{cell, convert::Infallible, marker, num};

use crate::{
    alloc,
    base::marker::ThreadBound,
    depot::{
        self,
        queue::{
            links,
            resident::{self, Bounds, Capacity, Error, Extent, Fixed, Lane, Storage, fifo},
        },
    },
};

/// Stable resident values partitioned among FIFO lanes and an active ring.
pub struct Lanes<W, L = num::NonZero<usize>, G = Infallible, P = Infallible> {
    pub(super) storage: Storage<resident::raw::Entry<W, G>, L>,
    thread: marker::PhantomData<(ThreadBound, P)>,
    pub(super) round: links::raw::Cycle,
    pub(super) free: links::Free,
    draining: cell::Cell<bool>,
}

impl<W, L, G, P> Lanes<W, L, G, P> {
    pub fn try_new(capacity: usize, lanes: L) -> Result<Self, Error>
    where
        L: Extent,
    {
        let extent = Capacity::new(capacity, lanes)?;
        let capacity = alloc::raw::Shape::len(&extent);
        let storage = alloc::raw::Uninit::try_from_fn(
            extent,
            (),
            |index| {
                let next = index + 1;
                let next = if capacity > next {
                    next as u32
                } else {
                    u32::MAX
                };
                depot::raw::Node::vacant(resident::raw::Entry::default(), index as u32, next)
            },
            |index| depot::raw::Node::vacant(links::raw::Chain::new(), index as u32, u32::MAX),
        )
        .map_err(Error::Exhausted)?;
        Ok(Self {
            storage,
            thread: marker::PhantomData,
            round: links::raw::Cycle::new(),
            free: links::Free::seeded(capacity),
            draining: cell::Cell::new(false),
        })
    }

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

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

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

    /// Takes the next value in drain order and ends draining at quiescence.
    #[must_use = "a drained item remains caller-owned"]
    pub fn drain_next(&self) -> Option<W> {
        if !self.draining.get() {
            return None;
        }
        let next = resident::raw::Entry::take_active::<false, _, _>(self);
        if next.is_none() {
            self.draining.set(false);
        }
        next
    }
}

impl<W, L> Lanes<W, L> {
    /// Borrows lane zero, retained by every constructor's nonzero extent.
    pub fn first(&self) -> Lane<'_, Self> {
        Lane::new(self, links::Index::from(num::NonZero::<u32>::MIN))
    }

    pub fn take_next(&self) -> Option<W> {
        if self.is_draining() {
            return None;
        }
        resident::raw::Entry::take_active::<true, _, _>(self)
    }
}

impl<W> Lanes<W> {
    pub fn try_lane(&self, index: usize) -> Result<Lane<'_, Self>, Bounds<num::NonZero<u32>>> {
        Ok(Lane::new(
            self,
            resident::raw::View::try_index(self, index)?,
        ))
    }

    /// Creates one FIFO and borrows its source under a fresh invariant scope.
    pub fn try_with<S, R>(
        capacity: usize,
        lanes: num::NonZero<usize>,
        source: S,
        run: impl for<'scope> FnOnce(&'scope S, Lanes<W, fifo::Scope<'scope>>) -> R,
    ) -> Result<R, Error> {
        let scope = fifo::Scope {
            lanes,
            brand: marker::PhantomData,
        };
        let fifo = Lanes::try_new(capacity, scope)?;
        Ok(run(&source, fifo))
    }
}

impl<'scope, W> Lanes<W, fifo::Scope<'scope>> {
    pub fn try_index(
        &self,
        index: usize,
    ) -> Result<fifo::Index<'scope>, Bounds<num::NonZero<u32>>> {
        Ok(fifo::Index {
            position: resident::raw::View::try_index(self, index)?.position(),
            brand: marker::PhantomData,
        })
    }

    /// Borrows a lane admitted by this scope's only FIFO.
    pub fn lane(&self, index: fifo::Index<'scope>) -> Lane<'_, Self> {
        Lane::new(self, links::Index::from(index.position))
    }
}

impl<W, G, P, const N: usize> Lanes<W, Fixed<N>, G, P> {
    pub fn lane<const I: usize>(&self) -> Lane<'_, Self> {
        const { assert!(I < N && N <= u32::MAX as usize) };
        Lane::new(
            self,
            links::Index::from(num::NonZero::<u32>::MIN.saturating_add(I as u32)),
        )
    }
}