externally_driven_async_rt/storage/
mod.rs1use 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#[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
32unsafe 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 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<'a>(
76 &'static self,
77 selector: &'a Selector,
78 ) -> Option<(
79 Context<'a, 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 claimed: unsafe { Claimed::new() },
91 };
92 Some((ctx, root))
93 }
94}
95
96#[cfg(test)]
97mod tests;