Skip to content

Commit

Permalink
Add an unstable conversion from thread ID to u64
Browse files Browse the repository at this point in the history
We see multiple cases inside rustc and ecosystem code where ThreadId is
transmuted to u64, exploiting the underlying detail. This is suboptimal
(can break unexpectedly if we change things in std).

It is unlikely that ThreadId will ever need to be larger than u64 --
creating even 2^32 threads over the course of a program is quite hard,
2^64 is even harder. As such, we do not choose to return a larger sized
type (e.g. u128). If we choose to shrink ThreadId in the future, or
otherwise change its internals, it is likely that a mapping to u64 will
still be applicable (though may become more complex).
  • Loading branch information
Mark-Simulacrum committed Jan 6, 2020
1 parent a80e63f commit d9a7db9
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,19 @@ impl ThreadId {
ThreadId(NonZeroU64::new(id).unwrap())
}
}

/// This returns a numeric identifier for the thread identified by this
/// `ThreadId`.
///
/// As noted in the documentation for the type itself, it is essentially an
/// opaque ID, but is guaranteed to be unique for each thread. The returned
/// value is entirely opaque -- only equality testing is stable. Note that
/// it is not guaranteed which values new threads will return, and this may
/// change across Rust versions.
#[unstable(feature = "thread_id_value", issue = "67939")]
pub fn as_u64(&self) -> u64 {
self.0.get()
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit d9a7db9

Please sign in to comment.