]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/thread_local_dtor.rs
Rollup merge of #106273 - notriddle:notriddle/source-content-overflow, r=GuillaumeGomez
[rust.git] / library / std / src / sys / windows / thread_local_dtor.rs
1 //! Implements thread-local destructors that are not associated with any
2 //! particular data.
3
4 #![unstable(feature = "thread_local_internals", issue = "none")]
5 #![cfg(target_thread_local)]
6
7 // Using a per-thread list avoids the problems in synchronizing global state.
8 #[thread_local]
9 static mut DESTRUCTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
10
11 // Ensure this can never be inlined because otherwise this may break in dylibs.
12 // See #44391.
13 #[inline(never)]
14 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
15     DESTRUCTORS.push((t, dtor));
16 }
17
18 #[inline(never)] // See comment above
19 /// Runs destructors. This should not be called until thread exit.
20 pub unsafe fn run_keyless_dtors() {
21     // Drop all the destructors.
22     //
23     // Note: While this is potentially an infinite loop, it *should* be
24     // the case that this loop always terminates because we provide the
25     // guarantee that a TLS key cannot be set after it is flagged for
26     // destruction.
27     while let Some((ptr, dtor)) = DESTRUCTORS.pop() {
28         (dtor)(ptr);
29     }
30     // We're done so free the memory.
31     DESTRUCTORS = Vec::new();
32 }