]> git.lizzy.rs Git - rust.git/blob - src/test/ui/threads-sendsync/tls-init-on-init.rs
Merge commit '953f024793dab92745fee9cd2c4dee6a60451771' into clippyup
[rust.git] / src / test / ui / threads-sendsync / tls-init-on-init.rs
1 // run-pass
2 #![allow(stable_features)]
3
4 // ignore-emscripten no threads support
5
6 #![feature(thread_local_try_with)]
7
8 use std::thread;
9 use std::sync::atomic::{AtomicUsize, Ordering};
10
11 struct Foo { cnt: usize }
12
13 thread_local!(static FOO: Foo = Foo::init());
14
15 static CNT: AtomicUsize = AtomicUsize::new(0);
16
17 impl Foo {
18     fn init() -> Foo {
19         let cnt = CNT.fetch_add(1, Ordering::SeqCst);
20         if cnt == 0 {
21             FOO.with(|_| {});
22         }
23         Foo { cnt: cnt }
24     }
25 }
26
27 impl Drop for Foo {
28     fn drop(&mut self) {
29         if self.cnt == 1 {
30             FOO.with(|foo| assert_eq!(foo.cnt, 0));
31         } else {
32             assert_eq!(self.cnt, 0);
33             if FOO.try_with(|_| ()).is_ok() {
34                 panic!("should not be in valid state");
35             }
36         }
37     }
38 }
39
40 fn main() {
41     thread::spawn(|| {
42         FOO.with(|_| {});
43     }).join().unwrap();
44 }