std/sys/thread_local/native/
eager.rs1use crate::cell::{Cell, UnsafeCell};
2use crate::ptr::{self, drop_in_place};
3use crate::sys::thread_local::{abort_on_dtor_unwind, destructors};
4
5#[derive(Clone, Copy)]
6enum State {
7 Initial,
8 Alive,
9 Destroyed,
10}
11
12#[allow(missing_debug_implementations)]
13pub struct Storage<T> {
14 state: Cell<State>,
15 val: UnsafeCell<T>,
16}
17
18impl<T> Storage<T> {
19 pub const fn new(val: T) -> Storage<T> {
20 Storage { state: Cell::new(State::Initial), val: UnsafeCell::new(val) }
21 }
22
23 #[inline]
32 pub unsafe fn get(&self) -> *const T {
33 match self.state.get() {
34 State::Alive => self.val.get(),
35 State::Destroyed => ptr::null(),
36 State::Initial => unsafe { self.initialize() },
37 }
38 }
39
40 #[cold]
41 unsafe fn initialize(&self) -> *const T {
42 unsafe {
47 destructors::register(ptr::from_ref(self).cast_mut().cast(), destroy::<T>);
48 }
49
50 self.state.set(State::Alive);
51 self.val.get()
52 }
53}
54
55unsafe extern "C" fn destroy<T>(ptr: *mut u8) {
63 abort_on_dtor_unwind(|| {
65 let storage = unsafe { &*(ptr as *const Storage<T>) };
66 storage.state.set(State::Destroyed);
69 unsafe {
70 drop_in_place(storage.val.get());
71 }
72 })
73}