File tree 1 file changed +17
-1
lines changed
1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -185,7 +185,23 @@ impl StaticKey {
185
185
}
186
186
187
187
unsafe fn lazy_init ( & self ) -> uint {
188
- let key = imp:: create ( self . dtor ) ;
188
+ // POSIX allows the key created here to be 0, but the compare_and_swap
189
+ // below relies on using 0 as a sentinel value to check who won the
190
+ // race to set the shared TLS key. As far as I know, there is no
191
+ // guaranteed value that cannot be returned as a posix_key_create key,
192
+ // so there is no value we can initialize the inner key with to
193
+ // prove that it has not yet been set. As such, we'll continue using a
194
+ // value of 0, but with some gyrations to make sure we have a non-0
195
+ // value returned from the creation routine.
196
+ // TODO: this is clearly a hack, and should be cleaned up.
197
+ let key1 = imp:: create ( self . dtor ) ;
198
+ let key = if key1 != 0 {
199
+ key1
200
+ } else {
201
+ let key2 = imp:: create ( self . dtor ) ;
202
+ imp:: destroy ( key1) ;
203
+ key2
204
+ } ;
189
205
assert ! ( key != 0 ) ;
190
206
match self . inner . key . compare_and_swap ( 0 , key as uint , atomic:: SeqCst ) {
191
207
// The CAS succeeded, so we've created the actual key
You can’t perform that action at this time.
0 commit comments