about summary refs log tree commit diff
path: root/src/libstd/sys/common/thread_local.rs
diff options
context:
space:
mode:
authorAidan Cully <github@aidan.users.panix.com>2014-12-05 17:20:44 -0500
committerAidan Cully <github@aidan.users.panix.com>2014-12-05 17:20:44 -0500
commit7bf7bd6a75c38ed71df69cb9149eb7fdce23ced5 (patch)
tree5603049884b85f5f91a7287bbbdb0a70f891378d /src/libstd/sys/common/thread_local.rs
parent95d17711397d63425688d18140a58723caddff8e (diff)
downloadrust-7bf7bd6a75c38ed71df69cb9149eb7fdce23ced5.tar.gz
rust-7bf7bd6a75c38ed71df69cb9149eb7fdce23ced5.zip
work around portability issue on FreeBSD, in which the key returned from
pthread_key_create can be 0.
Diffstat (limited to 'src/libstd/sys/common/thread_local.rs')
-rw-r--r--src/libstd/sys/common/thread_local.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/libstd/sys/common/thread_local.rs b/src/libstd/sys/common/thread_local.rs
index 370d74cc5e1..b33e74248d2 100644
--- a/src/libstd/sys/common/thread_local.rs
+++ b/src/libstd/sys/common/thread_local.rs
@@ -185,7 +185,23 @@ impl StaticKey {
     }
 
     unsafe fn lazy_init(&self) -> uint {
-        let key = imp::create(self.dtor);
+        // POSIX allows the key created here to be 0, but the compare_and_swap
+        // below relies on using 0 as a sentinel value to check who won the
+        // race to set the shared TLS key. As far as I know, there is no
+        // guaranteed value that cannot be returned as a posix_key_create key,
+        // so there is no value we can initialize the inner key with to
+        // prove that it has not yet been set. As such, we'll continue using a
+        // value of 0, but with some gyrations to make sure we have a non-0
+        // value returned from the creation routine.
+        // TODO: this is clearly a hack, and should be cleaned up.
+        let key1 = imp::create(self.dtor);
+        let key = if key1 != 0 {
+            key1
+        } else {
+            let key2 = imp::create(self.dtor);
+            imp::destroy(key1);
+            key2
+        };
         assert!(key != 0);
         match self.inner.key.compare_and_swap(0, key as uint, atomic::SeqCst) {
             // The CAS succeeded, so we've created the actual key