@@ -4,7 +4,7 @@ use crate::sys::thread_local::{abort_on_dtor_unwind, destructors};
4
4
5
5
#[ derive( Clone , Copy ) ]
6
6
enum State {
7
- Initial ,
7
+ Uninitialized ,
8
8
Alive ,
9
9
Destroyed ,
10
10
}
@@ -17,7 +17,7 @@ pub struct Storage<T> {
17
17
18
18
impl < T > Storage < T > {
19
19
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) }
21
21
}
22
22
23
23
/// Gets a pointer to the TLS value. If the TLS variable has been destroyed,
@@ -28,18 +28,25 @@ impl<T> Storage<T> {
28
28
///
29
29
/// # Safety
30
30
/// The `self` reference must remain valid until the TLS destructor is run.
31
- #[ inline]
31
+ #[ inline( always ) ]
32
32
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 ( ) }
37
37
}
38
38
}
39
39
40
40
#[ 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.
43
50
44
51
// SAFETY:
45
52
// The caller guarantees that `self` will be valid until thread destruction.
0 commit comments