@@ -97,29 +97,26 @@ cfg_if::cfg_if! {
97
97
}
98
98
}
99
99
100
- /// Attempts to get the current thread if `get` has previously been
101
- /// called.
102
- #[ inline]
103
- pub ( crate ) fn try_get( ) -> Option <Thread > {
104
- unsafe {
105
- THREAD
106
- }
107
- }
108
-
109
100
/// Returns a thread ID for the current thread, allocating one if needed.
110
101
#[ inline]
111
102
pub ( crate ) fn get( ) -> Thread {
112
103
if let Some ( thread) = unsafe { THREAD } {
113
104
thread
114
105
} else {
115
- let new = Thread :: new( THREAD_ID_MANAGER . lock( ) . unwrap( ) . alloc( ) ) ;
116
- unsafe {
117
- THREAD = Some ( new) ;
118
- }
119
- THREAD_GUARD . with( |_| { } ) ;
120
- new
106
+ get_slow( )
121
107
}
122
108
}
109
+
110
+ /// Out-of-line slow path for allocating a thread ID.
111
+ #[ cold]
112
+ fn get_slow( ) -> Thread {
113
+ let new = Thread :: new( THREAD_ID_MANAGER . lock( ) . unwrap( ) . alloc( ) ) ;
114
+ unsafe {
115
+ THREAD = Some ( new) ;
116
+ }
117
+ THREAD_GUARD . with( |_| { } ) ;
118
+ new
119
+ }
123
120
} else {
124
121
use std:: cell:: Cell ;
125
122
@@ -140,27 +137,26 @@ cfg_if::cfg_if! {
140
137
}
141
138
}
142
139
143
- /// Attempts to get the current thread if `get` has previously been
144
- /// called.
145
- #[ inline]
146
- pub ( crate ) fn try_get( ) -> Option <Thread > {
147
- THREAD . with( |thread| thread. get( ) )
148
- }
149
-
150
140
/// Returns a thread ID for the current thread, allocating one if needed.
151
141
#[ inline]
152
142
pub ( crate ) fn get( ) -> Thread {
153
143
THREAD . with( |thread| {
154
144
if let Some ( thread) = thread. get( ) {
155
145
thread
156
146
} else {
157
- let new = Thread :: new( THREAD_ID_MANAGER . lock( ) . unwrap( ) . alloc( ) ) ;
158
- thread. set( Some ( new) ) ;
159
- THREAD_GUARD . with( |_| { } ) ;
160
- new
147
+ get_slow( thread)
161
148
}
162
149
} )
163
150
}
151
+
152
+ /// Out-of-line slow path for allocating a thread ID.
153
+ #[ cold]
154
+ fn get_slow( thread: & Cell <Option <Thread >>) -> Thread {
155
+ let new = Thread :: new( THREAD_ID_MANAGER . lock( ) . unwrap( ) . alloc( ) ) ;
156
+ thread. set( Some ( new) ) ;
157
+ THREAD_GUARD . with( |_| { } ) ;
158
+ new
159
+ }
164
160
}
165
161
}
166
162
0 commit comments