diff options
author | Gardner Vickers <gardner@vickers.me> | 2019-12-24 18:34:47 -0500 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2019-12-24 15:34:47 -0800 |
commit | 67bf9c36f347031ca05872d102a7f9abc8b465f0 (patch) | |
tree | 22c0f472b594382a1bb6fa186b7f40fe4c17013d /tokio/src/time/driver | |
parent | 101f770af33ae65820e1cc0e9b89d998b3c1317a (diff) |
rt: coalesce thread-locals used by the runtime (#1925)
Previously, thread-locals used by the various drivers were situated
with the driver code. This resulted in state being spread out and many
thread-locals being required to run a runtime.
This PR coalesces the thread-locals into a single struct.
Diffstat (limited to 'tokio/src/time/driver')
-rw-r--r-- | tokio/src/time/driver/handle.rs | 48 | ||||
-rw-r--r-- | tokio/src/time/driver/mod.rs | 2 |
2 files changed, 3 insertions, 47 deletions
diff --git a/tokio/src/time/driver/handle.rs b/tokio/src/time/driver/handle.rs index 7d21ad0d..6a8fe2bb 100644 --- a/tokio/src/time/driver/handle.rs +++ b/tokio/src/time/driver/handle.rs @@ -1,7 +1,6 @@ +use crate::runtime::context; use crate::time::driver::Inner; -use std::cell::RefCell; use std::fmt; -use std::marker::PhantomData; use std::sync::{Arc, Weak}; /// Handle to time driver instance. @@ -10,46 +9,6 @@ pub(crate) struct Handle { inner: Weak<Inner>, } -thread_local! { - /// Tracks the timer for the current execution context. - static CURRENT_TIMER: RefCell<Option<Handle>> = RefCell::new(None) -} - -#[derive(Debug)] -/// Guard that unsets the current default timer on drop. -pub(crate) struct DefaultGuard<'a> { - prev: Option<Handle>, - _lifetime: PhantomData<&'a u8>, -} - -impl Drop for DefaultGuard<'_> { - fn drop(&mut self) { - CURRENT_TIMER.with(|current| { - let mut current = current.borrow_mut(); - *current = self.prev.take(); - }) - } -} - -/// Sets handle to default timer, returning guard that unsets it on drop. -/// -/// # Panics -/// -/// This function panics if there already is a default timer set. -pub(crate) fn set_default(handle: &Handle) -> DefaultGuard<'_> { - CURRENT_TIMER.with(|current| { - let mut current = current.borrow_mut(); - let prev = current.take(); - - *current = Some(handle.clone()); - - DefaultGuard { - prev, - _lifetime: PhantomData, - } - }) -} - impl Handle { /// Create a new timer `Handle` from a shared `Inner` timer state. pub(crate) fn new(inner: Weak<Inner>) -> Self { @@ -62,10 +21,7 @@ impl Handle { /// /// This function panics if there is no current timer set. pub(crate) fn current() -> Self { - CURRENT_TIMER.with(|current| match *current.borrow() { - Some(ref handle) => handle.clone(), - None => panic!("no current timer"), - }) + context::ThreadContext::time_handle().expect("no current timer") } /// Try to return a strong ref to the inner diff --git a/tokio/src/time/driver/mod.rs b/tokio/src/time/driver/mod.rs index 5d487423..605d9bca 100644 --- a/tokio/src/time/driver/mod.rs +++ b/tokio/src/time/driver/mod.rs @@ -7,7 +7,7 @@ mod entry; use self::entry::Entry; mod handle; -pub(crate) use self::handle::{set_default, Handle}; +pub(crate) use self::handle::Handle; mod registration; pub(crate) use self::registration::Registration; |