1use crate::cell::UnsafeCell;
2use crate::hint::unreachable_unchecked;
3use crate::ptr;
4use crate::sys::thread_local::{abort_on_dtor_unwind, destructors};
56pub unsafe trait DestroyedState: Sized {
7fn register_dtor<T>(s: &Storage<T, Self>);
8}
910unsafe impl DestroyedState for ! {
11fn register_dtor<T>(_: &Storage<T, !>) {}
12}
1314unsafe impl DestroyedState for () {
15fn register_dtor<T>(s: &Storage<T, ()>) {
16unsafe {
17 destructors::register(ptr::from_ref(s).cast_mut().cast(), destroy::<T>);
18 }
19 }
20}
2122enum State<T, D> {
23 Initial,
24 Alive(T),
25 Destroyed(D),
26}
2728#[allow(missing_debug_implementations)]
29pub struct Storage<T, D> {
30 state: UnsafeCell<State<T, D>>,
31}
3233impl<T, D> Storage<T, D>
34where
35D: DestroyedState,
36{
37pub const fn new() -> Storage<T, D> {
38 Storage { state: UnsafeCell::new(State::Initial) }
39 }
4041/// Gets a pointer to the TLS value, potentially initializing it with the
42 /// provided parameters. If the TLS variable has been destroyed, a null
43 /// pointer is returned.
44 ///
45 /// The resulting pointer may not be used after reentrant inialialization
46 /// or thread destruction has occurred.
47 ///
48 /// # Safety
49 /// The `self` reference must remain valid until the TLS destructor is run.
50#[inline]
51pub unsafe fn get_or_init(&self, i: Option<&mut Option<T>>, f: impl FnOnce() -> T) -> *const T {
52let state = unsafe { &*self.state.get() };
53match state {
54 State::Alive(v) => v,
55 State::Destroyed(_) => ptr::null(),
56 State::Initial => unsafe { self.initialize(i, f) },
57 }
58 }
5960#[cold]
61unsafe fn initialize(&self, i: Option<&mut Option<T>>, f: impl FnOnce() -> T) -> *const T {
62// Perform initialization
6364let v = i.and_then(Option::take).unwrap_or_else(f);
6566let old = unsafe { self.state.get().replace(State::Alive(v)) };
67match old {
68// If the variable is not being recursively initialized, register
69 // the destructor. This might be a noop if the value does not need
70 // destruction.
71State::Initial => D::register_dtor(self),
72// Else, drop the old value. This might be changed to a panic.
73val => drop(val),
74 }
7576// SAFETY: the state was just set to `Alive`
77unsafe {
78let State::Alive(v) = &*self.state.get() else { unreachable_unchecked() };
79 v
80 }
81 }
82}
8384/// Transition an `Alive` TLS variable into the `Destroyed` state, dropping its
85/// value.
86///
87/// # Safety
88/// * Must only be called at thread destruction.
89/// * `ptr` must point to an instance of `Storage<T, ()>` and be valid for
90/// accessing that instance.
91unsafe extern "C" fn destroy<T>(ptr: *mut u8) {
92// Print a nice abort message if a panic occurs.
93abort_on_dtor_unwind(|| {
94let storage = unsafe { &*(ptr as *const Storage<T, ()>) };
95// Update the state before running the destructor as it may attempt to
96 // access the variable.
97let val = unsafe { storage.state.get().replace(State::Destroyed(())) };
98 drop(val);
99 })
100}