File tree Expand file tree Collapse file tree
library/std/src/sys/thread_local/key Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
146154unsafe 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.
You can’t perform that action at this time.
0 commit comments