11//! Wraps [`std::thread::ThreadId`], providng faster lookup.
22use core:: borrow:: Borrow ;
3+ #[ cfg( not( feature = "nightly" ) ) ]
34use core:: cell:: Cell ;
45use core:: ops:: Deref ;
56use std:: thread:: ThreadId ;
67
8+ use cfg_if:: cfg_if;
79use equivalent:: Equivalent ;
810
11+ #[ cfg( not( feature = "nightly" ) ) ]
912fast_thread_local ! {
1013 static STD_TID : Cell <Option <StdThreadId >> = Cell :: new( None ) ;
1114}
@@ -24,14 +27,20 @@ impl StdThreadId {
2427 /// Lookup the [`std::thread::ThreadId`] of the current thread.
2528 #[ inline]
2629 pub fn current ( ) -> Self {
27- STD_TID . with ( |cell| match cell. get ( ) {
28- None => {
29- let new_id = Self :: acquire ( ) ;
30- cell. set ( Some ( new_id) ) ;
31- new_id
30+ cfg_if ! {
31+ if #[ cfg( feature = "nightly" ) ] {
32+ StdThreadId ( std:: thread:: current_id( ) )
33+ } else {
34+ STD_TID . with( |cell| match cell. get( ) {
35+ None => {
36+ let new_id = Self :: acquire( ) ;
37+ cell. set( Some ( new_id) ) ;
38+ new_id
39+ }
40+ Some ( existing) => existing,
41+ } )
3242 }
33- Some ( existing) => existing,
34- } )
43+ }
3544 }
3645}
3746#[ cfg_attr( feature = "nightly-docs" , doc( cfg( feature = "std" ) ) ) ]
@@ -55,6 +64,7 @@ unsafe impl crate::IThreadId for ThreadId {
5564}
5665impl StdThreadId {
5766 #[ cold]
67+ #[ cfg( not( feature = "nightly" ) ) ]
5868 fn acquire ( ) -> StdThreadId {
5969 StdThreadId ( std:: thread:: current ( ) . id ( ) )
6070 }
0 commit comments