Add ThreadId for comparing threads

This commit is contained in:
Stephen M. Coakley 2016-09-07 23:48:07 -05:00
parent 9627e9ef6e
commit 5bd834bdb4
No known key found for this signature in database
GPG key ID: 91B59C18494C4FC0

View file

@ -165,6 +165,7 @@ use panic;
use panicking;
use str;
use sync::{Mutex, Condvar, Arc};
use sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use sys::thread as imp;
use sys_common::thread_info;
use sys_common::util;
@ -524,6 +525,35 @@ pub fn park_timeout(dur: Duration) {
*guard = false;
}
////////////////////////////////////////////////////////////////////////////////
// ThreadId
////////////////////////////////////////////////////////////////////////////////
/// A unique identifier for a running thread.
///
/// A `ThreadId` is an opaque object that has a unique value for each thread
/// that creates one. `ThreadId`s do not correspond to a thread's system-
/// designated identifier.
#[unstable(feature = "thread_id", issue = "21507")]
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct ThreadId(usize);
impl ThreadId {
/// Returns an identifier unique to the current calling thread.
#[unstable(feature = "thread_id", issue = "21507")]
pub fn current() -> ThreadId {
static THREAD_ID_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
#[thread_local] static mut THREAD_ID: ThreadId = ThreadId(0);
unsafe {
if THREAD_ID.0 == 0 {
THREAD_ID.0 = 1 + THREAD_ID_COUNT.fetch_add(1, Ordering::SeqCst);
}
THREAD_ID
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Thread
////////////////////////////////////////////////////////////////////////////////
@ -977,6 +1007,16 @@ mod tests {
thread::sleep(Duration::from_millis(2));
}
#[test]
fn test_thread_id_equal() {
assert_eq!(ThreadId::current(), ThreadId::current());
}
#[test]
fn test_thread_id_not_equal() {
assert!(ThreadId::current() != spawn(|| ThreadId::current()).join());
}
// NOTE: the corresponding test for stderr is in run-pass/thread-stderr, due
// to the test harness apparently interfering with stderr configuration.
}