]> git.proxmox.com Git - rustc.git/blob - src/test/ui/run-pass/intrinsics/intrinsic-atomics.rs
New upstream version 1.30.0+dfsg1
[rustc.git] / src / test / ui / run-pass / intrinsics / intrinsic-atomics.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // run-pass
12 #![feature(box_syntax)]
13 #![feature(intrinsics)]
14
15 mod rusti {
16 extern "rust-intrinsic" {
17 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
18 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
19 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
20
21 pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
22 pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
23 pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
24
25 pub fn atomic_load<T>(src: *const T) -> T;
26 pub fn atomic_load_acq<T>(src: *const T) -> T;
27
28 pub fn atomic_store<T>(dst: *mut T, val: T);
29 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
30
31 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
32 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
33 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
34
35 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
36 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
37 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
38
39 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
40 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
41 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
42 }
43 }
44
45 pub fn main() {
46 unsafe {
47 let mut x: Box<_> = box 1;
48
49 assert_eq!(rusti::atomic_load(&*x), 1);
50 *x = 5;
51 assert_eq!(rusti::atomic_load_acq(&*x), 5);
52
53 rusti::atomic_store(&mut *x,3);
54 assert_eq!(*x, 3);
55 rusti::atomic_store_rel(&mut *x,1);
56 assert_eq!(*x, 1);
57
58 assert_eq!(rusti::atomic_cxchg(&mut *x, 1, 2), (1, true));
59 assert_eq!(*x, 2);
60
61 assert_eq!(rusti::atomic_cxchg_acq(&mut *x, 1, 3), (2, false));
62 assert_eq!(*x, 2);
63
64 assert_eq!(rusti::atomic_cxchg_rel(&mut *x, 2, 1), (2, true));
65 assert_eq!(*x, 1);
66
67 assert_eq!(rusti::atomic_xchg(&mut *x, 0), 1);
68 assert_eq!(*x, 0);
69
70 assert_eq!(rusti::atomic_xchg_acq(&mut *x, 1), 0);
71 assert_eq!(*x, 1);
72
73 assert_eq!(rusti::atomic_xchg_rel(&mut *x, 0), 1);
74 assert_eq!(*x, 0);
75
76 assert_eq!(rusti::atomic_xadd(&mut *x, 1), 0);
77 assert_eq!(rusti::atomic_xadd_acq(&mut *x, 1), 1);
78 assert_eq!(rusti::atomic_xadd_rel(&mut *x, 1), 2);
79 assert_eq!(*x, 3);
80
81 assert_eq!(rusti::atomic_xsub(&mut *x, 1), 3);
82 assert_eq!(rusti::atomic_xsub_acq(&mut *x, 1), 2);
83 assert_eq!(rusti::atomic_xsub_rel(&mut *x, 1), 1);
84 assert_eq!(*x, 0);
85
86 loop {
87 let res = rusti::atomic_cxchgweak(&mut *x, 0, 1);
88 assert_eq!(res.0, 0);
89 if res.1 {
90 break;
91 }
92 }
93 assert_eq!(*x, 1);
94
95 loop {
96 let res = rusti::atomic_cxchgweak_acq(&mut *x, 1, 2);
97 assert_eq!(res.0, 1);
98 if res.1 {
99 break;
100 }
101 }
102 assert_eq!(*x, 2);
103
104 loop {
105 let res = rusti::atomic_cxchgweak_rel(&mut *x, 2, 3);
106 assert_eq!(res.0, 2);
107 if res.1 {
108 break;
109 }
110 }
111 assert_eq!(*x, 3);
112 }
113 }