rust/tests/fail/data_race/release_seq_race.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2022-07-08 11:08:32 -05:00
//@ignore-windows: Concurrency on Windows is not supported yet.
//@compile-flags: -Zmiri-disable-isolation -Zmiri-disable-weak-memory-emulation -Zmiri-preemption-rate=0
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::{sleep, spawn};
use std::time::Duration;
#[derive(Copy, Clone)]
struct EvilSend<T>(pub T);
unsafe impl<T> Send for EvilSend<T> {}
unsafe impl<T> Sync for EvilSend<T> {}
static SYNC: AtomicUsize = AtomicUsize::new(0);
pub fn main() {
let mut a = 0u32;
let b = &mut a as *mut u32;
let c = EvilSend(b);
// Note: this is scheduler-dependent
// the operations need to occur in
// order, the sleep operations currently
// force the desired ordering:
// 1. store release : 1
// 2. store relaxed : 2
// 3. store relaxed : 3
// 4. load acquire : 3
unsafe {
let j1 = spawn(move || {
*c.0 = 1;
SYNC.store(1, Ordering::Release);
2020-12-09 07:35:42 -06:00
sleep(Duration::from_millis(200));
SYNC.store(3, Ordering::Relaxed);
});
let j2 = spawn(move || {
// Blocks the acquire-release sequence
SYNC.store(2, Ordering::Relaxed);
});
let j3 = spawn(move || {
2020-12-09 07:35:42 -06:00
sleep(Duration::from_millis(500));
if SYNC.load(Ordering::Acquire) == 3 {
*c.0 //~ ERROR: Data race detected between Read on thread `<unnamed>` and Write on thread `<unnamed>`
2020-11-22 11:28:12 -06:00
} else {
0
}
});
j1.join().unwrap();
j2.join().unwrap();
j3.join().unwrap();
}
}