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);
131 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
133 let atom = AtomicPtr::<i64>::new(core::ptr::null_mut());
134 assert_eq!(atom.fetch_ptr_add(1, SeqCst).addr(), 0);
135 assert_eq!(atom.load(SeqCst).addr(), 8);
137 assert_eq!(atom.fetch_byte_add(1, SeqCst).addr(), 8);
138 assert_eq!(atom.load(SeqCst).addr(), 9);
140 assert_eq!(atom.fetch_ptr_sub(1, SeqCst).addr(), 9);
141 assert_eq!(atom.load(SeqCst).addr(), 1);
143 assert_eq!(atom.fetch_byte_sub(1, SeqCst).addr(), 1);
144 assert_eq!(atom.load(SeqCst).addr(), 0);
148 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
151 let n = &num as *const i64 as *mut _;
152 let atom = AtomicPtr::<i64>::new(n);
153 assert_eq!(atom.fetch_ptr_add(1, SeqCst), n);
154 assert_eq!(atom.load(SeqCst), n.wrapping_add(1));
156 assert_eq!(atom.fetch_ptr_sub(1, SeqCst), n.wrapping_add(1));
157 assert_eq!(atom.load(SeqCst), n);
158 let bytes_from_n = |b| n.cast::<u8>().wrapping_add(b).cast::<i64>();
160 assert_eq!(atom.fetch_byte_add(1, SeqCst), n);
161 assert_eq!(atom.load(SeqCst), bytes_from_n(1));
163 assert_eq!(atom.fetch_byte_add(5, SeqCst), bytes_from_n(1));
164 assert_eq!(atom.load(SeqCst), bytes_from_n(6));
166 assert_eq!(atom.fetch_byte_sub(1, SeqCst), bytes_from_n(6));
167 assert_eq!(atom.load(SeqCst), bytes_from_n(5));
169 assert_eq!(atom.fetch_byte_sub(5, SeqCst), bytes_from_n(5));
170 assert_eq!(atom.load(SeqCst), n);
174 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
176 let atom = AtomicPtr::<i64>::new(core::ptr::null_mut());
177 assert_eq!(atom.fetch_or(0b0111, SeqCst).addr(), 0);
178 assert_eq!(atom.load(SeqCst).addr(), 0b0111);
180 assert_eq!(atom.fetch_and(0b1101, SeqCst).addr(), 0b0111);
181 assert_eq!(atom.load(SeqCst).addr(), 0b0101);
183 assert_eq!(atom.fetch_xor(0b1111, SeqCst).addr(), 0b0101);
184 assert_eq!(atom.load(SeqCst).addr(), 0b1010);
188 #[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
189 fn ptr_bitops_tagging() {
193 let tagme = Tagme(1000);
194 let ptr = &tagme as *const Tagme as *mut Tagme;
195 let atom: AtomicPtr<Tagme> = AtomicPtr::new(ptr);
197 const MASK_TAG: usize = 0b1111;
198 const MASK_PTR: usize = !MASK_TAG;
200 assert_eq!(ptr.addr() & MASK_TAG, 0);
202 assert_eq!(atom.fetch_or(0b0111, SeqCst), ptr);
203 assert_eq!(atom.load(SeqCst), ptr.map_addr(|a| a | 0b111));
205 assert_eq!(atom.fetch_and(MASK_PTR | 0b0010, SeqCst), ptr.map_addr(|a| a | 0b111));
206 assert_eq!(atom.load(SeqCst), ptr.map_addr(|a| a | 0b0010));
208 assert_eq!(atom.fetch_xor(0b1011, SeqCst), ptr.map_addr(|a| a | 0b0010));
209 assert_eq!(atom.load(SeqCst), ptr.map_addr(|a| a | 0b1001));
211 assert_eq!(atom.fetch_and(MASK_PTR, SeqCst), ptr.map_addr(|a| a | 0b1001));
212 assert_eq!(atom.load(SeqCst), ptr);
215 static S_FALSE: AtomicBool = AtomicBool::new(false);
216 static S_TRUE: AtomicBool = AtomicBool::new(true);
217 static S_INT: AtomicIsize = AtomicIsize::new(0);
218 static S_UINT: AtomicUsize = AtomicUsize::new(0);
222 // Note that we're not really testing the mutability here but it's important
223 // on Android at the moment (#49775)
224 assert!(!S_FALSE.swap(true, SeqCst));
225 assert!(S_TRUE.swap(false, SeqCst));
226 assert!(S_INT.fetch_add(1, SeqCst) == 0);
227 assert!(S_UINT.fetch_add(1, SeqCst) == 0);
231 fn atomic_access_bool() {
232 static mut ATOMIC: AtomicBool = AtomicBool::new(false);
235 assert_eq!(*ATOMIC.get_mut(), false);
236 ATOMIC.store(true, SeqCst);
237 assert_eq!(*ATOMIC.get_mut(), true);
238 ATOMIC.fetch_or(false, SeqCst);
239 assert_eq!(*ATOMIC.get_mut(), true);
240 ATOMIC.fetch_and(false, SeqCst);
241 assert_eq!(*ATOMIC.get_mut(), false);
242 ATOMIC.fetch_nand(true, SeqCst);
243 assert_eq!(*ATOMIC.get_mut(), true);
244 ATOMIC.fetch_xor(true, SeqCst);
245 assert_eq!(*ATOMIC.get_mut(), false);
250 fn atomic_alignment() {
251 use std::mem::{align_of, size_of};
253 #[cfg(target_has_atomic = "8")]
254 assert_eq!(align_of::<AtomicBool>(), size_of::<AtomicBool>());
255 #[cfg(target_has_atomic = "ptr")]
256 assert_eq!(align_of::<AtomicPtr<u8>>(), size_of::<AtomicPtr<u8>>());
257 #[cfg(target_has_atomic = "8")]
258 assert_eq!(align_of::<AtomicU8>(), size_of::<AtomicU8>());
259 #[cfg(target_has_atomic = "8")]
260 assert_eq!(align_of::<AtomicI8>(), size_of::<AtomicI8>());
261 #[cfg(target_has_atomic = "16")]
262 assert_eq!(align_of::<AtomicU16>(), size_of::<AtomicU16>());
263 #[cfg(target_has_atomic = "16")]
264 assert_eq!(align_of::<AtomicI16>(), size_of::<AtomicI16>());
265 #[cfg(target_has_atomic = "32")]
266 assert_eq!(align_of::<AtomicU32>(), size_of::<AtomicU32>());
267 #[cfg(target_has_atomic = "32")]
268 assert_eq!(align_of::<AtomicI32>(), size_of::<AtomicI32>());
269 #[cfg(target_has_atomic = "64")]
270 assert_eq!(align_of::<AtomicU64>(), size_of::<AtomicU64>());
271 #[cfg(target_has_atomic = "64")]
272 assert_eq!(align_of::<AtomicI64>(), size_of::<AtomicI64>());
273 #[cfg(target_has_atomic = "128")]
274 assert_eq!(align_of::<AtomicU128>(), size_of::<AtomicU128>());
275 #[cfg(target_has_atomic = "128")]
276 assert_eq!(align_of::<AtomicI128>(), size_of::<AtomicI128>());
277 #[cfg(target_has_atomic = "ptr")]
278 assert_eq!(align_of::<AtomicUsize>(), size_of::<AtomicUsize>());
279 #[cfg(target_has_atomic = "ptr")]
280 assert_eq!(align_of::<AtomicIsize>(), size_of::<AtomicIsize>());
284 fn atomic_compare_exchange() {
287 static ATOMIC: AtomicIsize = AtomicIsize::new(0);
289 ATOMIC.compare_exchange(0, 1, Relaxed, Relaxed).ok();
290 ATOMIC.compare_exchange(0, 1, Acquire, Relaxed).ok();
291 ATOMIC.compare_exchange(0, 1, Release, Relaxed).ok();
292 ATOMIC.compare_exchange(0, 1, AcqRel, Relaxed).ok();
293 ATOMIC.compare_exchange(0, 1, SeqCst, Relaxed).ok();
294 ATOMIC.compare_exchange(0, 1, Acquire, Acquire).ok();
295 ATOMIC.compare_exchange(0, 1, AcqRel, Acquire).ok();
296 ATOMIC.compare_exchange(0, 1, SeqCst, Acquire).ok();
297 ATOMIC.compare_exchange(0, 1, SeqCst, SeqCst).ok();
298 ATOMIC.compare_exchange_weak(0, 1, Relaxed, Relaxed).ok();
299 ATOMIC.compare_exchange_weak(0, 1, Acquire, Relaxed).ok();
300 ATOMIC.compare_exchange_weak(0, 1, Release, Relaxed).ok();
301 ATOMIC.compare_exchange_weak(0, 1, AcqRel, Relaxed).ok();
302 ATOMIC.compare_exchange_weak(0, 1, SeqCst, Relaxed).ok();
303 ATOMIC.compare_exchange_weak(0, 1, Acquire, Acquire).ok();
304 ATOMIC.compare_exchange_weak(0, 1, AcqRel, Acquire).ok();
305 ATOMIC.compare_exchange_weak(0, 1, SeqCst, Acquire).ok();
306 ATOMIC.compare_exchange_weak(0, 1, SeqCst, SeqCst).ok();
310 fn atomic_const_from() {
311 const _ATOMIC_U8: AtomicU8 = AtomicU8::from(1);
312 const _ATOMIC_BOOL: AtomicBool = AtomicBool::from(true);
313 const _ATOMIC_PTR: AtomicPtr<u32> = AtomicPtr::from(core::ptr::null_mut());