diff options
Diffstat (limited to 'tokio/src/time/driver/handle.rs')
-rw-r--r-- | tokio/src/time/driver/handle.rs | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/tokio/src/time/driver/handle.rs b/tokio/src/time/driver/handle.rs index 54b8a8bd..d4d315dc 100644 --- a/tokio/src/time/driver/handle.rs +++ b/tokio/src/time/driver/handle.rs @@ -1,22 +1,29 @@ -use crate::time::driver::Inner; +use crate::loom::sync::{Arc, Mutex}; +use crate::time::driver::ClockTime; use std::fmt; -use std::sync::{Arc, Weak}; /// Handle to time driver instance. #[derive(Clone)] pub(crate) struct Handle { - inner: Weak<Inner>, + time_source: ClockTime, + inner: Arc<Mutex<super::Inner>>, } impl Handle { /// Creates a new timer `Handle` from a shared `Inner` timer state. - pub(crate) fn new(inner: Weak<Inner>) -> Self { - Handle { inner } + pub(super) fn new(inner: Arc<Mutex<super::Inner>>) -> Self { + let time_source = inner.lock().time_source.clone(); + Handle { time_source, inner } } - /// Tries to return a strong ref to the inner - pub(crate) fn inner(&self) -> Option<Arc<Inner>> { - self.inner.upgrade() + /// Returns the time source associated with this handle + pub(super) fn time_source(&self) -> &ClockTime { + &self.time_source + } + + /// Locks the driver's inner structure + pub(super) fn lock(&self) -> crate::loom::sync::MutexGuard<'_, super::Inner> { + self.inner.lock() } } @@ -31,12 +38,12 @@ cfg_rt! { /// It can be triggered when `Builder::enable_time()` or /// `Builder::enable_all()` are not included in the builder. /// - /// It can also panic whenever a timer is created outside of a Tokio - /// runtime. That is why `rt.block_on(delay_for(...))` will panic, - /// since the function is executed outside of the runtime. - /// Whereas `rt.block_on(async {delay_for(...).await})` doesn't - /// panic. And this is because wrapping the function on an async makes it - /// lazy, and so gets executed inside the runtime successfuly without + /// It can also panic whenever a timer is created ouClockTimeide of a + /// Tokio runtime. That is why `rt.block_on(delay_for(...))` will panic, + /// since the function is executed ouClockTimeide of the runtime. + /// Whereas `rt.block_on(async {delay_for(...).await})` doesn't panic. + /// And this is because wrapping the function on an async makes it lazy, + /// and so gets executed inside the runtime successfuly without /// panicking. pub(crate) fn current() -> Self { crate::runtime::context::time_handle() @@ -56,12 +63,12 @@ cfg_not_rt! { /// It can be triggered when `Builder::enable_time()` or /// `Builder::enable_all()` are not included in the builder. /// - /// It can also panic whenever a timer is created outside of a Tokio + /// It can also panic whenever a timer is created ouClockTimeide of a Tokio /// runtime. That is why `rt.block_on(delay_for(...))` will panic, - /// since the function is executed outside of the runtime. + /// since the function is executed ouClockTimeide of the runtime. /// Whereas `rt.block_on(async {delay_for(...).await})` doesn't /// panic. And this is because wrapping the function on an async makes it - /// lazy, and so gets executed inside the runtime successfuly without + /// lazy, and so geClockTime executed inside the runtime successfuly without /// panicking. pub(crate) fn current() -> Self { panic!("there is no timer running, must be called from the context of Tokio runtime or \ |