]>
git.proxmox.com Git - rustc.git/blob - library/core/tests/atomic.rs
1 use core
::sync
::atomic
::Ordering
::SeqCst
;
2 use core
::sync
::atomic
::*;
6 let a
= AtomicBool
::new(false);
7 assert_eq
!(a
.compare_exchange(false, true, SeqCst
, SeqCst
), Ok(false));
8 assert_eq
!(a
.compare_exchange(false, true, SeqCst
, SeqCst
), Err(true));
10 a
.store(false, SeqCst
);
11 assert_eq
!(a
.compare_exchange(false, true, SeqCst
, SeqCst
), Ok(false));
16 let a
= AtomicBool
::new(true);
17 assert_eq
!(a
.fetch_and(false, SeqCst
), true);
18 assert_eq
!(a
.load(SeqCst
), false);
23 let a
= AtomicBool
::new(false);
24 assert_eq
!(a
.fetch_nand(false, SeqCst
), false);
25 assert_eq
!(a
.load(SeqCst
), true);
26 assert_eq
!(a
.fetch_nand(false, SeqCst
), true);
27 assert_eq
!(a
.load(SeqCst
), true);
28 assert_eq
!(a
.fetch_nand(true, SeqCst
), true);
29 assert_eq
!(a
.load(SeqCst
), false);
30 assert_eq
!(a
.fetch_nand(true, SeqCst
), false);
31 assert_eq
!(a
.load(SeqCst
), true);
36 let x
= AtomicUsize
::new(0xf731);
37 assert_eq
!(x
.fetch_and(0x137f, SeqCst
), 0xf731);
38 assert_eq
!(x
.load(SeqCst
), 0xf731 & 0x137f);
43 let x
= AtomicUsize
::new(0xf731);
44 assert_eq
!(x
.fetch_nand(0x137f, SeqCst
), 0xf731);
45 assert_eq
!(x
.load(SeqCst
), !(0xf731 & 0x137f));
50 let x
= AtomicUsize
::new(0xf731);
51 assert_eq
!(x
.fetch_or(0x137f, SeqCst
), 0xf731);
52 assert_eq
!(x
.load(SeqCst
), 0xf731 | 0x137f);
57 let x
= AtomicUsize
::new(0xf731);
58 assert_eq
!(x
.fetch_xor(0x137f, SeqCst
), 0xf731);
59 assert_eq
!(x
.load(SeqCst
), 0xf731 ^
0x137f);
63 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
65 let x
= AtomicUsize
::new(0xf731);
66 assert_eq
!(x
.fetch_min(0x137f, SeqCst
), 0xf731);
67 assert_eq
!(x
.load(SeqCst
), 0x137f);
68 assert_eq
!(x
.fetch_min(0xf731, SeqCst
), 0x137f);
69 assert_eq
!(x
.load(SeqCst
), 0x137f);
73 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
75 let x
= AtomicUsize
::new(0x137f);
76 assert_eq
!(x
.fetch_max(0xf731, SeqCst
), 0x137f);
77 assert_eq
!(x
.load(SeqCst
), 0xf731);
78 assert_eq
!(x
.fetch_max(0x137f, SeqCst
), 0xf731);
79 assert_eq
!(x
.load(SeqCst
), 0xf731);
84 let x
= AtomicIsize
::new(0xf731);
85 assert_eq
!(x
.fetch_and(0x137f, SeqCst
), 0xf731);
86 assert_eq
!(x
.load(SeqCst
), 0xf731 & 0x137f);
91 let x
= AtomicIsize
::new(0xf731);
92 assert_eq
!(x
.fetch_nand(0x137f, SeqCst
), 0xf731);
93 assert_eq
!(x
.load(SeqCst
), !(0xf731 & 0x137f));
98 let x
= AtomicIsize
::new(0xf731);
99 assert_eq
!(x
.fetch_or(0x137f, SeqCst
), 0xf731);
100 assert_eq
!(x
.load(SeqCst
), 0xf731 | 0x137f);
105 let x
= AtomicIsize
::new(0xf731);
106 assert_eq
!(x
.fetch_xor(0x137f, SeqCst
), 0xf731);
107 assert_eq
!(x
.load(SeqCst
), 0xf731 ^
0x137f);
111 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
113 let x
= AtomicIsize
::new(0xf731);
114 assert_eq
!(x
.fetch_min(0x137f, SeqCst
), 0xf731);
115 assert_eq
!(x
.load(SeqCst
), 0x137f);
116 assert_eq
!(x
.fetch_min(0xf731, SeqCst
), 0x137f);
117 assert_eq
!(x
.load(SeqCst
), 0x137f);
121 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
123 let x
= AtomicIsize
::new(0x137f);
124 assert_eq
!(x
.fetch_max(0xf731, SeqCst
), 0x137f);
125 assert_eq
!(x
.load(SeqCst
), 0xf731);
126 assert_eq
!(x
.fetch_max(0x137f, SeqCst
), 0xf731);
127 assert_eq
!(x
.load(SeqCst
), 0xf731);
130 static S_FALSE
: AtomicBool
= AtomicBool
::new(false);
131 static S_TRUE
: AtomicBool
= AtomicBool
::new(true);
132 static S_INT
: AtomicIsize
= AtomicIsize
::new(0);
133 static S_UINT
: AtomicUsize
= AtomicUsize
::new(0);
137 // Note that we're not really testing the mutability here but it's important
138 // on Android at the moment (#49775)
139 assert
!(!S_FALSE
.swap(true, SeqCst
));
140 assert
!(S_TRUE
.swap(false, SeqCst
));
141 assert
!(S_INT
.fetch_add(1, SeqCst
) == 0);
142 assert
!(S_UINT
.fetch_add(1, SeqCst
) == 0);
146 fn atomic_access_bool() {
147 static mut ATOMIC
: AtomicBool
= AtomicBool
::new(false);
150 assert_eq
!(*ATOMIC
.get_mut(), false);
151 ATOMIC
.store(true, SeqCst
);
152 assert_eq
!(*ATOMIC
.get_mut(), true);
153 ATOMIC
.fetch_or(false, SeqCst
);
154 assert_eq
!(*ATOMIC
.get_mut(), true);
155 ATOMIC
.fetch_and(false, SeqCst
);
156 assert_eq
!(*ATOMIC
.get_mut(), false);
157 ATOMIC
.fetch_nand(true, SeqCst
);
158 assert_eq
!(*ATOMIC
.get_mut(), true);
159 ATOMIC
.fetch_xor(true, SeqCst
);
160 assert_eq
!(*ATOMIC
.get_mut(), false);
165 fn atomic_alignment() {
166 use std
::mem
::{align_of, size_of}
;
168 #[cfg(target_has_atomic = "8")]
169 assert_eq
!(align_of
::<AtomicBool
>(), size_of
::<AtomicBool
>());
170 #[cfg(target_has_atomic = "ptr")]
171 assert_eq
!(align_of
::<AtomicPtr
<u8>>(), size_of
::<AtomicPtr
<u8>>());
172 #[cfg(target_has_atomic = "8")]
173 assert_eq
!(align_of
::<AtomicU8
>(), size_of
::<AtomicU8
>());
174 #[cfg(target_has_atomic = "8")]
175 assert_eq
!(align_of
::<AtomicI8
>(), size_of
::<AtomicI8
>());
176 #[cfg(target_has_atomic = "16")]
177 assert_eq
!(align_of
::<AtomicU16
>(), size_of
::<AtomicU16
>());
178 #[cfg(target_has_atomic = "16")]
179 assert_eq
!(align_of
::<AtomicI16
>(), size_of
::<AtomicI16
>());
180 #[cfg(target_has_atomic = "32")]
181 assert_eq
!(align_of
::<AtomicU32
>(), size_of
::<AtomicU32
>());
182 #[cfg(target_has_atomic = "32")]
183 assert_eq
!(align_of
::<AtomicI32
>(), size_of
::<AtomicI32
>());
184 #[cfg(target_has_atomic = "64")]
185 assert_eq
!(align_of
::<AtomicU64
>(), size_of
::<AtomicU64
>());
186 #[cfg(target_has_atomic = "64")]
187 assert_eq
!(align_of
::<AtomicI64
>(), size_of
::<AtomicI64
>());
188 #[cfg(target_has_atomic = "128")]
189 assert_eq
!(align_of
::<AtomicU128
>(), size_of
::<AtomicU128
>());
190 #[cfg(target_has_atomic = "128")]
191 assert_eq
!(align_of
::<AtomicI128
>(), size_of
::<AtomicI128
>());
192 #[cfg(target_has_atomic = "ptr")]
193 assert_eq
!(align_of
::<AtomicUsize
>(), size_of
::<AtomicUsize
>());
194 #[cfg(target_has_atomic = "ptr")]
195 assert_eq
!(align_of
::<AtomicIsize
>(), size_of
::<AtomicIsize
>());
199 fn atomic_compare_exchange() {
202 static ATOMIC
: AtomicIsize
= AtomicIsize
::new(0);
204 ATOMIC
.compare_exchange(0, 1, Relaxed
, Relaxed
).ok();
205 ATOMIC
.compare_exchange(0, 1, Acquire
, Relaxed
).ok();
206 ATOMIC
.compare_exchange(0, 1, Release
, Relaxed
).ok();
207 ATOMIC
.compare_exchange(0, 1, AcqRel
, Relaxed
).ok();
208 ATOMIC
.compare_exchange(0, 1, SeqCst
, Relaxed
).ok();
209 ATOMIC
.compare_exchange(0, 1, Acquire
, Acquire
).ok();
210 ATOMIC
.compare_exchange(0, 1, AcqRel
, Acquire
).ok();
211 ATOMIC
.compare_exchange(0, 1, SeqCst
, Acquire
).ok();
212 ATOMIC
.compare_exchange(0, 1, SeqCst
, SeqCst
).ok();
213 ATOMIC
.compare_exchange_weak(0, 1, Relaxed
, Relaxed
).ok();
214 ATOMIC
.compare_exchange_weak(0, 1, Acquire
, Relaxed
).ok();
215 ATOMIC
.compare_exchange_weak(0, 1, Release
, Relaxed
).ok();
216 ATOMIC
.compare_exchange_weak(0, 1, AcqRel
, Relaxed
).ok();
217 ATOMIC
.compare_exchange_weak(0, 1, SeqCst
, Relaxed
).ok();
218 ATOMIC
.compare_exchange_weak(0, 1, Acquire
, Acquire
).ok();
219 ATOMIC
.compare_exchange_weak(0, 1, AcqRel
, Acquire
).ok();
220 ATOMIC
.compare_exchange_weak(0, 1, SeqCst
, Acquire
).ok();
221 ATOMIC
.compare_exchange_weak(0, 1, SeqCst
, SeqCst
).ok();