]> git.lizzy.rs Git - rust.git/blob - tests/fail/weak_memory/racing_mixed_size_read.rs
Auto merge of #2203 - RalfJung:deprecate, r=oli-obk
[rust.git] / tests / fail / weak_memory / racing_mixed_size_read.rs
1 // ignore-windows: Concurrency on Windows is not supported yet.
2
3 #![feature(core_intrinsics)]
4
5 use std::sync::atomic::AtomicU32;
6 use std::sync::atomic::Ordering::*;
7 use std::thread::spawn;
8
9 fn static_atomic(val: u32) -> &'static AtomicU32 {
10     let ret = Box::leak(Box::new(AtomicU32::new(val)));
11     ret
12 }
13
14 fn split_u32_ptr(dword: *const u32) -> *const [u16; 2] {
15     unsafe { std::mem::transmute::<*const u32, *const [u16; 2]>(dword) }
16 }
17
18 // Racing mixed size reads may cause two loads to read-from
19 // the same store but observe different values, which doesn't make
20 // sense under the formal model so we forbade this.
21 pub fn main() {
22     let x = static_atomic(0);
23
24     let j1 = spawn(move || {
25         x.load(Relaxed);
26     });
27
28     let j2 = spawn(move || {
29         let x_ptr = x as *const AtomicU32 as *const u32;
30         let x_split = split_u32_ptr(x_ptr);
31         unsafe {
32             let hi = &(*x_split)[0] as *const u16;
33             std::intrinsics::atomic_load_relaxed(hi); //~ ERROR: imperfectly overlapping
34         }
35     });
36
37     j1.join().unwrap();
38     j2.join().unwrap();
39 }