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;
#[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 => {
let key = unsafe { node.key_ptr().read() };
if key.identity() != identity {
return State::Stale;
}
if CANCEL {
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 => {
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);
}
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()?;
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,
})
}
}
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
}
}