about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-12-10 11:07:39 +0100
committerRalf Jung <post@ralfj.de>2020-12-10 11:07:39 +0100
commit594b451ccc65329a3f5e60f5b6690bcd0f7e9e7b (patch)
treef07f28d007e73c068d990a269e9fe985b3803c8e
parente413d89aa706060ddc347e1e06d551ec86d3f471 (diff)
downloadrust-594b451ccc65329a3f5e60f5b6690bcd0f7e9e7b.tar.gz
rust-594b451ccc65329a3f5e60f5b6690bcd0f7e9e7b.zip
Windows TLS: ManuallyDrop instead of mem::forget
-rw-r--r--library/std/src/sys/windows/thread_local_key.rs11
1 files changed, 4 insertions, 7 deletions
diff --git a/library/std/src/sys/windows/thread_local_key.rs b/library/std/src/sys/windows/thread_local_key.rs
index fb2bb0613ee..065365e5572 100644
--- a/library/std/src/sys/windows/thread_local_key.rs
+++ b/library/std/src/sys/windows/thread_local_key.rs
@@ -1,4 +1,4 @@
-use crate::mem;
+use crate::mem::ManuallyDrop;
 use crate::ptr;
 use crate::sync::atomic::AtomicPtr;
 use crate::sync::atomic::Ordering::SeqCst;
@@ -111,16 +111,13 @@ struct Node {
 }
 
 unsafe fn register_dtor(key: Key, dtor: Dtor) {
-    let mut node = Box::new(Node { key, dtor, next: ptr::null_mut() });
+    let mut node = ManuallyDrop::new(Box::new(Node { key, dtor, next: ptr::null_mut() }));
 
     let mut head = DTORS.load(SeqCst);
     loop {
         node.next = head;
-        match DTORS.compare_exchange(head, &mut *node, SeqCst, SeqCst) {
-            Ok(_) => {
-                mem::forget(node);
-                return;
-            }
+        match DTORS.compare_exchange(head, &mut **node, SeqCst, SeqCst) {
+            Ok(_) => return, // nothing to drop, we successfully added the node to the list
             Err(cur) => head = cur,
         }
     }