]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/data_race/fence_after_load.rs
Rollup merge of #101717 - Pointerbender:unsafecell-memory-layout, r=Amanieu
[rust.git] / src / tools / miri / tests / fail / data_race / fence_after_load.rs
1 // We want to control preemption here.
2 //@compile-flags: -Zmiri-disable-isolation -Zmiri-preemption-rate=0
3 use std::sync::atomic::{fence, AtomicUsize, Ordering};
4 use std::sync::Arc;
5 use std::thread;
6 use std::time::Duration;
7
8 fn main() {
9     static mut V: u32 = 0;
10     let a = Arc::new(AtomicUsize::default());
11     let b = a.clone();
12     thread::spawn(move || {
13         unsafe { V = 1 }
14         b.store(1, Ordering::SeqCst);
15     });
16     thread::sleep(Duration::from_millis(100));
17     fence(Ordering::SeqCst);
18     // Imagine the other thread's actions happening here.
19     assert_eq!(a.load(Ordering::Relaxed), 1);
20     // The fence is useless, since it did not happen-after the `store` in the other thread.
21     // Hence this is a data race.
22     // Also see https://github.com/rust-lang/miri/issues/2192.
23     unsafe { V = 2 } //~ERROR: Data race detected between Write on thread `main` and Write on thread `<unnamed>`
24 }