1use crate::cell::{Cell, UnsafeCell};
2use crate::ptr::{self, drop_in_place};
3use crate::sys::thread_local::{abort_on_dtor_unwind, destructors};
45#[derive(Clone, Copy)]
6enum State {
7 Initial,
8 Alive,
9 Destroyed,
10}
1112#[allow(missing_debug_implementations)]
13pub struct Storage<T> {
14 state: Cell<State>,
15 val: UnsafeCell<T>,
16}
1718impl<T> Storage<T> {
19pub const fn new(val: T) -> Storage<T> {
20 Storage { state: Cell::new(State::Initial), val: UnsafeCell::new(val) }
21 }
2223/// Gets a pointer to the TLS value. If the TLS variable has been destroyed,
24 /// a null pointer is returned.
25 ///
26 /// The resulting pointer may not be used after thread destruction has
27 /// occurred.
28 ///
29 /// # Safety
30 /// The `self` reference must remain valid until the TLS destructor is run.
31#[inline]
32pub unsafe fn get(&self) -> *const T {
33match self.state.get() {
34 State::Alive => self.val.get(),
35 State::Destroyed => ptr::null(),
36 State::Initial => unsafe { self.initialize() },
37 }
38 }
3940#[cold]
41unsafe fn initialize(&self) -> *const T {
42// Register the destructor
4344 // SAFETY:
45 // The caller guarantees that `self` will be valid until thread destruction.
46unsafe {
47 destructors::register(ptr::from_ref(self).cast_mut().cast(), destroy::<T>);
48 }
4950self.state.set(State::Alive);
51self.val.get()
52 }
53}
5455/// Transition an `Alive` TLS variable into the `Destroyed` state, dropping its
56/// value.
57///
58/// # Safety
59/// * Must only be called at thread destruction.
60/// * `ptr` must point to an instance of `Storage` with `Alive` state and be
61/// valid for accessing that instance.
62unsafe extern "C" fn destroy<T>(ptr: *mut u8) {
63// Print a nice abort message if a panic occurs.
64abort_on_dtor_unwind(|| {
65let storage = unsafe { &*(ptr as *const Storage<T>) };
66// Update the state before running the destructor as it may attempt to
67 // access the variable.
68storage.state.set(State::Destroyed);
69unsafe {
70 drop_in_place(storage.val.get());
71 }
72 })
73}