Skip to main content

dope_core/executor/task/wait/
mod.rs

1use std::{cell, pin::Pin};
2
3use crate::{
4    driver,
5    driver::schedule::{app, ready::wake},
6    executor::task::context,
7};
8
9mod capacity;
10mod length;
11mod sealed;
12
13pub use capacity::Capacity;
14pub use length::Length;
15pub use sealed::links::{Queue, Slot, Waiter};
16
17/// Exact registration state of a caller-owned waiter.
18#[must_use = "registration state must be consumed"]
19#[derive(Clone, Copy, Eq, PartialEq)]
20#[repr(u8)]
21pub enum Registration {
22    Vacant,
23    Active,
24}
25
26const _: () = assert!(std::mem::size_of::<Registration>() == core::mem::size_of::<u8>());
27
28/// Exact reason a caller-owned waiter could not be registered.
29pub enum Error<'d> {
30    Foreign,
31    QueueFull {
32        capacity: Capacity,
33        wake: wake::Waker<'d>,
34    },
35    SlotFull(wake::Waker<'d>),
36}
37
38/// Exact registration failure without moving the caller's retained wake capability.
39#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40pub enum Cause {
41    Foreign,
42    QueueFull { capacity: Capacity },
43    SlotFull,
44}
45
46impl Error<'_> {
47    pub const fn cause(&self) -> Cause {
48        match self {
49            Self::Foreign => Cause::Foreign,
50            Self::QueueFull { capacity, .. } => Cause::QueueFull {
51                capacity: *capacity,
52            },
53            Self::SlotFull(_) => Cause::SlotFull,
54        }
55    }
56}
57
58#[must_use = "Pending means the wake did not fit in this application turn"]
59#[derive(Clone, Copy, PartialEq, Eq)]
60#[repr(u8)]
61pub enum WakeStatus {
62    Complete,
63    Pending,
64}
65
66/// A notification snapshots the waiter count; re-registration cannot extend it.
67pub struct Notifier<'d> {
68    queue: Queue<'d>,
69    remaining: cell::Cell<usize>,
70}
71
72impl<'d> Notifier<'d> {
73    pub const fn new(driver: driver::Reference<'d>, capacity: Capacity) -> Self {
74        Self {
75            queue: Queue::new(driver, capacity),
76            remaining: cell::Cell::new(0),
77        }
78    }
79
80    pub fn try_register<'target, 'poll>(
81        &'target self,
82        waiter: Pin<&Waiter<'target, 'd>>,
83        context: &context::Context<'poll, 'd>,
84    ) -> Result<(), Error<'d>> {
85        self.queue.try_register(waiter, context)
86    }
87
88    pub fn contains(&self, waiter: &Waiter<'_, 'd>) -> bool {
89        self.queue.contains(waiter)
90    }
91
92    /// Starts a new round with the current waiter count.
93    pub fn notify(&self) {
94        self.remaining.set(self.queue.len().get());
95    }
96
97    /// Wakes at most one waiter within the application's work budget.
98    pub fn flush(&self, work: app::Work<'_, 'd>) {
99        let left = self.remaining.get();
100        if left != 0 && self.queue.wake_one(work) == WakeStatus::Complete {
101            self.remaining.set((left - 1).min(self.queue.len().get()));
102        }
103    }
104
105    pub fn is_pending(&self) -> bool {
106        self.remaining.get() != 0
107    }
108}
109
110const _: () = assert!(
111    std::mem::size_of::<Notifier<'static>>()
112        == std::mem::size_of::<Queue<'static>>() + std::mem::size_of::<usize>()
113);