about summary refs log tree commit diff
path: root/src/libstd/rt/local_ptr.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-11-27 09:57:05 -0800
committerbors <bors@rust-lang.org>2013-11-27 09:57:05 -0800
commite4136bd552bc7bf2b83ceb5d114f9a254bfa2b01 (patch)
tree3b5accbf93dabd0921e46e326918a1c0e5bef263 /src/libstd/rt/local_ptr.rs
parenta6fc577ab580d09f05bb9b545b6a6511cfcb0a8f (diff)
parent5d6dbf3f262fabcb6cb920dd08be6f9d8df75d5c (diff)
downloadrust-e4136bd552bc7bf2b83ceb5d114f9a254bfa2b01.tar.gz
rust-e4136bd552bc7bf2b83ceb5d114f9a254bfa2b01.zip
auto merge of #10662 : alexcrichton/rust/thread-detach, r=pcwalton
This has one commit from a separate pull request (because these commits depend on that one), but otherwise the extra details can be found in the commit messages. The `rt::thread` module has been generally cleaned up for everyday safe usage (and it's a bug if it's not safe).
Diffstat (limited to 'src/libstd/rt/local_ptr.rs')
-rw-r--r--src/libstd/rt/local_ptr.rs38
1 files changed, 30 insertions, 8 deletions
diff --git a/src/libstd/rt/local_ptr.rs b/src/libstd/rt/local_ptr.rs
index e0e8750e146..6355de36d43 100644
--- a/src/libstd/rt/local_ptr.rs
+++ b/src/libstd/rt/local_ptr.rs
@@ -41,27 +41,49 @@ pub static mut RT_TLS_PTR: *mut c_void = 0 as *mut c_void;
 #[cfg(stage0)]
 #[cfg(windows)]
 static mut RT_TLS_KEY: tls::Key = -1;
+#[cfg(stage0)]
+#[cfg(windows)]
+static mut tls_lock: Mutex = MUTEX_INIT;
+static mut tls_initialized: bool = false;
 
 /// Initialize the TLS key. Other ops will fail if this isn't executed first.
 #[inline(never)]
 #[cfg(stage0)]
 #[cfg(windows)]
 pub fn init_tls_key() {
-    static mut lock: Mutex = MUTEX_INIT;
-    static mut initialized: bool = false;
-
     unsafe {
-        lock.lock();
-        if !initialized {
+        tls_lock.lock();
+        if !tls_initialized {
             tls::create(&mut RT_TLS_KEY);
-            initialized = true;
+            tls_initialized = true;
         }
-        lock.unlock();
+        tls_lock.unlock();
     }
 }
 
 #[cfg(not(stage0), not(windows))]
-pub fn init_tls_key() {}
+pub fn init_tls_key() {
+    unsafe {
+        tls_initialized = true;
+    }
+}
+
+#[cfg(windows)]
+pub unsafe fn cleanup() {
+    // No real use to acquiring a lock around these operations. All we're
+    // going to do is destroy the lock anyway which races locking itself. This
+    // is why the whole function is labeled as 'unsafe'
+    assert!(tls_initialized);
+    tls::destroy(RT_TLS_KEY);
+    tls_lock.destroy();
+    tls_initialized = false;
+}
+
+#[cfg(not(windows))]
+pub unsafe fn cleanup() {
+    assert!(tls_initialized);
+    tls_initialized = false;
+}
 
 /// Give a pointer to thread-local storage.
 ///