Skip to content

Commit 97b5f45

Browse files
committed
windows LazyKey: fix enabling the guard on all relevant threads
1 parent 7602e3f commit 97b5f45

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

library/std/src/sys/thread_local/key/windows.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ impl LazyKey {
6060

6161
#[inline]
6262
pub fn force(&'static self) -> Key {
63+
if self.dtor.is_some() {
64+
// Needs to be called on all threads where the key might have a non-null value!
65+
// Otherwise, `run_dtors` might not be called on this thread.
66+
guard::enable();
67+
}
68+
6369
match self.key.load(Acquire) {
6470
0 => unsafe { self.init() },
6571
key => key - 1,
@@ -88,6 +94,8 @@ impl LazyKey {
8894
}
8995

9096
unsafe {
97+
// Add ourselves to the `DTORS` list, so that when `run_dtors` gets called,
98+
// our dtor is invoked.
9199
register_dtor(self);
92100
}
93101

@@ -144,8 +152,6 @@ static DTORS: Atomic<*mut LazyKey> = AtomicPtr::new(ptr::null_mut());
144152
/// Should only be called once per key, otherwise loops or breaks may occur in
145153
/// the linked list.
146154
unsafe fn register_dtor(key: &'static LazyKey) {
147-
guard::enable();
148-
149155
let this = <*const LazyKey>::cast_mut(key);
150156
// Use acquire ordering to pass along the changes done by the previously
151157
// registered keys when we store the new head with release ordering.

0 commit comments

Comments
 (0)