]> git.lizzy.rs Git - rust.git/blob - tests/ui/intrinsics/intrinsic-atomics.rs
Auto merge of #106975 - tmiasko:basic-blocks-cache, r=cjgillot
[rust.git] / tests / ui / intrinsics / intrinsic-atomics.rs
1 // run-pass
2 #![feature(intrinsics)]
3
4 mod rusti {
5     extern "rust-intrinsic" {
6         pub fn atomic_cxchg_seqcst_seqcst<T>(dst: *mut T, old: T, src: T) -> (T, bool);
7         pub fn atomic_cxchg_acquire_acquire<T>(dst: *mut T, old: T, src: T) -> (T, bool);
8         pub fn atomic_cxchg_release_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
9
10         pub fn atomic_cxchgweak_seqcst_seqcst<T>(dst: *mut T, old: T, src: T) -> (T, bool);
11         pub fn atomic_cxchgweak_acquire_acquire<T>(dst: *mut T, old: T, src: T) -> (T, bool);
12         pub fn atomic_cxchgweak_release_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
13
14         pub fn atomic_load_seqcst<T>(src: *const T) -> T;
15         pub fn atomic_load_acquire<T>(src: *const T) -> T;
16
17         pub fn atomic_store_seqcst<T>(dst: *mut T, val: T);
18         pub fn atomic_store_release<T>(dst: *mut T, val: T);
19
20         pub fn atomic_xchg_seqcst<T>(dst: *mut T, src: T) -> T;
21         pub fn atomic_xchg_acquire<T>(dst: *mut T, src: T) -> T;
22         pub fn atomic_xchg_release<T>(dst: *mut T, src: T) -> T;
23
24         pub fn atomic_xadd_seqcst<T>(dst: *mut T, src: T) -> T;
25         pub fn atomic_xadd_acquire<T>(dst: *mut T, src: T) -> T;
26         pub fn atomic_xadd_release<T>(dst: *mut T, src: T) -> T;
27
28         pub fn atomic_xsub_seqcst<T>(dst: *mut T, src: T) -> T;
29         pub fn atomic_xsub_acquire<T>(dst: *mut T, src: T) -> T;
30         pub fn atomic_xsub_release<T>(dst: *mut T, src: T) -> T;
31     }
32 }
33
34 pub fn main() {
35     unsafe {
36         let mut x: Box<_> = Box::new(1);
37
38         assert_eq!(rusti::atomic_load_seqcst(&*x), 1);
39         *x = 5;
40         assert_eq!(rusti::atomic_load_acquire(&*x), 5);
41
42         rusti::atomic_store_seqcst(&mut *x,3);
43         assert_eq!(*x, 3);
44         rusti::atomic_store_release(&mut *x,1);
45         assert_eq!(*x, 1);
46
47         assert_eq!(rusti::atomic_cxchg_seqcst_seqcst(&mut *x, 1, 2), (1, true));
48         assert_eq!(*x, 2);
49
50         assert_eq!(rusti::atomic_cxchg_acquire_acquire(&mut *x, 1, 3), (2, false));
51         assert_eq!(*x, 2);
52
53         assert_eq!(rusti::atomic_cxchg_release_relaxed(&mut *x, 2, 1), (2, true));
54         assert_eq!(*x, 1);
55
56         assert_eq!(rusti::atomic_xchg_seqcst(&mut *x, 0), 1);
57         assert_eq!(*x, 0);
58
59         assert_eq!(rusti::atomic_xchg_acquire(&mut *x, 1), 0);
60         assert_eq!(*x, 1);
61
62         assert_eq!(rusti::atomic_xchg_release(&mut *x, 0), 1);
63         assert_eq!(*x, 0);
64
65         assert_eq!(rusti::atomic_xadd_seqcst(&mut *x, 1), 0);
66         assert_eq!(rusti::atomic_xadd_acquire(&mut *x, 1), 1);
67         assert_eq!(rusti::atomic_xadd_release(&mut *x, 1), 2);
68         assert_eq!(*x, 3);
69
70         assert_eq!(rusti::atomic_xsub_seqcst(&mut *x, 1), 3);
71         assert_eq!(rusti::atomic_xsub_acquire(&mut *x, 1), 2);
72         assert_eq!(rusti::atomic_xsub_release(&mut *x, 1), 1);
73         assert_eq!(*x, 0);
74
75         loop {
76             let res = rusti::atomic_cxchgweak_seqcst_seqcst(&mut *x, 0, 1);
77             assert_eq!(res.0, 0);
78             if res.1 {
79                 break;
80             }
81         }
82         assert_eq!(*x, 1);
83
84         loop {
85             let res = rusti::atomic_cxchgweak_acquire_acquire(&mut *x, 1, 2);
86             assert_eq!(res.0, 1);
87             if res.1 {
88                 break;
89             }
90         }
91         assert_eq!(*x, 2);
92
93         loop {
94             let res = rusti::atomic_cxchgweak_release_relaxed(&mut *x, 2, 3);
95             assert_eq!(res.0, 2);
96             if res.1 {
97                 break;
98             }
99         }
100         assert_eq!(*x, 3);
101     }
102 }