use std::{cell, pin::Pin};
use crate::{
driver,
driver::schedule::{app, ready::wake},
executor::task::context,
};
mod capacity;
mod length;
mod sealed;
pub use capacity::Capacity;
pub use length::Length;
pub use sealed::links::{Queue, Slot, Waiter};
#[must_use = "registration state must be consumed"]
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(u8)]
pub enum Registration {
Vacant,
Active,
}
const _: () = assert!(std::mem::size_of::<Registration>() == core::mem::size_of::<u8>());
pub enum Error<'d> {
Foreign,
QueueFull {
capacity: Capacity,
wake: wake::Waker<'d>,
},
SlotFull(wake::Waker<'d>),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Cause {
Foreign,
QueueFull { capacity: Capacity },
SlotFull,
}
impl Error<'_> {
pub const fn cause(&self) -> Cause {
match self {
Self::Foreign => Cause::Foreign,
Self::QueueFull { capacity, .. } => Cause::QueueFull {
capacity: *capacity,
},
Self::SlotFull(_) => Cause::SlotFull,
}
}
}
#[must_use = "Pending means the wake did not fit in this application turn"]
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum WakeStatus {
Complete,
Pending,
}
pub struct Notifier<'d> {
queue: Queue<'d>,
remaining: cell::Cell<usize>,
}
impl<'d> Notifier<'d> {
pub const fn new(driver: driver::Reference<'d>, capacity: Capacity) -> Self {
Self {
queue: Queue::new(driver, capacity),
remaining: cell::Cell::new(0),
}
}
pub fn try_register<'target, 'poll>(
&'target self,
waiter: Pin<&Waiter<'target, 'd>>,
context: &context::Context<'poll, 'd>,
) -> Result<(), Error<'d>> {
self.queue.try_register(waiter, context)
}
pub fn contains(&self, waiter: &Waiter<'_, 'd>) -> bool {
self.queue.contains(waiter)
}
pub fn notify(&self) {
self.remaining.set(self.queue.len().get());
}
pub fn flush(&self, work: app::Work<'_, 'd>) {
let left = self.remaining.get();
if left != 0 && self.queue.wake_one(work) == WakeStatus::Complete {
self.remaining.set((left - 1).min(self.queue.len().get()));
}
}
pub fn is_pending(&self) -> bool {
self.remaining.get() != 0
}
}
const _: () = assert!(
std::mem::size_of::<Notifier<'static>>()
== std::mem::size_of::<Queue<'static>>() + std::mem::size_of::<usize>()
);