Skip to content

Add fast-path for accessing the current thread id #143069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions library/std/src/thread/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,32 @@ pub(super) fn set_current(thread: Thread) -> Result<(), Thread> {
Ok(())
}

/// Gets the id of the thread that invokes it.
/// Gets the unique identifier of the thread which invokes it.
///
/// Calling this function may be more efficient than accessing the current
/// thread id through the current thread handle. i.e. `thread::current().id()`.
///
/// This function will always succeed, will always return the same value for
/// one thread and is guaranteed not to call the global allocator.
///
/// # Examples
///
/// ```
/// #![feature(current_thread_id)]
///
/// use std::thread;
///
/// let other_thread = thread::spawn(|| {
/// thread::current_id()
/// });
///
/// let other_thread_id = other_thread.join().unwrap();
/// assert!(thread::current_id() != other_thread_id);
/// ```
#[inline]
pub(crate) fn current_id() -> ThreadId {
#[must_use]
#[unstable(feature = "current_thread_id", issue = "none")]
pub fn current_id() -> ThreadId {
// If accessing the persistent thread ID takes multiple TLS accesses, try
// to retrieve it from the current thread handle, which will only take one
// TLS access.
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ mod current;

#[stable(feature = "rust1", since = "1.0.0")]
pub use current::current;
pub(crate) use current::{current_id, current_or_unnamed, drop_current};
#[unstable(feature = "current_thread_id", issue = "none")]
pub use current::current_id;
pub(crate) use current::{current_or_unnamed, drop_current};
use current::{set_current, try_with_current};

mod spawnhook;
Expand Down
Loading