summaryrefslogtreecommitdiffstats
path: root/library/std/src/sys/hermit/thread_local_dtor.rs
blob: 98adaf4bff1aa49b7370983e2fe0275dd6a2ff4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#![cfg(target_thread_local)]
#![unstable(feature = "thread_local_internals", issue = "none")]

// Simplify dtor registration by using a list of destructors.
// The this solution works like the implementation of macOS and
// doesn't additional OS support

use crate::cell::RefCell;

#[thread_local]
static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>> = RefCell::new(Vec::new());

pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
    match DTORS.try_borrow_mut() {
        Ok(mut dtors) => dtors.push((t, dtor)),
        Err(_) => rtabort!("global allocator may not use TLS"),
    }
}

// every thread call this function to run through all possible destructors
pub unsafe fn run_dtors() {
    let mut list = DTORS.take();
    while !list.is_empty() {
        for (ptr, dtor) in list {
            dtor(ptr);
        }
        list = DTORS.take();
    }
}