]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/intrinsic-atomics.rs
auto merge of #11866 : alexcrichton/rust/atomic-u64, r=brson
[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 mod rusti {
12     extern "rust-intrinsic" {
13         pub fn atomic_cxchg<T>(dst: &mut T, old: T, src: T) -> T;
14         pub fn atomic_cxchg_acq<T>(dst: &mut T, old: T, src: T) -> T;
15         pub fn atomic_cxchg_rel<T>(dst: &mut T, old: T, src: T) -> T;
16
17         pub fn atomic_load<T>(src: &T) -> T;
18         pub fn atomic_load_acq<T>(src: &T) -> T;
19
20         pub fn atomic_store<T>(dst: &mut T, val: T);
21         pub fn atomic_store_rel<T>(dst: &mut T, val: T);
22
23         pub fn atomic_xchg<T>(dst: &mut T, src: T) -> T;
24         pub fn atomic_xchg_acq<T>(dst: &mut T, src: T) -> T;
25         pub fn atomic_xchg_rel<T>(dst: &mut T, src: T) -> T;
26
27         pub fn atomic_xadd<T>(dst: &mut T, src: T) -> T;
28         pub fn atomic_xadd_acq<T>(dst: &mut T, src: T) -> T;
29         pub fn atomic_xadd_rel<T>(dst: &mut T, src: T) -> T;
30
31         pub fn atomic_xsub<T>(dst: &mut T, src: T) -> T;
32         pub fn atomic_xsub_acq<T>(dst: &mut T, src: T) -> T;
33         pub fn atomic_xsub_rel<T>(dst: &mut T, src: T) -> T;
34     }
35 }
36
37 pub fn main() {
38     unsafe {
39         let mut x = ~1;
40
41         assert_eq!(rusti::atomic_load(x), 1);
42         *x = 5;
43         assert_eq!(rusti::atomic_load_acq(x), 5);
44
45         rusti::atomic_store(x,3);
46         assert_eq!(*x, 3);
47         rusti::atomic_store_rel(x,1);
48         assert_eq!(*x, 1);
49
50         assert_eq!(rusti::atomic_cxchg(x, 1, 2), 1);
51         assert_eq!(*x, 2);
52
53         assert_eq!(rusti::atomic_cxchg_acq(x, 1, 3), 2);
54         assert_eq!(*x, 2);
55
56         assert_eq!(rusti::atomic_cxchg_rel(x, 2, 1), 2);
57         assert_eq!(*x, 1);
58
59         assert_eq!(rusti::atomic_xchg(x, 0), 1);
60         assert_eq!(*x, 0);
61
62         assert_eq!(rusti::atomic_xchg_acq(x, 1), 0);
63         assert_eq!(*x, 1);
64
65         assert_eq!(rusti::atomic_xchg_rel(x, 0), 1);
66         assert_eq!(*x, 0);
67
68         assert_eq!(rusti::atomic_xadd(x, 1), 0);
69         assert_eq!(rusti::atomic_xadd_acq(x, 1), 1);
70         assert_eq!(rusti::atomic_xadd_rel(x, 1), 2);
71         assert_eq!(*x, 3);
72
73         assert_eq!(rusti::atomic_xsub(x, 1), 3);
74         assert_eq!(rusti::atomic_xsub_acq(x, 1), 2);
75         assert_eq!(rusti::atomic_xsub_rel(x, 1), 1);
76         assert_eq!(*x, 0);
77     }
78 }