1 //! Thread local storage
3 #![unstable(feature = "thread_local_internals", issue = "0")]
5 use crate::error::Error;
8 /// A thread local storage key which owns its contents.
10 /// This key uses the fastest possible implementation available to it for the
11 /// target platform. It is instantiated with the [`thread_local!`] macro and the
12 /// primary method is the [`with`] method.
14 /// The [`with`] method yields a reference to the contained value which cannot be
15 /// sent across threads or escape the given closure.
17 /// # Initialization and Destruction
19 /// Initialization is dynamically performed on the first call to [`with`]
20 /// within a thread, and values that implement [`Drop`] get destructed when a
21 /// thread exits. Some caveats apply, which are explained below.
23 /// A `LocalKey`'s initializer cannot recursively depend on itself, and using
24 /// a `LocalKey` in this way will cause the initializer to infinitely recurse
25 /// on the first call to `with`.
30 /// use std::cell::RefCell;
33 /// thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
36 /// assert_eq!(*f.borrow(), 1);
37 /// *f.borrow_mut() = 2;
40 /// // each thread starts out with the initial value of 1
41 /// let t = thread::spawn(move|| {
43 /// assert_eq!(*f.borrow(), 1);
44 /// *f.borrow_mut() = 3;
48 /// // wait for the thread to complete and bail out on panic
49 /// t.join().unwrap();
51 /// // we retain our original value of 2 despite the child thread
53 /// assert_eq!(*f.borrow(), 2);
57 /// # Platform-specific behavior
59 /// Note that a "best effort" is made to ensure that destructors for types
60 /// stored in thread local storage are run, but not all platforms can guarantee
61 /// that destructors will be run for all types in thread local storage. For
62 /// example, there are a number of known caveats where destructors are not run:
64 /// 1. On Unix systems when pthread-based TLS is being used, destructors will
65 /// not be run for TLS values on the main thread when it exits. Note that the
66 /// application will exit immediately after the main thread exits as well.
67 /// 2. On all platforms it's possible for TLS to re-initialize other TLS slots
68 /// during destruction. Some platforms ensure that this cannot happen
69 /// infinitely by preventing re-initialization of any slot that has been
70 /// destroyed, but not all platforms have this guard. Those platforms that do
71 /// not guard typically have a synthetic limit after which point no more
72 /// destructors are run.
74 /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
75 /// [`thread_local!`]: ../../std/macro.thread_local.html
76 /// [`Drop`]: ../../std/ops/trait.Drop.html
77 #[stable(feature = "rust1", since = "1.0.0")]
78 pub struct LocalKey<T: 'static> {
79 // This outer `LocalKey<T>` type is what's going to be stored in statics,
80 // but actual data inside will sometimes be tagged with #[thread_local].
81 // It's not valid for a true static to reference a #[thread_local] static,
82 // so we get around that by exposing an accessor through a layer of function
83 // indirection (this thunk).
85 // Note that the thunk is itself unsafe because the returned lifetime of the
86 // slot where data lives, `'static`, is not actually valid. The lifetime
87 // here is actually slightly shorter than the currently running thread!
89 // Although this is an extra layer of indirection, it should in theory be
90 // trivially devirtualizable by LLVM because the value of `inner` never
91 // changes and the constant should be readonly within a crate. This mainly
92 // only runs into problems when TLS statics are exported across crates.
93 inner: unsafe fn() -> Option<&'static T>,
96 #[stable(feature = "std_debug", since = "1.16.0")]
97 impl<T: 'static> fmt::Debug for LocalKey<T> {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 f.pad("LocalKey { .. }")
103 /// Declare a new thread local storage key of type [`std::thread::LocalKey`].
107 /// The macro wraps any number of static declarations and makes them thread local.
108 /// Publicity and attributes for each static are allowed. Example:
111 /// use std::cell::RefCell;
113 /// pub static FOO: RefCell<u32> = RefCell::new(1);
116 /// static BAR: RefCell<f32> = RefCell::new(1.0);
121 /// See [LocalKey documentation][`std::thread::LocalKey`] for more
124 /// [`std::thread::LocalKey`]: ../std/thread/struct.LocalKey.html
126 #[stable(feature = "rust1", since = "1.0.0")]
127 #[allow_internal_unstable(thread_local_internals)]
128 macro_rules! thread_local {
129 // empty (base case for the recursion)
132 // process multiple declarations
133 ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
134 $crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
135 $crate::thread_local!($($rest)*);
138 // handle a single declaration
139 ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
140 $crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
145 #[unstable(feature = "thread_local_internals",
146 reason = "should not be necessary",
149 #[allow_internal_unstable(thread_local_internals, cfg_target_thread_local, thread_local)]
150 #[allow_internal_unsafe]
151 macro_rules! __thread_local_inner {
152 (@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
155 fn __init() -> $t { $init }
157 unsafe fn __getit() -> $crate::option::Option<&'static $t> {
158 #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
159 static __KEY: $crate::thread::__StaticLocalKeyInner<$t> =
160 $crate::thread::__StaticLocalKeyInner::new();
165 not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
167 static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
168 $crate::thread::__FastLocalKeyInner::new();
171 not(target_thread_local),
172 not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
174 static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
175 $crate::thread::__OsLocalKeyInner::new();
181 $crate::thread::LocalKey::new(__getit)
185 ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
186 $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
187 $crate::__thread_local_inner!(@key $(#[$attr])* $vis $name, $t, $init);
191 /// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
192 #[stable(feature = "thread_local_try_with", since = "1.26.0")]
193 #[derive(Clone, Copy, Eq, PartialEq)]
194 pub struct AccessError {
198 #[stable(feature = "thread_local_try_with", since = "1.26.0")]
199 impl fmt::Debug for AccessError {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 f.debug_struct("AccessError").finish()
205 #[stable(feature = "thread_local_try_with", since = "1.26.0")]
206 impl fmt::Display for AccessError {
207 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208 fmt::Display::fmt("already destroyed", f)
212 #[stable(feature = "thread_local_try_with", since = "1.26.0")]
213 impl Error for AccessError {}
215 impl<T: 'static> LocalKey<T> {
217 #[unstable(feature = "thread_local_internals",
218 reason = "recently added to create a key",
220 pub const unsafe fn new(inner: unsafe fn() -> Option<&'static T>) -> LocalKey<T> {
226 /// Acquires a reference to the value in this TLS key.
228 /// This will lazily initialize the value if this thread has not referenced
233 /// This function will `panic!()` if the key currently has its
234 /// destructor running, and it **may** panic if the destructor has
235 /// previously been run for this thread.
236 #[stable(feature = "rust1", since = "1.0.0")]
237 pub fn with<F, R>(&'static self, f: F) -> R
238 where F: FnOnce(&T) -> R {
239 self.try_with(f).expect("cannot access a TLS value during or \
240 after it is destroyed")
243 /// Acquires a reference to the value in this TLS key.
245 /// This will lazily initialize the value if this thread has not referenced
246 /// this key yet. If the key has been destroyed (which may happen if this is called
247 /// in a destructor), this function will return an [`AccessError`](struct.AccessError.html).
251 /// This function will still `panic!()` if the key is uninitialized and the
252 /// key's initializer panics.
253 #[stable(feature = "thread_local_try_with", since = "1.26.0")]
254 pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
259 let thread_local = (self.inner)().ok_or(AccessError {
268 use crate::cell::UnsafeCell;
272 pub struct LazyKeyInner<T> {
273 inner: UnsafeCell<Option<T>>,
276 impl<T> LazyKeyInner<T> {
277 pub const fn new() -> LazyKeyInner<T> {
279 inner: UnsafeCell::new(None),
283 pub unsafe fn get(&self) -> Option<&'static T> {
284 (*self.inner.get()).as_ref()
287 pub unsafe fn initialize<F: FnOnce() -> T>(&self, init: F) -> &'static T {
288 // Execute the initialization up front, *then* move it into our slot,
289 // just in case initialization fails.
291 let ptr = self.inner.get();
293 // note that this can in theory just be `*ptr = Some(value)`, but due to
294 // the compiler will currently codegen that pattern with something like:
296 // ptr::drop_in_place(ptr)
297 // ptr::write(ptr, Some(value))
299 // Due to this pattern it's possible for the destructor of the value in
300 // `ptr` (e.g., if this is being recursively initialized) to re-access
301 // TLS, in which case there will be a `&` and `&mut` pointer to the same
302 // value (an aliasing violation). To avoid setting the "I'm running a
303 // destructor" flag we just use `mem::replace` which should sequence the
304 // operations a little differently and make this safe to call.
305 mem::replace(&mut *ptr, Some(value));
307 // After storing `Some` we want to get a reference to the contents of
308 // what we just stored. While we could use `unwrap` here and it should
309 // always work it empirically doesn't seem to always get optimized away,
310 // which means that using something like `try_with` can pull in
311 // panicking code and cause a large size bloat.
314 None => hint::unreachable_unchecked(),
319 pub unsafe fn take(&mut self) -> Option<T> {
320 (*self.inner.get()).take()
325 /// On some platforms like wasm32 there's no threads, so no need to generate
326 /// thread locals and we can instead just use plain statics!
328 #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
330 use super::lazy::LazyKeyInner;
334 inner: LazyKeyInner<T>,
337 unsafe impl<T> Sync for Key<T> { }
339 impl<T> fmt::Debug for Key<T> {
340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
346 pub const fn new() -> Key<T> {
348 inner: LazyKeyInner::new(),
352 pub unsafe fn get(&self, init: fn() -> T) -> Option<&'static T> {
353 let value = match self.inner.get() {
354 Some(ref value) => value,
355 None => self.inner.initialize(init),
363 #[cfg(target_thread_local)]
365 use super::lazy::LazyKeyInner;
366 use crate::cell::Cell;
369 use crate::sys::fast_thread_local::register_dtor;
371 #[derive(Copy, Clone)]
378 // This data structure has been carefully constructed so that the fast path
379 // only contains one branch on x86. That optimization is necessary to avoid
380 // duplicated tls lookups on OSX.
382 // LLVM issue: https://bugs.llvm.org/show_bug.cgi?id=41722
384 // If `LazyKeyInner::get` returns `None`, that indicates either:
385 // * The value has never been initialized
386 // * The value is being recursively initialized
387 // * The value has already been destroyed or is being destroyed
388 // To determine which kind of `None`, check `dtor_state`.
390 // This is very optimizer friendly for the fast path - initialized but
392 inner: LazyKeyInner<T>,
394 // Metadata to keep track of the state of the destructor. Remember that
395 // this variable is thread-local, not global.
396 dtor_state: Cell<DtorState>,
399 impl<T> fmt::Debug for Key<T> {
400 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
406 pub const fn new() -> Key<T> {
408 inner: LazyKeyInner::new(),
409 dtor_state: Cell::new(DtorState::Unregistered),
413 pub unsafe fn get<F: FnOnce() -> T>(&self, init: F) -> Option<&'static T> {
414 match self.inner.get() {
415 Some(val) => Some(val),
416 None => self.try_initialize(init),
420 // `try_initialize` is only called once per fast thread local variable,
421 // except in corner cases where thread_local dtors reference other
422 // thread_local's, or it is being recursively initialized.
424 // Macos: Inlining this function can cause two `tlv_get_addr` calls to
425 // be performed for every call to `Key::get`. The #[cold] hint makes
427 // LLVM issue: https://bugs.llvm.org/show_bug.cgi?id=41722
429 unsafe fn try_initialize<F: FnOnce() -> T>(&self, init: F) -> Option<&'static T> {
430 if !mem::needs_drop::<T>() || self.try_register_dtor() {
431 Some(self.inner.initialize(init))
437 // `try_register_dtor` is only called once per fast thread local
438 // variable, except in corner cases where thread_local dtors reference
439 // other thread_local's, or it is being recursively initialized.
440 unsafe fn try_register_dtor(&self) -> bool {
441 match self.dtor_state.get() {
442 DtorState::Unregistered => {
443 // dtor registration happens before initialization.
444 register_dtor(self as *const _ as *mut u8,
446 self.dtor_state.set(DtorState::Registered);
449 DtorState::Registered => {
450 // recursively initialized
453 DtorState::RunningOrHasRun => {
460 unsafe extern fn destroy_value<T>(ptr: *mut u8) {
461 let ptr = ptr as *mut Key<T>;
463 // Right before we run the user destructor be sure to set the
464 // `Option<T>` to `None`, and `dtor_state` to `RunningOrHasRun`. This
465 // causes future calls to `get` to run `try_initialize_drop` again,
466 // which will now fail, and return `None`.
467 let value = (*ptr).inner.take();
468 (*ptr).dtor_state.set(DtorState::RunningOrHasRun);
475 use super::lazy::LazyKeyInner;
476 use crate::cell::Cell;
480 use crate::sys_common::thread_local::StaticKey as OsStaticKey;
483 // OS-TLS key that we'll use to key off.
485 marker: marker::PhantomData<Cell<T>>,
488 impl<T> fmt::Debug for Key<T> {
489 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
494 unsafe impl<T> Sync for Key<T> { }
496 struct Value<T: 'static> {
497 inner: LazyKeyInner<T>,
498 key: &'static Key<T>,
501 impl<T: 'static> Key<T> {
502 pub const fn new() -> Key<T> {
504 os: OsStaticKey::new(Some(destroy_value::<T>)),
505 marker: marker::PhantomData
509 pub unsafe fn get(&'static self, init: fn() -> T) -> Option<&'static T> {
510 let ptr = self.os.get() as *mut Value<T>;
511 if ptr as usize > 1 {
512 match (*ptr).inner.get() {
513 Some(ref value) => return Some(value),
517 self.try_initialize(init)
520 // `try_initialize` is only called once per os thread local variable,
521 // except in corner cases where thread_local dtors reference other
522 // thread_local's, or it is being recursively initialized.
523 unsafe fn try_initialize(&'static self, init: fn() -> T) -> Option<&'static T> {
524 let ptr = self.os.get() as *mut Value<T>;
525 if ptr as usize == 1 {
526 // destructor is running
530 let ptr = if ptr.is_null() {
531 // If the lookup returned null, we haven't initialized our own
532 // local copy, so do that now.
533 let ptr: Box<Value<T>> = box Value {
534 inner: LazyKeyInner::new(),
537 let ptr = Box::into_raw(ptr);
538 self.os.set(ptr as *mut u8);
541 // recursive initialization
545 Some((*ptr).inner.initialize(init))
549 unsafe extern fn destroy_value<T: 'static>(ptr: *mut u8) {
550 // The OS TLS ensures that this key contains a NULL value when this
551 // destructor starts to run. We set it back to a sentinel value of 1 to
552 // ensure that any future calls to `get` for this thread will return
555 // Note that to prevent an infinite loop we reset it back to null right
556 // before we return from the destructor ourselves.
557 let ptr = Box::from_raw(ptr as *mut Value<T>);
559 key.os.set(1 as *mut u8);
561 key.os.set(ptr::null_mut());
565 #[cfg(all(test, not(target_os = "emscripten")))]
567 use crate::sync::mpsc::{channel, Sender};
568 use crate::cell::{Cell, UnsafeCell};
571 struct Foo(Sender<()>);
575 let Foo(ref s) = *self;
582 thread_local!(static FOO: Cell<i32> = Cell::new(1));
585 assert_eq!(f.get(), 1);
588 let (tx, rx) = channel();
589 let _t = thread::spawn(move|| {
591 assert_eq!(f.get(), 1);
593 tx.send(()).unwrap();
598 assert_eq!(f.get(), 2);
607 assert!(FOO.try_with(|_| ()).is_err());
610 thread_local!(static FOO: Foo = Foo);
613 assert!(FOO.try_with(|_| ()).is_ok());
614 }).join().ok().expect("thread panicked");
619 thread_local!(static FOO: UnsafeCell<Option<Foo>> = UnsafeCell::new(None));
621 let (tx, rx) = channel();
622 let _t = thread::spawn(move|| unsafe {
623 let mut tx = Some(tx);
625 *f.get() = Some(Foo(tx.take().unwrap()));
635 thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
636 thread_local!(static K2: UnsafeCell<Option<S2>> = UnsafeCell::new(None));
637 static mut HITS: u32 = 0;
643 if K2.try_with(|_| ()).is_err() {
647 K2.with(|s| *s.get() = Some(S2));
659 assert!(K1.try_with(|_| ()).is_ok());
661 K1.with(|s| *s.get() = Some(S1));
666 thread::spawn(move|| {
668 }).join().ok().expect("thread panicked");
672 fn self_referential() {
674 thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
678 assert!(K1.try_with(|_| ()).is_err());
682 thread::spawn(move|| unsafe {
683 K1.with(|s| *s.get() = Some(S1));
684 }).join().ok().expect("thread panicked");
687 // Note that this test will deadlock if TLS destructors aren't run (this
688 // requires the destructor to be run to pass the test).
690 fn dtors_in_dtors_in_dtors() {
691 struct S1(Sender<()>);
692 thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
693 thread_local!(static K2: UnsafeCell<Option<Foo>> = UnsafeCell::new(None));
697 let S1(ref tx) = *self;
699 let _ = K2.try_with(|s| *s.get() = Some(Foo(tx.clone())));
704 let (tx, rx) = channel();
705 let _t = thread::spawn(move|| unsafe {
706 let mut tx = Some(tx);
707 K1.with(|s| *s.get() = Some(S1(tx.take().unwrap())));
715 use crate::cell::RefCell;
716 use crate::collections::HashMap;
720 fn square(i: i32) -> i32 { i * i }
721 thread_local!(static FOO: i32 = square(3));
730 fn map() -> RefCell<HashMap<i32, i32>> {
731 let mut m = HashMap::new();
735 thread_local!(static FOO: RefCell<HashMap<i32, i32>> = map());
738 assert_eq!(map.borrow()[&1], 2);
744 thread_local!(static FOO: RefCell<Vec<u32>> = RefCell::new(vec![1, 2, 3]));
747 assert_eq!(vec.borrow().len(), 3);
748 vec.borrow_mut().push(4);
749 assert_eq!(vec.borrow()[3], 4);