-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rt: add runtime::context to unify thread-locals (#5143)
This patch is the first step towards unifying all the thread-local variables spread out across Tokio. A new `Context` struct is added which will be used to replace the various thread-locals that exist today. Initially, `Context` only holds the current runtime handle and the random number generator. Further PRs will add other thread-local state. A previous PR removed `runtime::context`. At that time, `runtime::context` was used as an extra layer to access the various runtime driver handles. This version of `runtime::context` serves a different purpose (unifying all the thread-locals).
- Loading branch information
1 parent
d1a8ec6
commit df99428
Showing
12 changed files
with
103 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::util::rand::{FastRand, RngSeed}; | ||
|
||
cfg_rt! { | ||
use crate::runtime::scheduler; | ||
use std::cell::RefCell; | ||
} | ||
|
||
struct Context { | ||
/// Handle to the runtime scheduler running on the current thread. | ||
#[cfg(feature = "rt")] | ||
scheduler: RefCell<Option<scheduler::Handle>>, | ||
rng: FastRand, | ||
} | ||
|
||
tokio_thread_local! { | ||
static CONTEXT: Context = { | ||
Context { | ||
#[cfg(feature = "rt")] | ||
scheduler: RefCell::new(None), | ||
rng: FastRand::new(RngSeed::new()), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "macros")] | ||
pub(crate) fn thread_rng_n(n: u32) -> u32 { | ||
CONTEXT.with(|ctx| ctx.rng.fastrand_n(n)) | ||
} | ||
|
||
cfg_rt! { | ||
use crate::runtime::TryCurrentError; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct EnterGuard { | ||
old_handle: Option<scheduler::Handle>, | ||
old_seed: RngSeed, | ||
} | ||
|
||
pub(crate) fn try_current() -> Result<scheduler::Handle, TryCurrentError> { | ||
match CONTEXT.try_with(|ctx| ctx.scheduler.borrow().clone()) { | ||
Ok(Some(handle)) => Ok(handle), | ||
Ok(None) => Err(TryCurrentError::new_no_context()), | ||
Err(_access_error) => Err(TryCurrentError::new_thread_local_destroyed()), | ||
} | ||
} | ||
|
||
/// Sets this [`Handle`] as the current active [`Handle`]. | ||
/// | ||
/// [`Handle`]: crate::runtime::scheduler::Handle | ||
pub(crate) fn try_enter(handle: &scheduler::Handle) -> Option<EnterGuard> { | ||
let rng_seed = handle.seed_generator().next_seed(); | ||
|
||
CONTEXT.try_with(|ctx| { | ||
let old_handle = ctx.scheduler.borrow_mut().replace(handle.clone()); | ||
let old_seed = ctx.rng.replace_seed(rng_seed); | ||
|
||
EnterGuard { | ||
old_handle, | ||
old_seed, | ||
} | ||
}).ok() | ||
} | ||
|
||
impl Drop for EnterGuard { | ||
fn drop(&mut self) { | ||
CONTEXT.with(|ctx| { | ||
*ctx.scheduler.borrow_mut() = self.old_handle.take(); | ||
ctx.rng.replace_seed(self.old_seed.clone()); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters