xref: /drstd/src/std/sys/hermit/thread_local_dtor.rs (revision 9670759b785600bf6315e4173e46a602f16add7a)
1 #![cfg(target_thread_local)]
2 
3 // Simplify dtor registration by using a list of destructors.
4 // The this solution works like the implementation of macOS and
5 // doesn't additional OS support
6 
7 use crate::std::mem;
8 
9 #[thread_local]
10 static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
11 
register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8))12 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
13     let list = &mut DTORS;
14     list.push((t, dtor));
15 }
16 
17 // every thread call this function to run through all possible destructors
run_dtors()18 pub unsafe fn run_dtors() {
19     let mut list = mem::take(&mut DTORS);
20     while !list.is_empty() {
21         for (ptr, dtor) in list {
22             dtor(ptr);
23         }
24         list = mem::take(&mut DTORS);
25     }
26 }
27