| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
128-bit atomic implementation without inline assembly. |
| 5 |
|
| 6 |
Adapted from https://github.com/rust-lang/rust/blob/1.84.0/library/core/src/sync/atomic.rs. |
| 7 |
|
| 8 |
Note: This module is currently only enabled on Miri and ThreadSanitizer which |
| 9 |
do not support inline assembly. |
| 10 |
|
| 11 |
This uses `core::arch::x86_64::cmpxchg16b` on x86_64 and |
| 12 |
`core::intrinsics::atomic_*` on aarch64, powerpc64, and s390x. |
| 13 |
|
| 14 |
See README.md of this directory for performance comparison with the |
| 15 |
implementation with inline assembly. |
| 16 |
|
| 17 |
Note: |
| 18 |
- This currently needs Rust 1.70 on x86_64, otherwise nightly compilers. |
| 19 |
- On powerpc64, this requires LLVM 15+ and quadword-atomics target feature: |
| 20 |
https://github.com/llvm/llvm-project/commit/549e118e93c666914a1045fde38a2cac33e1e445 |
| 21 |
- On s390x, old LLVM (pre-18) generates libcalls for operations other than load/store/cmpxchg: |
| 22 |
https://github.com/llvm/llvm-project/commit/c568927f3e2e7d9804ea74ecbf11c16c014ddcbc |
| 23 |
- On aarch64 big-endian, LLVM (as of 17) generates broken code. (wrong result in stress test) |
| 24 |
(on cfg(miri)/cfg(sanitize) it may be fine though) |
| 25 |
- On powerpc64, LLVM (as of 17) doesn't support 128-bit atomic min/max: |
| 26 |
https://github.com/llvm/llvm-project/issues/68390 |
| 27 |
- On powerpc64le, LLVM (as of 17) generates broken code. (wrong result from fetch_add) |
| 28 |
- On riscv64, LLVM does not automatically use 128-bit atomic instructions even if zacas feature is |
| 29 |
enabled, because doing it changes the ABI. (If the ability to do that is provided by LLVM in the |
| 30 |
future, it should probably be controlled by another ABI feature similar to forced-atomics.) |
| 31 |
*/ |
| 32 |
|
| 33 |
include!("macros.rs"); |
| 34 |
|
| 35 |
#[allow(dead_code)] // we only use compare_exchange. |
| 36 |
#[cfg(target_arch = "x86_64")] |
| 37 |
#[cfg(not(target_feature = "cmpxchg16b"))] |
| 38 |
#[path = "../fallback/outline_atomics.rs"] |
| 39 |
mod fallback; |
| 40 |
|
| 41 |
#[cfg(target_arch = "x86_64")] |
| 42 |
#[cfg(not(target_feature = "cmpxchg16b"))] |
| 43 |
#[path = "../detect/x86_64.rs"] |
| 44 |
mod detect; |
| 45 |
|
| 46 |
#[cfg(not(target_arch = "x86_64"))] |
| 47 |
use core::intrinsics; |
| 48 |
use core::sync::atomic::Ordering::{self, AcqRel, Acquire, Relaxed, Release, SeqCst}; |
| 49 |
|
| 50 |
#[cfg(target_arch = "x86_64")] |
| 51 |
#[inline] |
| 52 |
fn strongest_failure_ordering(order: Ordering) -> Ordering { |
| 53 |
match order { |
| 54 |
Release | Relaxed => Relaxed, |
| 55 |
SeqCst => SeqCst, |
| 56 |
Acquire | AcqRel => Acquire, |
| 57 |
_ => unreachable!(), |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#[inline] |
| 62 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 63 |
unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 { |
| 64 |
#[cfg(target_arch = "x86_64")] |
| 65 |
// SAFETY: the caller must uphold the safety contract. |
| 66 |
unsafe { |
| 67 |
let fail_order = strongest_failure_ordering(order); |
| 68 |
match atomic_compare_exchange(src, 0, 0, order, fail_order) { |
| 69 |
Ok(v) | Err(v) => v, |
| 70 |
} |
| 71 |
} |
| 72 |
#[cfg(not(target_arch = "x86_64"))] |
| 73 |
// SAFETY: the caller must uphold the safety contract. |
| 74 |
unsafe { |
| 75 |
match order { |
| 76 |
Acquire => intrinsics::atomic_load_acquire(src), |
| 77 |
Relaxed => intrinsics::atomic_load_relaxed(src), |
| 78 |
SeqCst => intrinsics::atomic_load_seqcst(src), |
| 79 |
_ => unreachable!(), |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[inline] |
| 85 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 86 |
unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) { |
| 87 |
#[cfg(target_arch = "x86_64")] |
| 88 |
// SAFETY: the caller must uphold the safety contract. |
| 89 |
unsafe { |
| 90 |
atomic_swap(dst, val, order); |
| 91 |
} |
| 92 |
#[cfg(not(target_arch = "x86_64"))] |
| 93 |
// SAFETY: the caller must uphold the safety contract. |
| 94 |
unsafe { |
| 95 |
match order { |
| 96 |
Release => intrinsics::atomic_store_release(dst, val), |
| 97 |
Relaxed => intrinsics::atomic_store_relaxed(dst, val), |
| 98 |
SeqCst => intrinsics::atomic_store_seqcst(dst, val), |
| 99 |
_ => unreachable!(), |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[inline] |
| 105 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 106 |
unsafe fn atomic_compare_exchange( |
| 107 |
dst: *mut u128, |
| 108 |
old: u128, |
| 109 |
new: u128, |
| 110 |
success: Ordering, |
| 111 |
failure: Ordering, |
| 112 |
) -> Result<u128, u128> { |
| 113 |
#[cfg(target_arch = "x86_64")] |
| 114 |
let (val, ok) = { |
| 115 |
#[target_feature(enable = "cmpxchg16b")] |
| 116 |
#[cfg_attr(target_feature = "cmpxchg16b", inline)] |
| 117 |
#[cfg_attr(not(target_feature = "cmpxchg16b"), inline(never))] |
| 118 |
unsafe fn cmpxchg16b( |
| 119 |
dst: *mut u128, |
| 120 |
old: u128, |
| 121 |
new: u128, |
| 122 |
success: Ordering, |
| 123 |
failure: Ordering, |
| 124 |
) -> (u128, bool) { |
| 125 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 126 |
use crate::utils::ptr::PtrExt as _; |
| 127 |
debug_assert!(dst.addr() % 16 == 0); |
| 128 |
#[cfg(not(target_feature = "cmpxchg16b"))] |
| 129 |
{ |
| 130 |
debug_assert!(detect::detect().cmpxchg16b()); |
| 131 |
} |
| 132 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 133 |
// reads, 16-byte aligned (required by CMPXCHG16B), that there are no |
| 134 |
// concurrent non-atomic operations, and that the CPU supports CMPXCHG16B. |
| 135 |
let prev = unsafe { core::arch::x86_64::cmpxchg16b(dst, old, new, success, failure) }; |
| 136 |
(prev, prev == old) |
| 137 |
} |
| 138 |
#[cfg(target_feature = "cmpxchg16b")] |
| 139 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 140 |
// reads, 16-byte aligned, that there are no concurrent non-atomic operations, |
| 141 |
// and cfg guarantees that CMPXCHG16B is available at compile-time. |
| 142 |
unsafe { |
| 143 |
cmpxchg16b(dst, old, new, success, failure) |
| 144 |
} |
| 145 |
#[cfg(not(target_feature = "cmpxchg16b"))] |
| 146 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 147 |
// reads, 16-byte aligned, and that there are no different kinds of concurrent accesses. |
| 148 |
unsafe { |
| 149 |
ifunc!(unsafe fn( |
| 150 |
dst: *mut u128, old: u128, new: u128, success: Ordering, failure: Ordering |
| 151 |
) -> (u128, bool) { |
| 152 |
if detect::detect().cmpxchg16b() { |
| 153 |
cmpxchg16b |
| 154 |
} else { |
| 155 |
fallback::atomic_compare_exchange |
| 156 |
} |
| 157 |
}) |
| 158 |
} |
| 159 |
}; |
| 160 |
#[cfg(not(target_arch = "x86_64"))] |
| 161 |
// SAFETY: the caller must uphold the safety contract. |
| 162 |
let (val, ok) = unsafe { |
| 163 |
match (success, failure) { |
| 164 |
(Relaxed, Relaxed) => intrinsics::atomic_cxchg_relaxed_relaxed(dst, old, new), |
| 165 |
(Relaxed, Acquire) => intrinsics::atomic_cxchg_relaxed_acquire(dst, old, new), |
| 166 |
(Relaxed, SeqCst) => intrinsics::atomic_cxchg_relaxed_seqcst(dst, old, new), |
| 167 |
(Acquire, Relaxed) => intrinsics::atomic_cxchg_acquire_relaxed(dst, old, new), |
| 168 |
(Acquire, Acquire) => intrinsics::atomic_cxchg_acquire_acquire(dst, old, new), |
| 169 |
(Acquire, SeqCst) => intrinsics::atomic_cxchg_acquire_seqcst(dst, old, new), |
| 170 |
(Release, Relaxed) => intrinsics::atomic_cxchg_release_relaxed(dst, old, new), |
| 171 |
(Release, Acquire) => intrinsics::atomic_cxchg_release_acquire(dst, old, new), |
| 172 |
(Release, SeqCst) => intrinsics::atomic_cxchg_release_seqcst(dst, old, new), |
| 173 |
(AcqRel, Relaxed) => intrinsics::atomic_cxchg_acqrel_relaxed(dst, old, new), |
| 174 |
(AcqRel, Acquire) => intrinsics::atomic_cxchg_acqrel_acquire(dst, old, new), |
| 175 |
(AcqRel, SeqCst) => intrinsics::atomic_cxchg_acqrel_seqcst(dst, old, new), |
| 176 |
(SeqCst, Relaxed) => intrinsics::atomic_cxchg_seqcst_relaxed(dst, old, new), |
| 177 |
(SeqCst, Acquire) => intrinsics::atomic_cxchg_seqcst_acquire(dst, old, new), |
| 178 |
(SeqCst, SeqCst) => intrinsics::atomic_cxchg_seqcst_seqcst(dst, old, new), |
| 179 |
_ => unreachable!(), |
| 180 |
} |
| 181 |
}; |
| 182 |
if ok { Ok(val) } else { Err(val) } |
| 183 |
} |
| 184 |
|
| 185 |
#[cfg(target_arch = "x86_64")] |
| 186 |
use self::atomic_compare_exchange as atomic_compare_exchange_weak; |
| 187 |
#[cfg(not(target_arch = "x86_64"))] |
| 188 |
#[inline] |
| 189 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 190 |
unsafe fn atomic_compare_exchange_weak( |
| 191 |
dst: *mut u128, |
| 192 |
old: u128, |
| 193 |
new: u128, |
| 194 |
success: Ordering, |
| 195 |
failure: Ordering, |
| 196 |
) -> Result<u128, u128> { |
| 197 |
// SAFETY: the caller must uphold the safety contract. |
| 198 |
let (val, ok) = unsafe { |
| 199 |
match (success, failure) { |
| 200 |
(Relaxed, Relaxed) => intrinsics::atomic_cxchgweak_relaxed_relaxed(dst, old, new), |
| 201 |
(Relaxed, Acquire) => intrinsics::atomic_cxchgweak_relaxed_acquire(dst, old, new), |
| 202 |
(Relaxed, SeqCst) => intrinsics::atomic_cxchgweak_relaxed_seqcst(dst, old, new), |
| 203 |
(Acquire, Relaxed) => intrinsics::atomic_cxchgweak_acquire_relaxed(dst, old, new), |
| 204 |
(Acquire, Acquire) => intrinsics::atomic_cxchgweak_acquire_acquire(dst, old, new), |
| 205 |
(Acquire, SeqCst) => intrinsics::atomic_cxchgweak_acquire_seqcst(dst, old, new), |
| 206 |
(Release, Relaxed) => intrinsics::atomic_cxchgweak_release_relaxed(dst, old, new), |
| 207 |
(Release, Acquire) => intrinsics::atomic_cxchgweak_release_acquire(dst, old, new), |
| 208 |
(Release, SeqCst) => intrinsics::atomic_cxchgweak_release_seqcst(dst, old, new), |
| 209 |
(AcqRel, Relaxed) => intrinsics::atomic_cxchgweak_acqrel_relaxed(dst, old, new), |
| 210 |
(AcqRel, Acquire) => intrinsics::atomic_cxchgweak_acqrel_acquire(dst, old, new), |
| 211 |
(AcqRel, SeqCst) => intrinsics::atomic_cxchgweak_acqrel_seqcst(dst, old, new), |
| 212 |
(SeqCst, Relaxed) => intrinsics::atomic_cxchgweak_seqcst_relaxed(dst, old, new), |
| 213 |
(SeqCst, Acquire) => intrinsics::atomic_cxchgweak_seqcst_acquire(dst, old, new), |
| 214 |
(SeqCst, SeqCst) => intrinsics::atomic_cxchgweak_seqcst_seqcst(dst, old, new), |
| 215 |
_ => unreachable!(), |
| 216 |
} |
| 217 |
}; |
| 218 |
if ok { Ok(val) } else { Err(val) } |
| 219 |
} |
| 220 |
|
| 221 |
#[inline(always)] |
| 222 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 223 |
unsafe fn atomic_update<F>(dst: *mut u128, order: Ordering, mut f: F) -> u128 |
| 224 |
where |
| 225 |
F: FnMut(u128) -> u128, |
| 226 |
{ |
| 227 |
// SAFETY: the caller must uphold the safety contract. |
| 228 |
unsafe { |
| 229 |
// This is a private function and all instances of `f` only operate on the value |
| 230 |
// loaded, so there is no need to synchronize the first load/failed CAS. |
| 231 |
let mut prev = atomic_load(dst, Ordering::Relaxed); |
| 232 |
loop { |
| 233 |
let next = f(prev); |
| 234 |
match atomic_compare_exchange_weak(dst, prev, next, order, Ordering::Relaxed) { |
| 235 |
Ok(x) => return x, |
| 236 |
Err(x) => prev = x, |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// On x86_64, we use core::arch::x86_64::cmpxchg16b instead of core::intrinsics. |
| 243 |
// - On s390x, old LLVM (pre-18) generates libcalls for operations other than load/store/cmpxchg (see also module-level comment). |
| 244 |
#[cfg(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18)))] |
| 245 |
atomic_rmw_by_atomic_update!(); |
| 246 |
// On powerpc64, LLVM doesn't support 128-bit atomic min/max (see also module-level comment). |
| 247 |
#[cfg(target_arch = "powerpc64")] |
| 248 |
atomic_rmw_by_atomic_update!(cmp); |
| 249 |
|
| 250 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 251 |
#[inline] |
| 252 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 253 |
unsafe fn atomic_swap(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 254 |
// SAFETY: the caller must uphold the safety contract. |
| 255 |
unsafe { |
| 256 |
match order { |
| 257 |
Acquire => intrinsics::atomic_xchg_acquire(dst, val), |
| 258 |
Release => intrinsics::atomic_xchg_release(dst, val), |
| 259 |
AcqRel => intrinsics::atomic_xchg_acqrel(dst, val), |
| 260 |
Relaxed => intrinsics::atomic_xchg_relaxed(dst, val), |
| 261 |
SeqCst => intrinsics::atomic_xchg_seqcst(dst, val), |
| 262 |
_ => unreachable!(), |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 268 |
#[inline] |
| 269 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 270 |
unsafe fn atomic_add(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 271 |
// SAFETY: the caller must uphold the safety contract. |
| 272 |
unsafe { |
| 273 |
match order { |
| 274 |
Acquire => intrinsics::atomic_xadd_acquire(dst, val), |
| 275 |
Release => intrinsics::atomic_xadd_release(dst, val), |
| 276 |
AcqRel => intrinsics::atomic_xadd_acqrel(dst, val), |
| 277 |
Relaxed => intrinsics::atomic_xadd_relaxed(dst, val), |
| 278 |
SeqCst => intrinsics::atomic_xadd_seqcst(dst, val), |
| 279 |
_ => unreachable!(), |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 285 |
#[inline] |
| 286 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 287 |
unsafe fn atomic_sub(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 288 |
// SAFETY: the caller must uphold the safety contract. |
| 289 |
unsafe { |
| 290 |
match order { |
| 291 |
Acquire => intrinsics::atomic_xsub_acquire(dst, val), |
| 292 |
Release => intrinsics::atomic_xsub_release(dst, val), |
| 293 |
AcqRel => intrinsics::atomic_xsub_acqrel(dst, val), |
| 294 |
Relaxed => intrinsics::atomic_xsub_relaxed(dst, val), |
| 295 |
SeqCst => intrinsics::atomic_xsub_seqcst(dst, val), |
| 296 |
_ => unreachable!(), |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 302 |
#[inline] |
| 303 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 304 |
unsafe fn atomic_and(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 305 |
// SAFETY: the caller must uphold the safety contract. |
| 306 |
unsafe { |
| 307 |
match order { |
| 308 |
Acquire => intrinsics::atomic_and_acquire(dst, val), |
| 309 |
Release => intrinsics::atomic_and_release(dst, val), |
| 310 |
AcqRel => intrinsics::atomic_and_acqrel(dst, val), |
| 311 |
Relaxed => intrinsics::atomic_and_relaxed(dst, val), |
| 312 |
SeqCst => intrinsics::atomic_and_seqcst(dst, val), |
| 313 |
_ => unreachable!(), |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 319 |
#[inline] |
| 320 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 321 |
unsafe fn atomic_nand(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 322 |
// SAFETY: the caller must uphold the safety contract. |
| 323 |
unsafe { |
| 324 |
match order { |
| 325 |
Acquire => intrinsics::atomic_nand_acquire(dst, val), |
| 326 |
Release => intrinsics::atomic_nand_release(dst, val), |
| 327 |
AcqRel => intrinsics::atomic_nand_acqrel(dst, val), |
| 328 |
Relaxed => intrinsics::atomic_nand_relaxed(dst, val), |
| 329 |
SeqCst => intrinsics::atomic_nand_seqcst(dst, val), |
| 330 |
_ => unreachable!(), |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 336 |
#[inline] |
| 337 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 338 |
unsafe fn atomic_or(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 339 |
// SAFETY: the caller must uphold the safety contract. |
| 340 |
unsafe { |
| 341 |
match order { |
| 342 |
Acquire => intrinsics::atomic_or_acquire(dst, val), |
| 343 |
Release => intrinsics::atomic_or_release(dst, val), |
| 344 |
AcqRel => intrinsics::atomic_or_acqrel(dst, val), |
| 345 |
Relaxed => intrinsics::atomic_or_relaxed(dst, val), |
| 346 |
SeqCst => intrinsics::atomic_or_seqcst(dst, val), |
| 347 |
_ => unreachable!(), |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 353 |
#[inline] |
| 354 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 355 |
unsafe fn atomic_xor(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 356 |
// SAFETY: the caller must uphold the safety contract. |
| 357 |
unsafe { |
| 358 |
match order { |
| 359 |
Acquire => intrinsics::atomic_xor_acquire(dst, val), |
| 360 |
Release => intrinsics::atomic_xor_release(dst, val), |
| 361 |
AcqRel => intrinsics::atomic_xor_acqrel(dst, val), |
| 362 |
Relaxed => intrinsics::atomic_xor_relaxed(dst, val), |
| 363 |
SeqCst => intrinsics::atomic_xor_seqcst(dst, val), |
| 364 |
_ => unreachable!(), |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
#[cfg(not(any( |
| 370 |
target_arch = "x86_64", |
| 371 |
target_arch = "powerpc64", |
| 372 |
all(target_arch = "s390x", portable_atomic_pre_llvm_18), |
| 373 |
)))] |
| 374 |
#[inline] |
| 375 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 376 |
unsafe fn atomic_max(dst: *mut u128, val: u128, order: Ordering) -> i128 { |
| 377 |
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)] |
| 378 |
// SAFETY: the caller must uphold the safety contract. |
| 379 |
unsafe { |
| 380 |
match order { |
| 381 |
Acquire => intrinsics::atomic_max_acquire(dst.cast::<i128>(), val as i128), |
| 382 |
Release => intrinsics::atomic_max_release(dst.cast::<i128>(), val as i128), |
| 383 |
AcqRel => intrinsics::atomic_max_acqrel(dst.cast::<i128>(), val as i128), |
| 384 |
Relaxed => intrinsics::atomic_max_relaxed(dst.cast::<i128>(), val as i128), |
| 385 |
SeqCst => intrinsics::atomic_max_seqcst(dst.cast::<i128>(), val as i128), |
| 386 |
_ => unreachable!(), |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
#[cfg(not(any( |
| 392 |
target_arch = "x86_64", |
| 393 |
target_arch = "powerpc64", |
| 394 |
all(target_arch = "s390x", portable_atomic_pre_llvm_18), |
| 395 |
)))] |
| 396 |
#[inline] |
| 397 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 398 |
unsafe fn atomic_min(dst: *mut u128, val: u128, order: Ordering) -> i128 { |
| 399 |
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)] |
| 400 |
// SAFETY: the caller must uphold the safety contract. |
| 401 |
unsafe { |
| 402 |
match order { |
| 403 |
Acquire => intrinsics::atomic_min_acquire(dst.cast::<i128>(), val as i128), |
| 404 |
Release => intrinsics::atomic_min_release(dst.cast::<i128>(), val as i128), |
| 405 |
AcqRel => intrinsics::atomic_min_acqrel(dst.cast::<i128>(), val as i128), |
| 406 |
Relaxed => intrinsics::atomic_min_relaxed(dst.cast::<i128>(), val as i128), |
| 407 |
SeqCst => intrinsics::atomic_min_seqcst(dst.cast::<i128>(), val as i128), |
| 408 |
_ => unreachable!(), |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
#[cfg(not(any( |
| 414 |
target_arch = "x86_64", |
| 415 |
target_arch = "powerpc64", |
| 416 |
all(target_arch = "s390x", portable_atomic_pre_llvm_18), |
| 417 |
)))] |
| 418 |
#[inline] |
| 419 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 420 |
unsafe fn atomic_umax(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 421 |
// SAFETY: the caller must uphold the safety contract. |
| 422 |
unsafe { |
| 423 |
match order { |
| 424 |
Acquire => intrinsics::atomic_umax_acquire(dst, val), |
| 425 |
Release => intrinsics::atomic_umax_release(dst, val), |
| 426 |
AcqRel => intrinsics::atomic_umax_acqrel(dst, val), |
| 427 |
Relaxed => intrinsics::atomic_umax_relaxed(dst, val), |
| 428 |
SeqCst => intrinsics::atomic_umax_seqcst(dst, val), |
| 429 |
_ => unreachable!(), |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
#[cfg(not(any( |
| 435 |
target_arch = "x86_64", |
| 436 |
target_arch = "powerpc64", |
| 437 |
all(target_arch = "s390x", portable_atomic_pre_llvm_18), |
| 438 |
)))] |
| 439 |
#[inline] |
| 440 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 441 |
unsafe fn atomic_umin(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 442 |
// SAFETY: the caller must uphold the safety contract. |
| 443 |
unsafe { |
| 444 |
match order { |
| 445 |
Acquire => intrinsics::atomic_umin_acquire(dst, val), |
| 446 |
Release => intrinsics::atomic_umin_release(dst, val), |
| 447 |
AcqRel => intrinsics::atomic_umin_acqrel(dst, val), |
| 448 |
Relaxed => intrinsics::atomic_umin_relaxed(dst, val), |
| 449 |
SeqCst => intrinsics::atomic_umin_seqcst(dst, val), |
| 450 |
_ => unreachable!(), |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 456 |
#[inline] |
| 457 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 458 |
unsafe fn atomic_not(dst: *mut u128, order: Ordering) -> u128 { |
| 459 |
// SAFETY: the caller must uphold the safety contract. |
| 460 |
unsafe { atomic_xor(dst, !0, order) } |
| 461 |
} |
| 462 |
|
| 463 |
#[cfg(not(any(target_arch = "x86_64", all(target_arch = "s390x", portable_atomic_pre_llvm_18))))] |
| 464 |
#[inline] |
| 465 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 466 |
unsafe fn atomic_neg(dst: *mut u128, order: Ordering) -> u128 { |
| 467 |
// SAFETY: the caller must uphold the safety contract. |
| 468 |
unsafe { atomic_update(dst, order, u128::wrapping_neg) } |
| 469 |
} |
| 470 |
|
| 471 |
#[cfg(not(target_arch = "x86_64"))] |
| 472 |
#[inline] |
| 473 |
const fn is_lock_free() -> bool { |
| 474 |
IS_ALWAYS_LOCK_FREE |
| 475 |
} |
| 476 |
#[cfg(not(target_arch = "x86_64"))] |
| 477 |
const IS_ALWAYS_LOCK_FREE: bool = true; |
| 478 |
|
| 479 |
#[cfg(target_arch = "x86_64")] |
| 480 |
#[inline] |
| 481 |
fn is_lock_free() -> bool { |
| 482 |
#[cfg(target_feature = "cmpxchg16b")] |
| 483 |
{ |
| 484 |
// CMPXCHG16B is available at compile-time. |
| 485 |
true |
| 486 |
} |
| 487 |
#[cfg(not(target_feature = "cmpxchg16b"))] |
| 488 |
{ |
| 489 |
detect::detect().cmpxchg16b() |
| 490 |
} |
| 491 |
} |
| 492 |
#[cfg(target_arch = "x86_64")] |
| 493 |
const IS_ALWAYS_LOCK_FREE: bool = cfg!(target_feature = "cmpxchg16b"); |
| 494 |
|
| 495 |
atomic128!(AtomicI128, i128, atomic_max, atomic_min); |
| 496 |
atomic128!(AtomicU128, u128, atomic_umax, atomic_umin); |
| 497 |
|
| 498 |
#[cfg(test)] |
| 499 |
mod tests { |
| 500 |
use super::*; |
| 501 |
|
| 502 |
test_atomic_int!(i128); |
| 503 |
test_atomic_int!(u128); |
| 504 |
|
| 505 |
// load/store/swap implementation is not affected by signedness, so it is |
| 506 |
// enough to test only unsigned types. |
| 507 |
stress_test!(u128); |
| 508 |
} |