rust/tests/fail/data_race/fence_after_load.rs

25 lines
916 B
Rust
Raw Normal View History

2022-06-16 12:03:41 -05:00
// We want to control preemption here.
2022-07-08 11:08:32 -05:00
//@compile-flags: -Zmiri-disable-isolation -Zmiri-preemption-rate=0
use std::sync::atomic::{fence, AtomicUsize, Ordering};
2022-06-05 11:14:57 -05:00
use std::sync::Arc;
use std::thread;
use std::time::Duration;
2022-06-05 11:14:57 -05:00
fn main() {
static mut V: u32 = 0;
let a = Arc::new(AtomicUsize::default());
let b = a.clone();
thread::spawn(move || {
unsafe { V = 1 }
b.store(1, Ordering::SeqCst);
});
thread::sleep(Duration::from_millis(100));
fence(Ordering::SeqCst);
// Imagine the other thread's actions happening here.
assert_eq!(a.load(Ordering::Relaxed), 1);
// The fence is useless, since it did not happen-after the `store` in the other thread.
// Hence this is a data race.
// Also see https://github.com/rust-lang/miri/issues/2192.
unsafe { V = 2 } //~ERROR: Data race detected between Write on thread `main` and Write on thread `<unnamed>`
2022-06-05 11:14:57 -05:00
}