1 //! Implements thread-local destructors that are not associated with any
4 #![unstable(feature = "thread_local_internals", issue = "none")]
5 #![cfg(target_thread_local)]
7 // Using a per-thread list avoids the problems in synchronizing global state.
9 static mut DESTRUCTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
11 // Ensure this can never be inlined because otherwise this may break in dylibs.
14 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
15 DESTRUCTORS.push((t, dtor));
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.
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
27 while let Some((ptr, dtor)) = DESTRUCTORS.pop() {
30 // We're done so free the memory.
31 DESTRUCTORS = Vec::new();