]> git.lizzy.rs Git - rust.git/blob - src/libstd/thread/local.rs
Auto merge of #43178 - zackmdavis:some_suggestion, r=eddyb
[rust.git] / src / libstd / thread / local.rs
1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Thread local storage
12
13 #![unstable(feature = "thread_local_internals", issue = "0")]
14
15 use cell::UnsafeCell;
16 use fmt;
17 use mem;
18
19 /// A thread local storage key which owns its contents.
20 ///
21 /// This key uses the fastest possible implementation available to it for the
22 /// target platform. It is instantiated with the [`thread_local!`] macro and the
23 /// primary method is the [`with`] method.
24 ///
25 /// The [`with`] method yields a reference to the contained value which cannot be
26 /// sent across threads or escape the given closure.
27 ///
28 /// # Initialization and Destruction
29 ///
30 /// Initialization is dynamically performed on the first call to [`with`]
31 /// within a thread, and values that implement [`Drop`] get destructed when a
32 /// thread exits. Some caveats apply, which are explained below.
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// use std::cell::RefCell;
38 /// use std::thread;
39 ///
40 /// thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
41 ///
42 /// FOO.with(|f| {
43 ///     assert_eq!(*f.borrow(), 1);
44 ///     *f.borrow_mut() = 2;
45 /// });
46 ///
47 /// // each thread starts out with the initial value of 1
48 /// thread::spawn(move|| {
49 ///     FOO.with(|f| {
50 ///         assert_eq!(*f.borrow(), 1);
51 ///         *f.borrow_mut() = 3;
52 ///     });
53 /// });
54 ///
55 /// // we retain our original value of 2 despite the child thread
56 /// FOO.with(|f| {
57 ///     assert_eq!(*f.borrow(), 2);
58 /// });
59 /// ```
60 ///
61 /// # Platform-specific behavior
62 ///
63 /// Note that a "best effort" is made to ensure that destructors for types
64 /// stored in thread local storage are run, but not all platforms can guarantee
65 /// that destructors will be run for all types in thread local storage. For
66 /// example, there are a number of known caveats where destructors are not run:
67 ///
68 /// 1. On Unix systems when pthread-based TLS is being used, destructors will
69 ///    not be run for TLS values on the main thread when it exits. Note that the
70 ///    application will exit immediately after the main thread exits as well.
71 /// 2. On all platforms it's possible for TLS to re-initialize other TLS slots
72 ///    during destruction. Some platforms ensure that this cannot happen
73 ///    infinitely by preventing re-initialization of any slot that has been
74 ///    destroyed, but not all platforms have this guard. Those platforms that do
75 ///    not guard typically have a synthetic limit after which point no more
76 ///    destructors are run.
77 /// 3. On macOS, initializing TLS during destruction of other TLS slots can
78 ///    sometimes cancel *all* destructors for the current thread, whether or not
79 ///    the slots have already had their destructors run or not.
80 ///
81 /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
82 /// [`thread_local!`]: ../../std/macro.thread_local.html
83 /// [`Drop`]: ../../std/ops/trait.Drop.html
84 #[stable(feature = "rust1", since = "1.0.0")]
85 pub struct LocalKey<T: 'static> {
86     // This outer `LocalKey<T>` type is what's going to be stored in statics,
87     // but actual data inside will sometimes be tagged with #[thread_local].
88     // It's not valid for a true static to reference a #[thread_local] static,
89     // so we get around that by exposing an accessor through a layer of function
90     // indirection (this thunk).
91     //
92     // Note that the thunk is itself unsafe because the returned lifetime of the
93     // slot where data lives, `'static`, is not actually valid. The lifetime
94     // here is actually `'thread`!
95     //
96     // Although this is an extra layer of indirection, it should in theory be
97     // trivially devirtualizable by LLVM because the value of `inner` never
98     // changes and the constant should be readonly within a crate. This mainly
99     // only runs into problems when TLS statics are exported across crates.
100     inner: fn() -> Option<&'static UnsafeCell<Option<T>>>,
101
102     // initialization routine to invoke to create a value
103     init: fn() -> T,
104 }
105
106 #[stable(feature = "std_debug", since = "1.16.0")]
107 impl<T: 'static> fmt::Debug for LocalKey<T> {
108     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109         f.pad("LocalKey { .. }")
110     }
111 }
112
113 #[cfg(not(stage0))]
114 /// Declare a new thread local storage key of type [`std::thread::LocalKey`].
115 ///
116 /// # Syntax
117 ///
118 /// The macro wraps any number of static declarations and makes them thread local.
119 /// Publicity and attributes for each static are allowed. Example:
120 ///
121 /// ```
122 /// use std::cell::RefCell;
123 /// thread_local! {
124 ///     pub static FOO: RefCell<u32> = RefCell::new(1);
125 ///
126 ///     #[allow(unused)]
127 ///     static BAR: RefCell<f32> = RefCell::new(1.0);
128 /// }
129 /// # fn main() {}
130 /// ```
131 ///
132 /// See [LocalKey documentation][`std::thread::LocalKey`] for more
133 /// information.
134 ///
135 /// [`std::thread::LocalKey`]: ../std/thread/struct.LocalKey.html
136 #[macro_export]
137 #[stable(feature = "rust1", since = "1.0.0")]
138 #[allow_internal_unstable]
139 macro_rules! thread_local {
140     // empty (base case for the recursion)
141     () => {};
142
143     // process multiple declarations
144     ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
145         __thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
146         thread_local!($($rest)*);
147     );
148
149     // handle a single declaration
150     ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
151         __thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
152     );
153 }
154
155 #[cfg(not(stage0))]
156 #[doc(hidden)]
157 #[unstable(feature = "thread_local_internals",
158            reason = "should not be necessary",
159            issue = "0")]
160 #[macro_export]
161 #[allow_internal_unstable]
162 macro_rules! __thread_local_inner {
163     ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
164         $(#[$attr])* $vis static $name: $crate::thread::LocalKey<$t> = {
165             fn __init() -> $t { $init }
166
167             fn __getit() -> $crate::option::Option<
168                 &'static $crate::cell::UnsafeCell<
169                     $crate::option::Option<$t>>>
170             {
171                 #[thread_local]
172                 #[cfg(target_thread_local)]
173                 static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
174                     $crate::thread::__FastLocalKeyInner::new();
175
176                 #[cfg(not(target_thread_local))]
177                 static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
178                     $crate::thread::__OsLocalKeyInner::new();
179
180                 __KEY.get()
181             }
182
183             $crate::thread::LocalKey::new(__getit, __init)
184         };
185     }
186 }
187
188 #[cfg(stage0)]
189 /// Declare a new thread local storage key of type `std::thread::LocalKey`.
190 #[macro_export]
191 #[stable(feature = "rust1", since = "1.0.0")]
192 #[allow_internal_unstable]
193 macro_rules! thread_local {
194     // rule 0: empty (base case for the recursion)
195     () => {};
196
197     // rule 1: process multiple declarations where the first one is private
198     ($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
199         thread_local!($(#[$attr])* static $name: $t = $init); // go to rule 2
200         thread_local!($($rest)*);
201     );
202
203     // rule 2: handle a single private declaration
204     ($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
205         $(#[$attr])* static $name: $crate::thread::LocalKey<$t> =
206             __thread_local_inner!($t, $init);
207     );
208
209     // rule 3: handle multiple declarations where the first one is public
210     ($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
211         thread_local!($(#[$attr])* pub static $name: $t = $init); // go to rule 4
212         thread_local!($($rest)*);
213     );
214
215     // rule 4: handle a single public declaration
216     ($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr) => (
217         $(#[$attr])* pub static $name: $crate::thread::LocalKey<$t> =
218             __thread_local_inner!($t, $init);
219     );
220 }
221
222 #[cfg(stage0)]
223 #[doc(hidden)]
224 #[unstable(feature = "thread_local_internals",
225            reason = "should not be necessary",
226            issue = "0")]
227 #[macro_export]
228 #[allow_internal_unstable]
229 macro_rules! __thread_local_inner {
230     ($t:ty, $init:expr) => {{
231         fn __init() -> $t { $init }
232
233         fn __getit() -> $crate::option::Option<
234             &'static $crate::cell::UnsafeCell<
235                 $crate::option::Option<$t>>>
236         {
237             #[thread_local]
238             #[cfg(target_thread_local)]
239             static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
240                 $crate::thread::__FastLocalKeyInner::new();
241
242             #[cfg(not(target_thread_local))]
243             static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
244                 $crate::thread::__OsLocalKeyInner::new();
245
246             __KEY.get()
247         }
248
249         $crate::thread::LocalKey::new(__getit, __init)
250     }}
251 }
252
253 /// Indicator of the state of a thread local storage key.
254 #[unstable(feature = "thread_local_state",
255            reason = "state querying was recently added",
256            issue = "27716")]
257 #[derive(Debug, Eq, PartialEq, Copy, Clone)]
258 pub enum LocalKeyState {
259     /// All keys are in this state whenever a thread starts. Keys will
260     /// transition to the `Valid` state once the first call to [`with`] happens
261     /// and the initialization expression succeeds.
262     ///
263     /// Keys in the `Uninitialized` state will yield a reference to the closure
264     /// passed to [`with`] so long as the initialization routine does not panic.
265     ///
266     /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
267     Uninitialized,
268
269     /// Once a key has been accessed successfully, it will enter the `Valid`
270     /// state. Keys in the `Valid` state will remain so until the thread exits,
271     /// at which point the destructor will be run and the key will enter the
272     /// `Destroyed` state.
273     ///
274     /// Keys in the `Valid` state will be guaranteed to yield a reference to the
275     /// closure passed to [`with`].
276     ///
277     /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
278     Valid,
279
280     /// When a thread exits, the destructors for keys will be run (if
281     /// necessary). While a destructor is running, and possibly after a
282     /// destructor has run, a key is in the `Destroyed` state.
283     ///
284     /// Keys in the `Destroyed` states will trigger a panic when accessed via
285     /// [`with`].
286     ///
287     /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
288     Destroyed,
289 }
290
291 /// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
292 #[unstable(feature = "thread_local_state",
293            reason = "state querying was recently added",
294            issue = "27716")]
295 pub struct AccessError {
296     _private: (),
297 }
298
299 #[unstable(feature = "thread_local_state",
300            reason = "state querying was recently added",
301            issue = "27716")]
302 impl fmt::Debug for AccessError {
303     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
304         f.debug_struct("AccessError").finish()
305     }
306 }
307
308 #[unstable(feature = "thread_local_state",
309            reason = "state querying was recently added",
310            issue = "27716")]
311 impl fmt::Display for AccessError {
312     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
313         fmt::Display::fmt("already destroyed", f)
314     }
315 }
316
317 impl<T: 'static> LocalKey<T> {
318     #[doc(hidden)]
319     #[unstable(feature = "thread_local_internals",
320                reason = "recently added to create a key",
321                issue = "0")]
322     pub const fn new(inner: fn() -> Option<&'static UnsafeCell<Option<T>>>,
323                      init: fn() -> T) -> LocalKey<T> {
324         LocalKey {
325             inner: inner,
326             init: init,
327         }
328     }
329
330     /// Acquires a reference to the value in this TLS key.
331     ///
332     /// This will lazily initialize the value if this thread has not referenced
333     /// this key yet.
334     ///
335     /// # Panics
336     ///
337     /// This function will `panic!()` if the key currently has its
338     /// destructor running, and it **may** panic if the destructor has
339     /// previously been run for this thread.
340     #[stable(feature = "rust1", since = "1.0.0")]
341     pub fn with<F, R>(&'static self, f: F) -> R
342                       where F: FnOnce(&T) -> R {
343         self.try_with(f).expect("cannot access a TLS value during or \
344                                  after it is destroyed")
345     }
346
347     unsafe fn init(&self, slot: &UnsafeCell<Option<T>>) -> &T {
348         // Execute the initialization up front, *then* move it into our slot,
349         // just in case initialization fails.
350         let value = (self.init)();
351         let ptr = slot.get();
352
353         // note that this can in theory just be `*ptr = Some(value)`, but due to
354         // the compiler will currently codegen that pattern with something like:
355         //
356         //      ptr::drop_in_place(ptr)
357         //      ptr::write(ptr, Some(value))
358         //
359         // Due to this pattern it's possible for the destructor of the value in
360         // `ptr` (e.g. if this is being recursively initialized) to re-access
361         // TLS, in which case there will be a `&` and `&mut` pointer to the same
362         // value (an aliasing violation). To avoid setting the "I'm running a
363         // destructor" flag we just use `mem::replace` which should sequence the
364         // operations a little differently and make this safe to call.
365         mem::replace(&mut *ptr, Some(value));
366
367         (*ptr).as_ref().unwrap()
368     }
369
370     /// Query the current state of this key.
371     ///
372     /// A key is initially in the `Uninitialized` state whenever a thread
373     /// starts. It will remain in this state up until the first call to [`with`]
374     /// within a thread has run the initialization expression successfully.
375     ///
376     /// Once the initialization expression succeeds, the key transitions to the
377     /// `Valid` state which will guarantee that future calls to [`with`] will
378     /// succeed within the thread.
379     ///
380     /// When a thread exits, each key will be destroyed in turn, and as keys are
381     /// destroyed they will enter the `Destroyed` state just before the
382     /// destructor starts to run. Keys may remain in the `Destroyed` state after
383     /// destruction has completed. Keys without destructors (e.g. with types
384     /// that are [`Copy`]), may never enter the `Destroyed` state.
385     ///
386     /// Keys in the `Uninitialized` state can be accessed so long as the
387     /// initialization does not panic. Keys in the `Valid` state are guaranteed
388     /// to be able to be accessed. Keys in the `Destroyed` state will panic on
389     /// any call to [`with`].
390     ///
391     /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
392     /// [`Copy`]: ../../std/marker/trait.Copy.html
393     #[unstable(feature = "thread_local_state",
394                reason = "state querying was recently added",
395                issue = "27716")]
396     pub fn state(&'static self) -> LocalKeyState {
397         unsafe {
398             match (self.inner)() {
399                 Some(cell) => {
400                     match *cell.get() {
401                         Some(..) => LocalKeyState::Valid,
402                         None => LocalKeyState::Uninitialized,
403                     }
404                 }
405                 None => LocalKeyState::Destroyed,
406             }
407         }
408     }
409
410     /// Acquires a reference to the value in this TLS key.
411     ///
412     /// This will lazily initialize the value if this thread has not referenced
413     /// this key yet. If the key has been destroyed (which may happen if this is called
414     /// in a destructor), this function will return a ThreadLocalError.
415     ///
416     /// # Panics
417     ///
418     /// This function will still `panic!()` if the key is uninitialized and the
419     /// key's initializer panics.
420     #[unstable(feature = "thread_local_state",
421                reason = "state querying was recently added",
422                issue = "27716")]
423     pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
424                       where F: FnOnce(&T) -> R {
425         unsafe {
426             let slot = (self.inner)().ok_or(AccessError {
427                 _private: (),
428             })?;
429             Ok(f(match *slot.get() {
430                 Some(ref inner) => inner,
431                 None => self.init(slot),
432             }))
433         }
434     }
435 }
436
437 #[doc(hidden)]
438 #[cfg(target_thread_local)]
439 pub mod fast {
440     use cell::{Cell, UnsafeCell};
441     use fmt;
442     use mem;
443     use ptr;
444     use sys::fast_thread_local::{register_dtor, requires_move_before_drop};
445
446     pub struct Key<T> {
447         inner: UnsafeCell<Option<T>>,
448
449         // Metadata to keep track of the state of the destructor. Remember that
450         // these variables are thread-local, not global.
451         dtor_registered: Cell<bool>,
452         dtor_running: Cell<bool>,
453     }
454
455     impl<T> fmt::Debug for Key<T> {
456         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
457             f.pad("Key { .. }")
458         }
459     }
460
461     unsafe impl<T> ::marker::Sync for Key<T> { }
462
463     impl<T> Key<T> {
464         pub const fn new() -> Key<T> {
465             Key {
466                 inner: UnsafeCell::new(None),
467                 dtor_registered: Cell::new(false),
468                 dtor_running: Cell::new(false)
469             }
470         }
471
472         pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
473             unsafe {
474                 if mem::needs_drop::<T>() && self.dtor_running.get() {
475                     return None
476                 }
477                 self.register_dtor();
478             }
479             Some(&self.inner)
480         }
481
482         unsafe fn register_dtor(&self) {
483             if !mem::needs_drop::<T>() || self.dtor_registered.get() {
484                 return
485             }
486
487             register_dtor(self as *const _ as *mut u8,
488                           destroy_value::<T>);
489             self.dtor_registered.set(true);
490         }
491     }
492
493     unsafe extern fn destroy_value<T>(ptr: *mut u8) {
494         let ptr = ptr as *mut Key<T>;
495         // Right before we run the user destructor be sure to flag the
496         // destructor as running for this thread so calls to `get` will return
497         // `None`.
498         (*ptr).dtor_running.set(true);
499
500         // Some implementations may require us to move the value before we drop
501         // it as it could get re-initialized in-place during destruction.
502         //
503         // Hence, we use `ptr::read` on those platforms (to move to a "safe"
504         // location) instead of drop_in_place.
505         if requires_move_before_drop() {
506             ptr::read((*ptr).inner.get());
507         } else {
508             ptr::drop_in_place((*ptr).inner.get());
509         }
510     }
511 }
512
513 #[doc(hidden)]
514 pub mod os {
515     use cell::{Cell, UnsafeCell};
516     use fmt;
517     use marker;
518     use ptr;
519     use sys_common::thread_local::StaticKey as OsStaticKey;
520
521     pub struct Key<T> {
522         // OS-TLS key that we'll use to key off.
523         os: OsStaticKey,
524         marker: marker::PhantomData<Cell<T>>,
525     }
526
527     impl<T> fmt::Debug for Key<T> {
528         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
529             f.pad("Key { .. }")
530         }
531     }
532
533     unsafe impl<T> ::marker::Sync for Key<T> { }
534
535     struct Value<T: 'static> {
536         key: &'static Key<T>,
537         value: UnsafeCell<Option<T>>,
538     }
539
540     impl<T: 'static> Key<T> {
541         pub const fn new() -> Key<T> {
542             Key {
543                 os: OsStaticKey::new(Some(destroy_value::<T>)),
544                 marker: marker::PhantomData
545             }
546         }
547
548         pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
549             unsafe {
550                 let ptr = self.os.get() as *mut Value<T>;
551                 if !ptr.is_null() {
552                     if ptr as usize == 1 {
553                         return None
554                     }
555                     return Some(&(*ptr).value);
556                 }
557
558                 // If the lookup returned null, we haven't initialized our own
559                 // local copy, so do that now.
560                 let ptr: Box<Value<T>> = box Value {
561                     key: self,
562                     value: UnsafeCell::new(None),
563                 };
564                 let ptr = Box::into_raw(ptr);
565                 self.os.set(ptr as *mut u8);
566                 Some(&(*ptr).value)
567             }
568         }
569     }
570
571     unsafe extern fn destroy_value<T: 'static>(ptr: *mut u8) {
572         // The OS TLS ensures that this key contains a NULL value when this
573         // destructor starts to run. We set it back to a sentinel value of 1 to
574         // ensure that any future calls to `get` for this thread will return
575         // `None`.
576         //
577         // Note that to prevent an infinite loop we reset it back to null right
578         // before we return from the destructor ourselves.
579         let ptr = Box::from_raw(ptr as *mut Value<T>);
580         let key = ptr.key;
581         key.os.set(1 as *mut u8);
582         drop(ptr);
583         key.os.set(ptr::null_mut());
584     }
585 }
586
587 #[cfg(all(test, not(target_os = "emscripten")))]
588 mod tests {
589     use sync::mpsc::{channel, Sender};
590     use cell::{Cell, UnsafeCell};
591     use super::LocalKeyState;
592     use thread;
593
594     struct Foo(Sender<()>);
595
596     impl Drop for Foo {
597         fn drop(&mut self) {
598             let Foo(ref s) = *self;
599             s.send(()).unwrap();
600         }
601     }
602
603     #[test]
604     fn smoke_no_dtor() {
605         thread_local!(static FOO: Cell<i32> = Cell::new(1));
606
607         FOO.with(|f| {
608             assert_eq!(f.get(), 1);
609             f.set(2);
610         });
611         let (tx, rx) = channel();
612         let _t = thread::spawn(move|| {
613             FOO.with(|f| {
614                 assert_eq!(f.get(), 1);
615             });
616             tx.send(()).unwrap();
617         });
618         rx.recv().unwrap();
619
620         FOO.with(|f| {
621             assert_eq!(f.get(), 2);
622         });
623     }
624
625     #[test]
626     fn states() {
627         struct Foo;
628         impl Drop for Foo {
629             fn drop(&mut self) {
630                 assert!(FOO.state() == LocalKeyState::Destroyed);
631             }
632         }
633         fn foo() -> Foo {
634             assert!(FOO.state() == LocalKeyState::Uninitialized);
635             Foo
636         }
637         thread_local!(static FOO: Foo = foo());
638
639         thread::spawn(|| {
640             assert!(FOO.state() == LocalKeyState::Uninitialized);
641             FOO.with(|_| {
642                 assert!(FOO.state() == LocalKeyState::Valid);
643             });
644             assert!(FOO.state() == LocalKeyState::Valid);
645         }).join().ok().unwrap();
646     }
647
648     #[test]
649     fn smoke_dtor() {
650         thread_local!(static FOO: UnsafeCell<Option<Foo>> = UnsafeCell::new(None));
651
652         let (tx, rx) = channel();
653         let _t = thread::spawn(move|| unsafe {
654             let mut tx = Some(tx);
655             FOO.with(|f| {
656                 *f.get() = Some(Foo(tx.take().unwrap()));
657             });
658         });
659         rx.recv().unwrap();
660     }
661
662     #[test]
663     fn circular() {
664         struct S1;
665         struct S2;
666         thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
667         thread_local!(static K2: UnsafeCell<Option<S2>> = UnsafeCell::new(None));
668         static mut HITS: u32 = 0;
669
670         impl Drop for S1 {
671             fn drop(&mut self) {
672                 unsafe {
673                     HITS += 1;
674                     if K2.state() == LocalKeyState::Destroyed {
675                         assert_eq!(HITS, 3);
676                     } else {
677                         if HITS == 1 {
678                             K2.with(|s| *s.get() = Some(S2));
679                         } else {
680                             assert_eq!(HITS, 3);
681                         }
682                     }
683                 }
684             }
685         }
686         impl Drop for S2 {
687             fn drop(&mut self) {
688                 unsafe {
689                     HITS += 1;
690                     assert!(K1.state() != LocalKeyState::Destroyed);
691                     assert_eq!(HITS, 2);
692                     K1.with(|s| *s.get() = Some(S1));
693                 }
694             }
695         }
696
697         thread::spawn(move|| {
698             drop(S1);
699         }).join().ok().unwrap();
700     }
701
702     #[test]
703     fn self_referential() {
704         struct S1;
705         thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
706
707         impl Drop for S1 {
708             fn drop(&mut self) {
709                 assert!(K1.state() == LocalKeyState::Destroyed);
710             }
711         }
712
713         thread::spawn(move|| unsafe {
714             K1.with(|s| *s.get() = Some(S1));
715         }).join().ok().unwrap();
716     }
717
718     // Note that this test will deadlock if TLS destructors aren't run (this
719     // requires the destructor to be run to pass the test). macOS has a known bug
720     // where dtors-in-dtors may cancel other destructors, so we just ignore this
721     // test on macOS.
722     #[test]
723     #[cfg_attr(target_os = "macos", ignore)]
724     fn dtors_in_dtors_in_dtors() {
725         struct S1(Sender<()>);
726         thread_local!(static K1: UnsafeCell<Option<S1>> = UnsafeCell::new(None));
727         thread_local!(static K2: UnsafeCell<Option<Foo>> = UnsafeCell::new(None));
728
729         impl Drop for S1 {
730             fn drop(&mut self) {
731                 let S1(ref tx) = *self;
732                 unsafe {
733                     if K2.state() != LocalKeyState::Destroyed {
734                         K2.with(|s| *s.get() = Some(Foo(tx.clone())));
735                     }
736                 }
737             }
738         }
739
740         let (tx, rx) = channel();
741         let _t = thread::spawn(move|| unsafe {
742             let mut tx = Some(tx);
743             K1.with(|s| *s.get() = Some(S1(tx.take().unwrap())));
744         });
745         rx.recv().unwrap();
746     }
747 }
748
749 #[cfg(test)]
750 mod dynamic_tests {
751     use cell::RefCell;
752     use collections::HashMap;
753
754     #[test]
755     fn smoke() {
756         fn square(i: i32) -> i32 { i * i }
757         thread_local!(static FOO: i32 = square(3));
758
759         FOO.with(|f| {
760             assert_eq!(*f, 9);
761         });
762     }
763
764     #[test]
765     fn hashmap() {
766         fn map() -> RefCell<HashMap<i32, i32>> {
767             let mut m = HashMap::new();
768             m.insert(1, 2);
769             RefCell::new(m)
770         }
771         thread_local!(static FOO: RefCell<HashMap<i32, i32>> = map());
772
773         FOO.with(|map| {
774             assert_eq!(map.borrow()[&1], 2);
775         });
776     }
777
778     #[test]
779     fn refcell_vec() {
780         thread_local!(static FOO: RefCell<Vec<u32>> = RefCell::new(vec![1, 2, 3]));
781
782         FOO.with(|vec| {
783             assert_eq!(vec.borrow().len(), 3);
784             vec.borrow_mut().push(4);
785             assert_eq!(vec.borrow()[3], 4);
786         });
787     }
788 }