• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //@compile-flags: -Zmiri-ignore-leaks
2 
3 // Tests operations not performable through C++'s atomic API
4 // but doable in unsafe Rust which we think *should* be fine.
5 // Nonetheless they may be determined as inconsistent with the
6 // memory model in the future.
7 
8 #![feature(atomic_from_mut)]
9 
10 use std::sync::atomic::AtomicU32;
11 use std::sync::atomic::Ordering::*;
12 use std::thread::spawn;
13 
static_atomic(val: u32) -> &'static AtomicU3214 fn static_atomic(val: u32) -> &'static AtomicU32 {
15     let ret = Box::leak(Box::new(AtomicU32::new(val)));
16     ret
17 }
18 
19 // We allow perfectly overlapping non-atomic and atomic reads to race
racing_mixed_atomicity_read()20 fn racing_mixed_atomicity_read() {
21     let x = static_atomic(0);
22     x.store(42, Relaxed);
23 
24     let j1 = spawn(move || x.load(Relaxed));
25 
26     let j2 = spawn(move || {
27         let x_ptr = x as *const AtomicU32 as *const u32;
28         unsafe { x_ptr.read() }
29     });
30 
31     let r1 = j1.join().unwrap();
32     let r2 = j2.join().unwrap();
33 
34     assert_eq!(r1, 42);
35     assert_eq!(r2, 42);
36 }
37 
main()38 pub fn main() {
39     racing_mixed_atomicity_read();
40 }
41