1 #![cfg(target_thread_local)]
2 #![unstable(feature = "thread_local_internals", issue = "none")]
4 // Simplify dtor registration by using a list of destructors.
6 use super::{abi, itron::task};
11 static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
13 type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
15 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
16 if DTORS.get().is_null() {
17 let tid = task::current_task_id_aborting();
18 let v: Box<List> = box Vec::new();
19 DTORS.set(Box::into_raw(v));
21 // Register `tls_dtor` to make sure the TLS destructors are called
22 // for tasks created by other means than `std::thread`
23 unsafe { abi::SOLID_TLS_AddDestructor(tid as i32, tls_dtor) };
26 let list: &mut List = unsafe { &mut *DTORS.get() };
30 pub unsafe fn run_dtors() {
31 let ptr = DTORS.get();
33 // Swap the destructor list, call all registered destructors,
34 // and repeat this until the list becomes permanently empty.
35 while let Some(list) = Some(crate::mem::replace(unsafe { &mut *ptr }, Vec::new()))
36 .filter(|list| !list.is_empty())
38 for (ptr, dtor) in list.into_iter() {
43 // Drop the destructor list
44 unsafe { Box::from_raw(DTORS.replace(ptr::null_mut())) };
48 unsafe extern "C" fn tls_dtor(_unused: *mut u8) {
49 unsafe { run_dtors() };