tokio/runtime/time_alt/
context.rs

1use super::{cancellation_queue, RegistrationQueue, Wheel};
2
3/// Local context for the time driver, used when the runtime wants to
4/// fire/cancel timers.
5pub(crate) struct LocalContext {
6    pub(crate) wheel: Wheel,
7    pub(crate) registration_queue: RegistrationQueue,
8    pub(crate) canc_tx: cancellation_queue::Sender,
9    pub(crate) canc_rx: cancellation_queue::Receiver,
10}
11
12impl LocalContext {
13    pub(crate) fn new() -> Self {
14        let (canc_tx, canc_rx) = cancellation_queue::new();
15        Self {
16            wheel: Wheel::new(),
17            registration_queue: RegistrationQueue::new(),
18            canc_tx,
19            canc_rx,
20        }
21    }
22}
23
24pub(crate) enum TempLocalContext<'a> {
25    /// The runtime is running, we can access it.
26    Running {
27        registration_queue: &'a mut RegistrationQueue,
28        elapsed: u64,
29    },
30    #[cfg(feature = "rt-multi-thread")]
31    /// The runtime is shutting down, no timers can be registered.
32    Shutdown,
33}
34
35impl<'a> TempLocalContext<'a> {
36    pub(crate) fn new_running(cx: &'a mut LocalContext) -> Self {
37        TempLocalContext::Running {
38            registration_queue: &mut cx.registration_queue,
39            elapsed: cx.wheel.elapsed(),
40        }
41    }
42
43    #[cfg(feature = "rt-multi-thread")]
44    pub(crate) fn new_shutdown() -> Self {
45        TempLocalContext::Shutdown
46    }
47}