tokio/runtime/time/handle.rs
1use crate::runtime::time::TimeSource;
2use std::fmt;
3
4/// Handle to time driver instance.
5pub(crate) struct Handle {
6 pub(super) time_source: TimeSource,
7 pub(super) inner: super::Inner,
8}
9
10impl Handle {
11 /// Returns the time source associated with this handle.
12 pub(crate) fn time_source(&self) -> &TimeSource {
13 &self.time_source
14 }
15
16 /// Checks whether the driver has been shutdown.
17 pub(super) fn is_shutdown(&self) -> bool {
18 self.inner.is_shutdown()
19 }
20
21 /// Track that the driver is being unparked
22 pub(crate) fn unpark(&self) {
23 #[cfg(feature = "test-util")]
24 match self.inner {
25 super::Inner::Traditional { ref did_wake, .. } => {
26 did_wake.store(true, std::sync::atomic::Ordering::SeqCst);
27 }
28 #[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
29 super::Inner::Alternative { ref did_wake, .. } => {
30 did_wake.store(true, std::sync::atomic::Ordering::SeqCst);
31 }
32 }
33 }
34}
35
36cfg_not_rt! {
37 impl Handle {
38 /// Tries to get a handle to the current timer.
39 ///
40 /// # Panics
41 ///
42 /// This function panics if there is no current timer set.
43 ///
44 /// It can be triggered when [`Builder::enable_time`] or
45 /// [`Builder::enable_all`] are not included in the builder.
46 ///
47 /// It can also panic whenever a timer is created outside of a
48 /// Tokio runtime. That is why `rt.block_on(sleep(...))` will panic,
49 /// since the function is executed outside of the runtime.
50 /// Whereas `rt.block_on(async {sleep(...).await})` doesn't panic.
51 /// And this is because wrapping the function on an async makes it lazy,
52 /// and so gets executed inside the runtime successfully without
53 /// panicking.
54 ///
55 /// [`Builder::enable_time`]: crate::runtime::Builder::enable_time
56 /// [`Builder::enable_all`]: crate::runtime::Builder::enable_all
57 #[track_caller]
58 pub(crate) fn current() -> Self {
59 panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR)
60 }
61 }
62}
63
64impl fmt::Debug for Handle {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "Handle")
67 }
68}