]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/intrinsic-atomics.rs
Convert unknown_features lint into an error
[rust.git] / src / test / run-pass / 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 #![feature(box_syntax)]
12 #![feature(intrinsics)]
13
14 mod rusti {
15     extern "rust-intrinsic" {
16         pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
17         pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
18         pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
19
20         pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
21         pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
22         pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
23
24         pub fn atomic_load<T>(src: *const T) -> T;
25         pub fn atomic_load_acq<T>(src: *const T) -> T;
26
27         pub fn atomic_store<T>(dst: *mut T, val: T);
28         pub fn atomic_store_rel<T>(dst: *mut T, val: T);
29
30         pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
31         pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
32         pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
33
34         pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
35         pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
36         pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
37
38         pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
39         pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
40         pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
41     }
42 }
43
44 pub fn main() {
45     unsafe {
46         let mut x: Box<_> = box 1;
47
48         assert_eq!(rusti::atomic_load(&*x), 1);
49         *x = 5;
50         assert_eq!(rusti::atomic_load_acq(&*x), 5);
51
52         rusti::atomic_store(&mut *x,3);
53         assert_eq!(*x, 3);
54         rusti::atomic_store_rel(&mut *x,1);
55         assert_eq!(*x, 1);
56
57         assert_eq!(rusti::atomic_cxchg(&mut *x, 1, 2), (1, true));
58         assert_eq!(*x, 2);
59
60         assert_eq!(rusti::atomic_cxchg_acq(&mut *x, 1, 3), (2, false));
61         assert_eq!(*x, 2);
62
63         assert_eq!(rusti::atomic_cxchg_rel(&mut *x, 2, 1), (2, true));
64         assert_eq!(*x, 1);
65
66         assert_eq!(rusti::atomic_xchg(&mut *x, 0), 1);
67         assert_eq!(*x, 0);
68
69         assert_eq!(rusti::atomic_xchg_acq(&mut *x, 1), 0);
70         assert_eq!(*x, 1);
71
72         assert_eq!(rusti::atomic_xchg_rel(&mut *x, 0), 1);
73         assert_eq!(*x, 0);
74
75         assert_eq!(rusti::atomic_xadd(&mut *x, 1), 0);
76         assert_eq!(rusti::atomic_xadd_acq(&mut *x, 1), 1);
77         assert_eq!(rusti::atomic_xadd_rel(&mut *x, 1), 2);
78         assert_eq!(*x, 3);
79
80         assert_eq!(rusti::atomic_xsub(&mut *x, 1), 3);
81         assert_eq!(rusti::atomic_xsub_acq(&mut *x, 1), 2);
82         assert_eq!(rusti::atomic_xsub_rel(&mut *x, 1), 1);
83         assert_eq!(*x, 0);
84
85         loop {
86             let res = rusti::atomic_cxchgweak(&mut *x, 0, 1);
87             assert_eq!(res.0, 0);
88             if res.1 {
89                 break;
90             }
91         }
92         assert_eq!(*x, 1);
93
94         loop {
95             let res = rusti::atomic_cxchgweak_acq(&mut *x, 1, 2);
96             assert_eq!(res.0, 1);
97             if res.1 {
98                 break;
99             }
100         }
101         assert_eq!(*x, 2);
102
103         loop {
104             let res = rusti::atomic_cxchgweak_rel(&mut *x, 2, 3);
105             assert_eq!(res.0, 2);
106             if res.1 {
107                 break;
108             }
109         }
110         assert_eq!(*x, 3);
111     }
112 }