aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/runtime/context.rs')
-rw-r--r--src/runtime/context.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/runtime/context.rs b/src/runtime/context.rs
index 2e54c8b..fef53ca 100644
--- a/src/runtime/context.rs
+++ b/src/runtime/context.rs
@@ -15,6 +15,10 @@ cfg_rt! {
}
struct Context {
+ /// Uniquely identifies the current thread
+ #[cfg(feature = "rt")]
+ thread_id: Cell<Option<ThreadId>>,
+
/// Handle to the runtime scheduler running on the current thread.
#[cfg(feature = "rt")]
handle: RefCell<Option<scheduler::Handle>>,
@@ -46,6 +50,9 @@ struct Context {
tokio_thread_local! {
static CONTEXT: Context = {
Context {
+ #[cfg(feature = "rt")]
+ thread_id: Cell::new(None),
+
/// Tracks the current runtime handle to use when spawning,
/// accessing drivers, etc...
#[cfg(feature = "rt")]
@@ -82,10 +89,23 @@ pub(super) fn budget<R>(f: impl FnOnce(&Cell<coop::Budget>) -> R) -> Result<R, A
}
cfg_rt! {
- use crate::runtime::TryCurrentError;
+ use crate::runtime::{ThreadId, TryCurrentError};
use std::fmt;
+ pub(crate) fn thread_id() -> Result<ThreadId, AccessError> {
+ CONTEXT.try_with(|ctx| {
+ match ctx.thread_id.get() {
+ Some(id) => id,
+ None => {
+ let id = ThreadId::next();
+ ctx.thread_id.set(Some(id));
+ id
+ }
+ }
+ })
+ }
+
#[derive(Debug, Clone, Copy)]
#[must_use]
pub(crate) enum EnterRuntime {