@@ -4,7 +4,7 @@ use crate::sys::thread_local::{abort_on_dtor_unwind, destructors};
44
55#[ derive( Clone , Copy ) ]
66enum State {
7- Initial ,
7+ Uninitialized ,
88 Alive ,
99 Destroyed ,
1010}
@@ -17,7 +17,7 @@ pub struct Storage<T> {
1717
1818impl < T > Storage < T > {
1919 pub const fn new ( val : T ) -> Storage < T > {
20- Storage { state : Cell :: new ( State :: Initial ) , val : UnsafeCell :: new ( val) }
20+ Storage { state : Cell :: new ( State :: Uninitialized ) , val : UnsafeCell :: new ( val) }
2121 }
2222
2323 /// Gets a pointer to the TLS value. If the TLS variable has been destroyed,
@@ -28,18 +28,25 @@ impl<T> Storage<T> {
2828 ///
2929 /// # Safety
3030 /// The `self` reference must remain valid until the TLS destructor is run.
31- #[ inline]
31+ #[ inline( always ) ]
3232 pub unsafe fn get ( & self ) -> * const T {
33- match self . state . get ( ) {
34- State :: Alive => self . val . get ( ) ,
35- State :: Destroyed => ptr :: null ( ) ,
36- State :: Initial => unsafe { self . initialize ( ) } ,
33+ if let State :: Alive = self . state . get ( ) {
34+ self . val . get ( )
35+ } else {
36+ unsafe { self . get_or_init_slow ( ) }
3737 }
3838 }
3939
4040 #[ cold]
41- unsafe fn initialize ( & self ) -> * const T {
42- // Register the destructor
41+ #[ inline( never) ]
42+ unsafe fn get_or_init_slow ( & self ) -> * const T {
43+ match self . state . get ( ) {
44+ State :: Uninitialized => { }
45+ State :: Alive => return self . val . get ( ) ,
46+ State :: Destroyed => return ptr:: null ( ) ,
47+ }
48+
49+ // Register the destructor.
4350
4451 // SAFETY:
4552 // The caller guarantees that `self` will be valid until thread destruction.
0 commit comments