1use crate::cell::{Cell, UnsafeCell};
2use crate::ptr::{self, drop_in_place};
3use crate::sys::thread_local::{abort_on_dtor_unwind, destructors};
45#[derive(#[automatically_derived]
impl ::core::clone::Clone for State {
#[inline]
fn clone(&self) -> State { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for State { }Copy)]
6enum State {
7 Initial,
8 Alive,
9 Destroyed,
10}
1112#[allow(missing_debug_implementations)]
13#[repr(C)]
14pub struct Storage<T> {
15// This field must be first, for correctness of `#[rustc_align_static]`
16val: UnsafeCell<T>,
17 state: Cell<State>,
18}
1920impl<T> Storage<T> {
21pub const fn new(val: T) -> Storage<T> {
22Storage { state: Cell::new(State::Initial), val: UnsafeCell::new(val) }
23 }
2425/// Gets a pointer to the TLS value. If the TLS variable has been destroyed,
26 /// a null pointer is returned.
27 ///
28 /// The resulting pointer may not be used after thread destruction has
29 /// occurred.
30 ///
31 /// # Safety
32 /// The `self` reference must remain valid until the TLS destructor is run.
33#[inline]
34pub unsafe fn get(&self) -> *const T {
35match self.state.get() {
36 State::Alive => self.val.get(),
37 State::Destroyed => ptr::null(),
38 State::Initial => unsafe { self.initialize() },
39 }
40 }
4142#[cold]
43unsafe fn initialize(&self) -> *const T {
44// Register the destructor
4546 // SAFETY:
47 // The caller guarantees that `self` will be valid until thread destruction.
48unsafe {
49 destructors::register(ptr::from_ref(self).cast_mut().cast(), destroy::<T>);
50 }
5152self.state.set(State::Alive);
53self.val.get()
54 }
55}
5657/// Transition an `Alive` TLS variable into the `Destroyed` state, dropping its
58/// value.
59///
60/// # Safety
61/// * Must only be called at thread destruction.
62/// * `ptr` must point to an instance of `Storage` with `Alive` state and be
63/// valid for accessing that instance.
64unsafe extern "C" fn destroy<T>(ptr: *mut u8) {
65// Print a nice abort message if a panic occurs.
66abort_on_dtor_unwind(|| {
67let storage = unsafe { &*(ptras *const Storage<T>) };
68// Update the state before running the destructor as it may attempt to
69 // access the variable.
70storage.state.set(State::Destroyed);
71unsafe {
72drop_in_place(storage.val.get());
73 }
74 })
75}