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