std/sys/thread_local/destructors/
list.rs1use crate::alloc::System;
2use crate::cell::RefCell;
3use crate::sys::thread_local::guard;
4
5#[thread_local]
6static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8)), System>> =
7 RefCell::new(Vec::new_in(System));
8
9pub unsafe fn register(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
10 let Ok(mut dtors) = DTORS.try_borrow_mut() else {
11 rtabort!("the System allocator may not use TLS with destructors")
12 };
13 guard::enable();
14
15 if dtors.capacity() == dtors.len() {
17 dtors.try_reserve(1).unwrap_or_else(|_| rtabort!("Failed to grow TLS destructor list"))
18 }
19 dtors.push((t, dtor));
20}
21
22pub unsafe fn run() {
30 loop {
31 let mut dtors = DTORS.borrow_mut();
32 match dtors.pop() {
33 Some((t, dtor)) => {
34 drop(dtors);
35 unsafe {
36 dtor(t);
37 }
38 }
39 None => {
40 *dtors = Vec::new_in(System);
42 break;
43 }
44 }
45 }
46}