Skip to content

Commit

Permalink
add comments explaining main thread identification
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Jan 14, 2025
1 parent 1d25252 commit 2c28cc4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/std/src/thread/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ where
{
let current = CURRENT.get();
if current > DESTROYED {
// SAFETY: `Arc` does not contain interior mutability, so it does not
// matter that the address of the handle might be different depending
// on where this is called.
unsafe {
let current = ManuallyDrop::new(Thread::from_raw(current));
f(Some(&current))
Expand Down
26 changes: 26 additions & 0 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,18 @@ mod thread_name_string {

use thread_name_string::ThreadNameString;

/// Store the ID of the main thread.
///
/// The thread handle for the main thread is created lazily, and this might even
/// happen pre-main. Since not every platform has a way to identify the main
/// thread when that happens – macOS's `pthread_main_np` function being a notable
/// exception – we cannot assign it the right name right then. Instead, in our
/// runtime startup code, we remember the thread ID of the main thread (through
/// this modules `set` function) and use it to identify the main thread from then
/// on. This works reliably and has the additional advantage that we can report
/// the right thread name on main even after the thread handle has been destroyed.
/// Note however that this also means that the name reported in pre-main functions
/// will be incorrect, but that's just something we have to live with.
pub(crate) mod main_thread {
cfg_if::cfg_if! {
if #[cfg(target_has_atomic = "64")] {
Expand Down Expand Up @@ -1331,21 +1343,35 @@ pub(crate) mod main_thread {
}
}

/// Run a function with the current thread's name.
///
/// Modulo thread local accesses, this function is safe to call from signal
/// handlers and in similar circumstances where allocations are not possible.
pub(crate) fn with_current_name<F, R>(f: F) -> R
where
F: FnOnce(Option<&str>) -> R,
{
try_with_current(|thread| {
if let Some(thread) = thread {
// If there is a current thread handle, try to use the name stored
// there.
if let Some(name) = &thread.inner.name {
return f(Some(name.as_str()));
} else if Some(thread.inner.id) == main_thread::get() {
// The main thread doesn't store its name in the handle, we must
// identify it through its ID. Since we already have the `Thread`,
// we can retrieve the ID from it instead of going through another
// thread local.
return f(Some("main"));
}
} else if let Some(main) = main_thread::get()
&& let Some(id) = current::id::get()
&& id == main
{
// The main thread doesn't always have a thread handle, we must
// identify it through its ID instead. The checks are ordered so
// that the current ID is only loaded if it is actually needed,
// since loading it from TLS might need multiple expensive accesses.
return f(Some("main"));
}

Expand Down

0 comments on commit 2c28cc4

Please sign in to comment.