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