about summary refs log tree commit diff
path: root/tests/ui/threads-sendsync/tls-dont-move-after-init.rs
blob: 54fcc32e9bd7964f379b9d276dc0eb159b8e4fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//@ run-pass
//@ needs-threads

use std::cell::Cell;
use std::thread;

#[derive(Default)]
struct Foo {
    ptr: Cell<*const Foo>,
}

impl Foo {
    fn touch(&self) {
        if self.ptr.get().is_null() {
            self.ptr.set(self);
        } else {
            assert!(self.ptr.get() == self);
        }
    }
}

impl Drop for Foo {
    fn drop(&mut self) {
        self.touch();
    }
}

thread_local!(static FOO: Foo = Foo::default());

fn main() {
    thread::spawn(|| {
        FOO.with(|foo| foo.touch());
        FOO.with(|foo| foo.touch());
    })
    .join()
    .unwrap();
}