]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/thread_local_dtor.rs
Rollup merge of #74266 - GuillaumeGomez:cleanup-e0720, r=Dylan-DPC
[rust.git] / library / std / src / sys / hermit / thread_local_dtor.rs
1 #![cfg(target_thread_local)]
2 #![unstable(feature = "thread_local_internals", issue = "none")]
3
4 // Simplify dtor registration by using a list of destructors.
5 // The this solution works like the implementation of macOS and
6 // doesn't additional OS support
7
8 use crate::cell::Cell;
9 use crate::ptr;
10
11 #[thread_local]
12 static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
13
14 type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
15
16 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
17     if DTORS.get().is_null() {
18         let v: Box<List> = box Vec::new();
19         DTORS.set(Box::into_raw(v));
20     }
21
22     let list: &mut List = &mut *DTORS.get();
23     list.push((t, dtor));
24 }
25
26 // every thread call this function to run through all possible destructors
27 pub unsafe fn run_dtors() {
28     let mut ptr = DTORS.replace(ptr::null_mut());
29     while !ptr.is_null() {
30         let list = Box::from_raw(ptr);
31         for (ptr, dtor) in list.into_iter() {
32             dtor(ptr);
33         }
34         ptr = DTORS.replace(ptr::null_mut());
35     }
36 }