diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-03-22 19:31:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-22 19:31:27 +0100 |
| commit | d2a958f4227460aa0d6b42c2b3a24ad01ed3d388 (patch) | |
| tree | 64a37ec8b26af12b57e5150252e462118c1d6ffc | |
| parent | 28644cd9fafe92fb8ce14e15cada77b9757afc1a (diff) | |
| parent | c1d9191fa576c600775b4fbb90a3d09ca5e0fa0b (diff) | |
| download | rust-d2a958f4227460aa0d6b42c2b3a24ad01ed3d388.tar.gz rust-d2a958f4227460aa0d6b42c2b3a24ad01ed3d388.zip | |
Rollup merge of #59291 - SimonSapin:nonzero-thread-id, r=alexcrichton
Make Option<ThreadId> no larger than ThreadId, with NonZeroU64
| -rw-r--r-- | src/libstd/thread/mod.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 08f0aa2f0d2..d856f9b465e 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -163,6 +163,7 @@ use crate::ffi::{CStr, CString}; use crate::fmt; use crate::io; use crate::mem; +use crate::num::NonZeroU64; use crate::panic; use crate::panicking; use crate::str; @@ -1036,7 +1037,7 @@ pub fn park_timeout(dur: Duration) { /// [`Thread`]: ../../std/thread/struct.Thread.html #[stable(feature = "thread_id", since = "1.19.0")] #[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)] -pub struct ThreadId(u64); +pub struct ThreadId(NonZeroU64); impl ThreadId { // Generate a new unique thread ID. @@ -1044,7 +1045,7 @@ impl ThreadId { // We never call `GUARD.init()`, so it is UB to attempt to // acquire this mutex reentrantly! static GUARD: mutex::Mutex = mutex::Mutex::new(); - static mut COUNTER: u64 = 0; + static mut COUNTER: u64 = 1; unsafe { let _guard = GUARD.lock(); @@ -1058,7 +1059,7 @@ impl ThreadId { let id = COUNTER; COUNTER += 1; - ThreadId(id) + ThreadId(NonZeroU64::new(id).unwrap()) } } } @@ -1484,9 +1485,10 @@ fn _assert_sync_and_send() { mod tests { use super::Builder; use crate::any::Any; + use crate::mem; use crate::sync::mpsc::{channel, Sender}; use crate::result; - use crate::thread; + use crate::thread::{self, ThreadId}; use crate::time::Duration; use crate::u32; @@ -1717,6 +1719,11 @@ mod tests { } #[test] + fn test_size_of_option_thread_id() { + assert_eq!(mem::size_of::<Option<ThreadId>>(), mem::size_of::<ThreadId>()); + } + + #[test] fn test_thread_id_equal() { assert!(thread::current().id() == thread::current().id()); } |
