1 #![cfg(target_thread_local)]
2 #![unstable(feature = "thread_local_internals", issue = "0")]
4 use cell::{Cell, UnsafeCell};
10 inner: UnsafeCell<Option<T>>,
12 // Metadata to keep track of the state of the destructor. Remember that
13 // these variables are thread-local, not global.
14 dtor_registered: Cell<bool>,
15 dtor_running: Cell<bool>,
18 unsafe impl<T> ::marker::Sync for Key<T> { }
21 pub const fn new() -> Key<T> {
23 inner: UnsafeCell::new(None),
24 dtor_registered: Cell::new(false),
25 dtor_running: Cell::new(false)
29 pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
31 if mem::needs_drop::<T>() && self.dtor_running.get() {
39 unsafe fn register_dtor(&self) {
40 if !mem::needs_drop::<T>() || self.dtor_registered.get() {
44 register_dtor(self as *const _ as *mut u8,
46 self.dtor_registered.set(true);
50 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
51 // The fallback implementation uses a vanilla OS-based TLS key to track
52 // the list of destructors that need to be run for this thread. The key
53 // then has its own destructor which runs all the other destructors.
55 // The destructor for DTORS is a little special in that it has a `while`
56 // loop to continuously drain the list of registered destructors. It
57 // *should* be the case that this loop always terminates because we
58 // provide the guarantee that a TLS key cannot be set after it is
59 // flagged for destruction.
60 use sys_common::thread_local as os;
62 static DTORS: os::StaticKey = os::StaticKey::new(Some(run_dtors));
63 type List = Vec<(*mut u8, unsafe extern fn(*mut u8))>;
64 if DTORS.get().is_null() {
65 let v: Box<List> = box Vec::new();
66 DTORS.set(Box::into_raw(v) as *mut u8);
68 let list: &mut List = &mut *(DTORS.get() as *mut List);
71 unsafe extern fn run_dtors(mut ptr: *mut u8) {
72 while !ptr.is_null() {
73 let list: Box<List> = Box::from_raw(ptr as *mut List);
74 for (ptr, dtor) in list.into_iter() {
78 DTORS.set(ptr::null_mut());
83 pub unsafe extern fn destroy_value<T>(ptr: *mut u8) {
84 let ptr = ptr as *mut Key<T>;
85 // Right before we run the user destructor be sure to flag the
86 // destructor as running for this thread so calls to `get` will return
88 (*ptr).dtor_running.set(true);
90 // The macOS implementation of TLS apparently had an odd aspect to it
91 // where the pointer we have may be overwritten while this destructor
92 // is running. Specifically if a TLS destructor re-accesses TLS it may
93 // trigger a re-initialization of all TLS variables, paving over at
94 // least some destroyed ones with initial values.
96 // This means that if we drop a TLS value in place on macOS that we could
97 // revert the value to its original state halfway through the
98 // destructor, which would be bad!
100 // Hence, we use `ptr::read` on macOS (to move to a "safe" location)
101 // instead of drop_in_place.
102 if cfg!(target_os = "macos") {
103 ptr::read((*ptr).inner.get());
105 ptr::drop_in_place((*ptr).inner.get());
109 pub fn requires_move_before_drop() -> bool {