@@ -163,6 +163,7 @@ use crate::ffi::{CStr, CString};
163
163
use crate :: fmt;
164
164
use crate :: io;
165
165
use crate :: mem;
166
+ use crate :: num:: NonZeroU64 ;
166
167
use crate :: panic;
167
168
use crate :: panicking;
168
169
use crate :: str;
@@ -1036,15 +1037,15 @@ pub fn park_timeout(dur: Duration) {
1036
1037
/// [`Thread`]: ../../std/thread/struct.Thread.html
1037
1038
#[ stable( feature = "thread_id" , since = "1.19.0" ) ]
1038
1039
#[ derive( Eq , PartialEq , Clone , Copy , Hash , Debug ) ]
1039
- pub struct ThreadId ( u64 ) ;
1040
+ pub struct ThreadId ( NonZeroU64 ) ;
1040
1041
1041
1042
impl ThreadId {
1042
1043
// Generate a new unique thread ID.
1043
1044
fn new ( ) -> ThreadId {
1044
1045
// We never call `GUARD.init()`, so it is UB to attempt to
1045
1046
// acquire this mutex reentrantly!
1046
1047
static GUARD : mutex:: Mutex = mutex:: Mutex :: new ( ) ;
1047
- static mut COUNTER : u64 = 0 ;
1048
+ static mut COUNTER : u64 = 1 ;
1048
1049
1049
1050
unsafe {
1050
1051
let _guard = GUARD . lock ( ) ;
@@ -1058,7 +1059,7 @@ impl ThreadId {
1058
1059
let id = COUNTER ;
1059
1060
COUNTER += 1 ;
1060
1061
1061
- ThreadId ( id )
1062
+ ThreadId ( NonZeroU64 :: new ( id ) . unwrap ( ) )
1062
1063
}
1063
1064
}
1064
1065
}
@@ -1484,9 +1485,10 @@ fn _assert_sync_and_send() {
1484
1485
mod tests {
1485
1486
use super :: Builder ;
1486
1487
use crate :: any:: Any ;
1488
+ use crate :: mem;
1487
1489
use crate :: sync:: mpsc:: { channel, Sender } ;
1488
1490
use crate :: result;
1489
- use crate :: thread;
1491
+ use crate :: thread:: { self , ThreadId } ;
1490
1492
use crate :: time:: Duration ;
1491
1493
use crate :: u32;
1492
1494
@@ -1716,6 +1718,11 @@ mod tests {
1716
1718
thread:: sleep ( Duration :: from_millis ( 2 ) ) ;
1717
1719
}
1718
1720
1721
+ #[ test]
1722
+ fn test_size_of_option_thread_id ( ) {
1723
+ assert_eq ! ( mem:: size_of:: <Option <ThreadId >>( ) , mem:: size_of:: <ThreadId >( ) ) ;
1724
+ }
1725
+
1719
1726
#[ test]
1720
1727
fn test_thread_id_equal ( ) {
1721
1728
assert ! ( thread:: current( ) . id( ) == thread:: current( ) . id( ) ) ;
0 commit comments