Skip to main content

externally_driven_async_rt/storage/
mod.rs

1use crate::{
2    claimed::Claimed,
3    context::Context,
4    data::ring_buffer::RingBuffer,
5    io::IoWaiters,
6    notify::Notification,
7    selector::Selector,
8    tasks::{RootSlot, Tasks},
9    time::{Instant, Timers},
10};
11
12/// The main pre-allocated storage that [`EventLoop`](crate::EventLoop) needs.
13///
14/// A storage can be used by only one [`EventLoop`](crate::EventLoop) ever.
15#[must_use]
16pub struct LoopStorage<
17    E,
18    const EVENTS: usize,
19    const TASKS: usize,
20    const TASK_SIZE: usize,
21    const ROOT_TASK_SIZE: usize,
22    const TIMERS: usize,
23    const FDS: usize,
24> {
25    pub(crate) timers: Timers<TIMERS>,
26    pub(crate) io_waiters: IoWaiters<FDS>,
27    pub(crate) tasks: Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
28    pub(crate) notification: Notification,
29    pub(crate) events: RingBuffer<E, EVENTS>,
30}
31
32// SAFETY: a storage has no public methods, so other threads can only pass `&LoopStorage` around.
33// The crate touches it only through a `Context`, which only `LoopStorage::claim`
34// creates, and that succeeds once ever, on the thread that runs the claiming loop (claiming the
35// tasks claims their `WakeQueue`, which panics the second time); `EventLoop` and `Context` are
36// `!Send`, so everything but the wakers stays on that thread, and wakers only touch the
37// `WakeQueue`, which asserts that thread.
38unsafe impl<
39    E,
40    const EVENTS: usize,
41    const TASKS: usize,
42    const TASK_SIZE: usize,
43    const ROOT_TASK_SIZE: usize,
44    const TIMERS: usize,
45    const FDS: usize,
46> Sync for LoopStorage<E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
47{
48}
49
50impl<
51    E,
52    const EVENTS: usize,
53    const TASKS: usize,
54    const TASK_SIZE: usize,
55    const ROOT_TASK_SIZE: usize,
56    const TIMERS: usize,
57    const FDS: usize,
58> LoopStorage<E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
59{
60    /// Constructor of an empty storage, **can be called in const-context**, so you can put it into a static variable.
61    pub const fn new() -> Self {
62        Self::with_clock(Instant::now)
63    }
64
65    pub(crate) const fn with_clock(now: fn() -> Instant) -> Self {
66        Self {
67            timers: Timers::new(now),
68            io_waiters: IoWaiters::new(),
69            tasks: Tasks::new(),
70            notification: Notification::new(),
71            events: RingBuffer::new(),
72        }
73    }
74
75    pub(crate) fn claim<'sel>(
76        &'static self,
77        selector: &'sel Selector,
78    ) -> Option<(
79        Context<'sel, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
80        RootSlot,
81    )> {
82        let root = self.tasks.claim()?;
83        let ctx = Context {
84            storage: self,
85            selector,
86            // SAFETY: claiming the queue above fails unless this is the storage's first claim, so
87            // these tasks belong to this loop alone. Only the `EventLoop<'sel>` that owns this claim
88            // polls and drops them, and it drops them all before `'sel` ends or, if leaked, never
89            // touches them again.
90            claimed: unsafe { Claimed::new() },
91        };
92        Some((ctx, root))
93    }
94}
95
96#[cfg(test)]
97mod tests;