| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
64-bit atomic implementation on riscv32. |
| 5 |
|
| 6 |
This architecture provides the following 64-bit atomic instructions: |
| 7 |
|
| 8 |
- amocas.d: CAS (Zacas extension) |
| 9 |
|
| 10 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 11 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 12 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#risc-v |
| 13 |
|
| 14 |
Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use |
| 15 |
this module and use fallback implementation instead. |
| 16 |
|
| 17 |
Refs: |
| 18 |
- RISC-V Instruction Set Manual |
| 19 |
"Zacas" Extension for Atomic Compare-and-Swap (CAS) Instructions |
| 20 |
https://docs.riscv.org/reference/isa/v20260120/unpriv/zacas.html |
| 21 |
- RISC-V Atomics ABI Specification |
| 22 |
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20250812-301374e92976e298e676e7129a6212926b2299ce/riscv-atomic.adoc |
| 23 |
|
| 24 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 25 |
*/ |
| 26 |
|
| 27 |
// TODO: merge duplicated code with atomic128/riscv64.rs |
| 28 |
|
| 29 |
include!("macros.rs"); |
| 30 |
|
| 31 |
#[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))] |
| 32 |
#[path = "../fallback/outline_atomics.rs"] |
| 33 |
mod fallback; |
| 34 |
|
| 35 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 36 |
#[cfg(any(test, not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))))] |
| 37 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 38 |
#[path = "../detect/riscv_linux.rs"] |
| 39 |
mod detect; |
| 40 |
|
| 41 |
#[cfg(not(portable_atomic_no_asm))] |
| 42 |
use core::arch::asm; |
| 43 |
use core::sync::atomic::Ordering; |
| 44 |
|
| 45 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 46 |
use crate::utils::ptr::PtrExt as _; |
| 47 |
use crate::utils::{Pair, U64}; |
| 48 |
|
| 49 |
macro_rules! debug_assert_zacas { |
| 50 |
() => { |
| 51 |
#[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))] |
| 52 |
{ |
| 53 |
debug_assert!(detect::detect().zacas()); |
| 54 |
} |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
// `.option arch, +zacas` directive requires LLVM 20, so we use .4byte directive for old LLVM. |
| 59 |
// Note that `.insn <value>` directive requires LLVM 19. |
| 60 |
// https://github.com/llvm/llvm-project/commit/2a086dce691e3cc34a2fc27f4fb255bb2cbbfac9 |
| 61 |
// https://github.com/riscv-non-isa/riscv-asm-manual/blob/v0.0.1/src/asm-manual.adoc#arch |
| 62 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 63 |
macro_rules! start_zacas { |
| 64 |
() => { |
| 65 |
".option push\n.option arch, +zacas" |
| 66 |
}; |
| 67 |
} |
| 68 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 69 |
macro_rules! end_zacas { |
| 70 |
() => { |
| 71 |
".option pop" |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 76 |
macro_rules! atomic_rmw_amocas_order { |
| 77 |
($op:ident, $order:ident) => { |
| 78 |
atomic_rmw_amocas_order!($op, $order, failure = $order) |
| 79 |
}; |
| 80 |
($op:ident, $order:ident, failure = $failure:ident) => { |
| 81 |
match $order { |
| 82 |
Ordering::Relaxed => $op!("", ""), |
| 83 |
Ordering::Acquire => $op!("", ".aq"), |
| 84 |
Ordering::Release => $op!("", ".rl"), |
| 85 |
Ordering::AcqRel => $op!("", ".aqrl"), |
| 86 |
Ordering::SeqCst if $failure == Ordering::SeqCst => $op!("fence rw,rw", ".aqrl"), |
| 87 |
Ordering::SeqCst => $op!("", ".aqrl"), |
| 88 |
_ => unreachable!(), |
| 89 |
} |
| 90 |
}; |
| 91 |
} |
| 92 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 93 |
macro_rules! atomic_rmw_amocas_order_insn { |
| 94 |
($op:ident, $order:ident) => { |
| 95 |
atomic_rmw_amocas_order_insn!($op, $order, failure = $order) |
| 96 |
}; |
| 97 |
($op:ident, $order:ident, failure = $failure:ident) => { |
| 98 |
match $order { |
| 99 |
Ordering::Relaxed => $op!("", "8"), |
| 100 |
Ordering::Acquire => $op!("", "c"), |
| 101 |
Ordering::Release => $op!("", "a"), |
| 102 |
Ordering::AcqRel => $op!("", "e"), |
| 103 |
Ordering::SeqCst if $failure == Ordering::SeqCst => $op!("fence rw,rw", "e"), |
| 104 |
Ordering::SeqCst => $op!("", "e"), |
| 105 |
_ => unreachable!(), |
| 106 |
} |
| 107 |
}; |
| 108 |
} |
| 109 |
|
| 110 |
// If zacas is available at compile-time, we can always use zacas_fn. |
| 111 |
#[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))] |
| 112 |
use self::atomic_load_zacas as atomic_load; |
| 113 |
// Otherwise, we need to do run-time detection and can use zacas_fn only if zacas is available. |
| 114 |
#[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))] |
| 115 |
#[inline] |
| 116 |
unsafe fn atomic_load(src: *mut u64, order: Ordering) -> u64 { |
| 117 |
fn_alias! { |
| 118 |
// inline(never) is just a hint and also not strictly necessary |
| 119 |
// because we use ifunc helper macro, but used for clarity. |
| 120 |
#[inline(never)] |
| 121 |
unsafe fn(src: *mut u64) -> u64; |
| 122 |
atomic_load_zacas_relaxed = atomic_load_zacas(Ordering::Relaxed); |
| 123 |
atomic_load_zacas_acquire = atomic_load_zacas(Ordering::Acquire); |
| 124 |
atomic_load_zacas_seqcst = atomic_load_zacas(Ordering::SeqCst); |
| 125 |
} |
| 126 |
// SAFETY: the caller must uphold the safety contract. |
| 127 |
// we only calls atomic_load_zacas if zacas is available. |
| 128 |
unsafe { |
| 129 |
match order { |
| 130 |
Ordering::Relaxed => { |
| 131 |
ifunc!(unsafe fn(src: *mut u64) -> u64 { |
| 132 |
if detect::detect().zacas() { |
| 133 |
atomic_load_zacas_relaxed |
| 134 |
} else { |
| 135 |
fallback::atomic_load_non_seqcst |
| 136 |
} |
| 137 |
}) |
| 138 |
} |
| 139 |
Ordering::Acquire => { |
| 140 |
ifunc!(unsafe fn(src: *mut u64) -> u64 { |
| 141 |
if detect::detect().zacas() { |
| 142 |
atomic_load_zacas_acquire |
| 143 |
} else { |
| 144 |
fallback::atomic_load_non_seqcst |
| 145 |
} |
| 146 |
}) |
| 147 |
} |
| 148 |
Ordering::SeqCst => { |
| 149 |
ifunc!(unsafe fn(src: *mut u64) -> u64 { |
| 150 |
if detect::detect().zacas() { |
| 151 |
atomic_load_zacas_seqcst |
| 152 |
} else { |
| 153 |
fallback::atomic_load_seqcst |
| 154 |
} |
| 155 |
}) |
| 156 |
} |
| 157 |
_ => unreachable!(), |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
#[inline] |
| 162 |
unsafe fn atomic_load_zacas(src: *mut u64, order: Ordering) -> u64 { |
| 163 |
debug_assert!(src.addr() % 8 == 0); |
| 164 |
debug_assert_zacas!(); |
| 165 |
let (out_lo, out_hi); |
| 166 |
|
| 167 |
// SAFETY: the caller must uphold the safety contract. |
| 168 |
unsafe { |
| 169 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 170 |
macro_rules! load { |
| 171 |
($fence:tt, $asm_order:tt) => { |
| 172 |
asm!( |
| 173 |
start_zacas!(), |
| 174 |
$fence, // fence |
| 175 |
concat!("amocas.d", $asm_order, " a2, a2, 0({src})"), // atomic { if *dst == a2:a3 { *dst = a2:a3 } else { a2:a3 = *dst } } |
| 176 |
end_zacas!(), |
| 177 |
src = in(reg) ptr_reg!(src), |
| 178 |
inout("a2") 0_u32 => out_lo, |
| 179 |
inout("a3") 0_u32 => out_hi, |
| 180 |
options(nostack, preserves_flags), |
| 181 |
) |
| 182 |
}; |
| 183 |
} |
| 184 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 185 |
atomic_rmw_amocas_order!(load, order); |
| 186 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 187 |
macro_rules! load { |
| 188 |
($fence:tt, $insn_order:tt) => { |
| 189 |
asm!( |
| 190 |
$fence, // fence |
| 191 |
// amocas.d{,.aq,.rl,.aqrl} a2, a2, (a0) // atomic { if *a0 == a2:a3 { *a0 = a2:a3 } else { a2:a3 = *a0 } } |
| 192 |
concat!(".4byte 0x2", $insn_order, "c5362f"), |
| 193 |
in("a0") ptr_reg!(src), |
| 194 |
inout("a2") 0_u32 => out_lo, |
| 195 |
inout("a3") 0_u32 => out_hi, |
| 196 |
options(nostack, preserves_flags), |
| 197 |
) |
| 198 |
}; |
| 199 |
} |
| 200 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 201 |
atomic_rmw_amocas_order_insn!(load, order); |
| 202 |
U64 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
#[inline] |
| 207 |
unsafe fn atomic_store(dst: *mut u64, val: u64, order: Ordering) { |
| 208 |
// SAFETY: the caller must uphold the safety contract. |
| 209 |
unsafe { |
| 210 |
atomic_swap(dst, val, order); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
#[inline] |
| 215 |
unsafe fn atomic_compare_exchange( |
| 216 |
dst: *mut u64, |
| 217 |
old: u64, |
| 218 |
new: u64, |
| 219 |
success: Ordering, |
| 220 |
failure: Ordering, |
| 221 |
) -> Result<u64, u64> { |
| 222 |
#[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))] |
| 223 |
// SAFETY: the caller must uphold the safety contract. |
| 224 |
// cfg guarantees that zacas instructions are available at compile-time. |
| 225 |
let (prev, ok) = unsafe { atomic_compare_exchange_zacas(dst, old, new, success, failure) }; |
| 226 |
#[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))] |
| 227 |
let (prev, ok) = { |
| 228 |
fn_alias! { |
| 229 |
// inline(never) is just a hint and also not strictly necessary |
| 230 |
// because we use ifunc helper macro, but used for clarity. |
| 231 |
#[inline(never)] |
| 232 |
unsafe fn(dst: *mut u64, old: u64, new: u64) -> (u64, bool); |
| 233 |
zacas_relaxed_fn = atomic_compare_exchange_zacas(Ordering::Relaxed, Ordering::Relaxed); |
| 234 |
zacas_acquire_fn = atomic_compare_exchange_zacas(Ordering::Acquire, Ordering::Acquire); |
| 235 |
zacas_release_fn = atomic_compare_exchange_zacas(Ordering::Release, Ordering::Relaxed); |
| 236 |
zacas_acqrel_fn = atomic_compare_exchange_zacas(Ordering::AcqRel, Ordering::Acquire); |
| 237 |
zacas_seqcst_fn = atomic_compare_exchange_zacas(Ordering::SeqCst, Ordering::SeqCst); |
| 238 |
} |
| 239 |
let order = crate::utils::upgrade_success_ordering(success, failure); |
| 240 |
// SAFETY: the caller must uphold the safety contract. |
| 241 |
// we only calls atomic_compare_exchange_zacas if zacas is available. |
| 242 |
unsafe { |
| 243 |
match order { |
| 244 |
Ordering::Relaxed => { |
| 245 |
ifunc!(unsafe fn(dst: *mut u64, old: u64, new: u64) -> (u64, bool) { |
| 246 |
if detect::detect().zacas() { |
| 247 |
zacas_relaxed_fn |
| 248 |
} else { |
| 249 |
fallback::atomic_compare_exchange_non_seqcst |
| 250 |
} |
| 251 |
}) |
| 252 |
} |
| 253 |
Ordering::Acquire => { |
| 254 |
ifunc!(unsafe fn(dst: *mut u64, old: u64, new: u64) -> (u64, bool) { |
| 255 |
if detect::detect().zacas() { |
| 256 |
zacas_acquire_fn |
| 257 |
} else { |
| 258 |
fallback::atomic_compare_exchange_non_seqcst |
| 259 |
} |
| 260 |
}) |
| 261 |
} |
| 262 |
Ordering::Release => { |
| 263 |
ifunc!(unsafe fn(dst: *mut u64, old: u64, new: u64) -> (u64, bool) { |
| 264 |
if detect::detect().zacas() { |
| 265 |
zacas_release_fn |
| 266 |
} else { |
| 267 |
fallback::atomic_compare_exchange_non_seqcst |
| 268 |
} |
| 269 |
}) |
| 270 |
} |
| 271 |
Ordering::AcqRel => { |
| 272 |
ifunc!(unsafe fn(dst: *mut u64, old: u64, new: u64) -> (u64, bool) { |
| 273 |
if detect::detect().zacas() { |
| 274 |
zacas_acqrel_fn |
| 275 |
} else { |
| 276 |
fallback::atomic_compare_exchange_non_seqcst |
| 277 |
} |
| 278 |
}) |
| 279 |
} |
| 280 |
Ordering::SeqCst => { |
| 281 |
ifunc!(unsafe fn(dst: *mut u64, old: u64, new: u64) -> (u64, bool) { |
| 282 |
if detect::detect().zacas() { |
| 283 |
zacas_seqcst_fn |
| 284 |
} else { |
| 285 |
fallback::atomic_compare_exchange_seqcst |
| 286 |
} |
| 287 |
}) |
| 288 |
} |
| 289 |
_ => unreachable!(), |
| 290 |
} |
| 291 |
} |
| 292 |
}; |
| 293 |
if ok { Ok(prev) } else { Err(prev) } |
| 294 |
} |
| 295 |
#[inline] |
| 296 |
unsafe fn atomic_compare_exchange_zacas( |
| 297 |
dst: *mut u64, |
| 298 |
old: u64, |
| 299 |
new: u64, |
| 300 |
success: Ordering, |
| 301 |
failure: Ordering, |
| 302 |
) -> (u64, bool) { |
| 303 |
debug_assert!(dst.addr() % 8 == 0); |
| 304 |
debug_assert_zacas!(); |
| 305 |
let order = crate::utils::upgrade_success_ordering(success, failure); |
| 306 |
let old = U64 { whole: old }; |
| 307 |
let new = U64 { whole: new }; |
| 308 |
let (prev_lo, prev_hi); |
| 309 |
|
| 310 |
// SAFETY: the caller must uphold the safety contract. |
| 311 |
unsafe { |
| 312 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 313 |
macro_rules! cmpxchg { |
| 314 |
($fence:tt, $asm_order:tt) => { |
| 315 |
asm!( |
| 316 |
start_zacas!(), |
| 317 |
$fence, // fence |
| 318 |
concat!("amocas.d", $asm_order, " a4, a2, 0({dst})"), // atomic { if *dst == a4:a5 { *dst = a2:a3 } else { a4:a5 = *dst } } |
| 319 |
end_zacas!(), |
| 320 |
dst = in(reg) ptr_reg!(dst), |
| 321 |
// must be allocated to even/odd register pair |
| 322 |
inout("a4") old.pair.lo => prev_lo, |
| 323 |
inout("a5") old.pair.hi => prev_hi, |
| 324 |
// must be allocated to even/odd register pair |
| 325 |
in("a2") new.pair.lo, |
| 326 |
in("a3") new.pair.hi, |
| 327 |
options(nostack, preserves_flags), |
| 328 |
) |
| 329 |
}; |
| 330 |
} |
| 331 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 332 |
atomic_rmw_amocas_order!(cmpxchg, order, failure = failure); |
| 333 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 334 |
macro_rules! cmpxchg { |
| 335 |
($fence:tt, $insn_order:tt) => { |
| 336 |
asm!( |
| 337 |
$fence, // fence |
| 338 |
// amocas.d{,.aq,.rl,.aqrl} a4, a2, (a0) // atomic { if *a0 == a4:a5 { *a0 = a2:a3 } else { a4:a5 = *a0 } } |
| 339 |
concat!(".4byte 0x2", $insn_order, "c5372f"), |
| 340 |
in("a0") ptr_reg!(dst), |
| 341 |
// must be allocated to even/odd register pair |
| 342 |
inout("a4") old.pair.lo => prev_lo, |
| 343 |
inout("a5") old.pair.hi => prev_hi, |
| 344 |
// must be allocated to even/odd register pair |
| 345 |
in("a2") new.pair.lo, |
| 346 |
in("a3") new.pair.hi, |
| 347 |
options(nostack, preserves_flags), |
| 348 |
) |
| 349 |
}; |
| 350 |
} |
| 351 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 352 |
atomic_rmw_amocas_order_insn!(cmpxchg, order, failure = failure); |
| 353 |
let prev = U64 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole; |
| 354 |
(prev, prev == old.whole) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// amocas is always strong. |
| 359 |
use self::atomic_compare_exchange as atomic_compare_exchange_weak; |
| 360 |
|
| 361 |
// 64-bit atomic load by two 32-bit atomic loads. |
| 362 |
#[inline] |
| 363 |
unsafe fn byte_wise_atomic_load(src: *const u64) -> u64 { |
| 364 |
let (out_lo, out_hi); |
| 365 |
|
| 366 |
// SAFETY: the caller must uphold the safety contract. |
| 367 |
unsafe { |
| 368 |
asm!( |
| 369 |
"lw {out_lo}, ({src})", // atomic { out_lo = *src } |
| 370 |
"lw {out_hi}, 4({src})", // atomic { out_hi = *src.byte_add(4) } |
| 371 |
src = in(reg) ptr_reg!(src), |
| 372 |
out_lo = out(reg) out_lo, |
| 373 |
out_hi = out(reg) out_hi, |
| 374 |
options(pure, nostack, preserves_flags, readonly), |
| 375 |
); |
| 376 |
U64 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
macro_rules! select_atomic_rmw { |
| 381 |
( |
| 382 |
unsafe fn $name:ident(dst: *mut u64 $(, $($arg:tt)*)?) $(-> $ret_ty:ty)? { |
| 383 |
|$zacas_fn_binding:ident| $($zacas_fn_body:tt)* |
| 384 |
} |
| 385 |
zacas = $zacas_fn:ident; |
| 386 |
non_seqcst_fallback = $non_seqcst_fallback_fn:ident; |
| 387 |
seqcst_fallback = $seqcst_fallback_fn:ident; |
| 388 |
) => { |
| 389 |
#[inline] |
| 390 |
unsafe fn $zacas_fn(dst: *mut u64 $(, $($arg)*)?, order: Ordering) $(-> $ret_ty)? { |
| 391 |
// SAFETY: the caller must uphold the safety contract. |
| 392 |
unsafe { |
| 393 |
// This is not single-copy atomic reads, but this is ok because subsequent |
| 394 |
// CAS will check for consistency. |
| 395 |
// |
| 396 |
// Note that the C++20 memory model does not allow mixed-sized atomic access, |
| 397 |
// so we must use inline assembly to implement byte_wise_atomic_load. |
| 398 |
// (i.e., byte-wise atomic based on the standard library's atomic types |
| 399 |
// cannot be used here). |
| 400 |
let mut prev = byte_wise_atomic_load(dst); |
| 401 |
loop { |
| 402 |
let next = { |
| 403 |
let $zacas_fn_binding = prev; |
| 404 |
$($zacas_fn_body)* |
| 405 |
}; |
| 406 |
match atomic_compare_exchange_weak(dst, prev, next, order, Ordering::Relaxed) { |
| 407 |
Ok(x) => return x, |
| 408 |
Err(x) => prev = x, |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
// If zacas is available at compile-time, we can always use zacas_fn. |
| 414 |
#[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))] |
| 415 |
use self::$zacas_fn as $name; |
| 416 |
// Otherwise, we need to do run-time detection and can use zacas_fn only if zacas is available. |
| 417 |
#[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))] |
| 418 |
#[inline] |
| 419 |
unsafe fn $name(dst: *mut u64 $(, $($arg)*)?, order: Ordering) $(-> $ret_ty)? { |
| 420 |
fn_alias! { |
| 421 |
// inline(never) is just a hint and also not strictly necessary |
| 422 |
// because we use ifunc helper macro, but used for clarity. |
| 423 |
#[inline(never)] |
| 424 |
unsafe fn(dst: *mut u64 $(, $($arg)*)?) $(-> $ret_ty)?; |
| 425 |
zacas_relaxed_fn = $zacas_fn(Ordering::Relaxed); |
| 426 |
zacas_acquire_fn = $zacas_fn(Ordering::Acquire); |
| 427 |
zacas_release_fn = $zacas_fn(Ordering::Release); |
| 428 |
zacas_acqrel_fn = $zacas_fn(Ordering::AcqRel); |
| 429 |
zacas_seqcst_fn = $zacas_fn(Ordering::SeqCst); |
| 430 |
} |
| 431 |
// SAFETY: the caller must uphold the safety contract. |
| 432 |
// we only calls zacas_fn if zacas is available. |
| 433 |
unsafe { |
| 434 |
match order { |
| 435 |
Ordering::Relaxed => { |
| 436 |
ifunc!(unsafe fn(dst: *mut u64 $(, $($arg)*)?) $(-> $ret_ty)? { |
| 437 |
if detect::detect().zacas() { |
| 438 |
zacas_relaxed_fn |
| 439 |
} else { |
| 440 |
fallback::$non_seqcst_fallback_fn |
| 441 |
} |
| 442 |
}) |
| 443 |
} |
| 444 |
Ordering::Acquire => { |
| 445 |
ifunc!(unsafe fn(dst: *mut u64 $(, $($arg)*)?) $(-> $ret_ty)? { |
| 446 |
if detect::detect().zacas() { |
| 447 |
zacas_acquire_fn |
| 448 |
} else { |
| 449 |
fallback::$non_seqcst_fallback_fn |
| 450 |
} |
| 451 |
}) |
| 452 |
} |
| 453 |
Ordering::Release => { |
| 454 |
ifunc!(unsafe fn(dst: *mut u64 $(, $($arg)*)?) $(-> $ret_ty)? { |
| 455 |
if detect::detect().zacas() { |
| 456 |
zacas_release_fn |
| 457 |
} else { |
| 458 |
fallback::$non_seqcst_fallback_fn |
| 459 |
} |
| 460 |
}) |
| 461 |
} |
| 462 |
Ordering::AcqRel => { |
| 463 |
ifunc!(unsafe fn(dst: *mut u64 $(, $($arg)*)?) $(-> $ret_ty)? { |
| 464 |
if detect::detect().zacas() { |
| 465 |
zacas_acqrel_fn |
| 466 |
} else { |
| 467 |
fallback::$non_seqcst_fallback_fn |
| 468 |
} |
| 469 |
}) |
| 470 |
} |
| 471 |
Ordering::SeqCst => { |
| 472 |
ifunc!(unsafe fn(dst: *mut u64 $(, $($arg)*)?) $(-> $ret_ty)? { |
| 473 |
if detect::detect().zacas() { |
| 474 |
zacas_seqcst_fn |
| 475 |
} else { |
| 476 |
fallback::$seqcst_fallback_fn |
| 477 |
} |
| 478 |
}) |
| 479 |
} |
| 480 |
_ => unreachable!(), |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
}; |
| 485 |
} |
| 486 |
|
| 487 |
select_atomic_rmw! { |
| 488 |
unsafe fn atomic_swap(dst: *mut u64, val: u64) -> u64 { |
| 489 |
|_x| val |
| 490 |
} |
| 491 |
zacas = atomic_swap_zacas; |
| 492 |
non_seqcst_fallback = atomic_swap_non_seqcst; |
| 493 |
seqcst_fallback = atomic_swap_seqcst; |
| 494 |
} |
| 495 |
select_atomic_rmw! { |
| 496 |
unsafe fn atomic_add(dst: *mut u64, val: u64) -> u64 { |
| 497 |
|x| x.wrapping_add(val) |
| 498 |
} |
| 499 |
zacas = atomic_add_zacas; |
| 500 |
non_seqcst_fallback = atomic_add_non_seqcst; |
| 501 |
seqcst_fallback = atomic_add_seqcst; |
| 502 |
} |
| 503 |
select_atomic_rmw! { |
| 504 |
unsafe fn atomic_sub(dst: *mut u64, val: u64) -> u64 { |
| 505 |
|x| x.wrapping_sub(val) |
| 506 |
} |
| 507 |
zacas = atomic_sub_zacas; |
| 508 |
non_seqcst_fallback = atomic_sub_non_seqcst; |
| 509 |
seqcst_fallback = atomic_sub_seqcst; |
| 510 |
} |
| 511 |
select_atomic_rmw! { |
| 512 |
unsafe fn atomic_and(dst: *mut u64, val: u64) -> u64 { |
| 513 |
|x| x & val |
| 514 |
} |
| 515 |
zacas = atomic_and_zacas; |
| 516 |
non_seqcst_fallback = atomic_and_non_seqcst; |
| 517 |
seqcst_fallback = atomic_and_seqcst; |
| 518 |
} |
| 519 |
select_atomic_rmw! { |
| 520 |
unsafe fn atomic_nand(dst: *mut u64, val: u64) -> u64 { |
| 521 |
|x| !(x & val) |
| 522 |
} |
| 523 |
zacas = atomic_nand_zacas; |
| 524 |
non_seqcst_fallback = atomic_nand_non_seqcst; |
| 525 |
seqcst_fallback = atomic_nand_seqcst; |
| 526 |
} |
| 527 |
select_atomic_rmw! { |
| 528 |
unsafe fn atomic_or(dst: *mut u64, val: u64) -> u64 { |
| 529 |
|x| x | val |
| 530 |
} |
| 531 |
zacas = atomic_or_zacas; |
| 532 |
non_seqcst_fallback = atomic_or_non_seqcst; |
| 533 |
seqcst_fallback = atomic_or_seqcst; |
| 534 |
} |
| 535 |
select_atomic_rmw! { |
| 536 |
unsafe fn atomic_xor(dst: *mut u64, val: u64) -> u64 { |
| 537 |
|x| x ^ val |
| 538 |
} |
| 539 |
zacas = atomic_xor_zacas; |
| 540 |
non_seqcst_fallback = atomic_xor_non_seqcst; |
| 541 |
seqcst_fallback = atomic_xor_seqcst; |
| 542 |
} |
| 543 |
select_atomic_rmw! { |
| 544 |
unsafe fn atomic_max(dst: *mut u64, val: u64) -> u64 { |
| 545 |
|x| { |
| 546 |
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)] |
| 547 |
{ core::cmp::max(x as i64, val as i64) as u64 } |
| 548 |
} |
| 549 |
} |
| 550 |
zacas = atomic_max_zacas; |
| 551 |
non_seqcst_fallback = atomic_max_non_seqcst; |
| 552 |
seqcst_fallback = atomic_max_seqcst; |
| 553 |
} |
| 554 |
select_atomic_rmw! { |
| 555 |
unsafe fn atomic_umax(dst: *mut u64, val: u64) -> u64 { |
| 556 |
|x| core::cmp::max(x, val) |
| 557 |
} |
| 558 |
zacas = atomic_umax_zacas; |
| 559 |
non_seqcst_fallback = atomic_umax_non_seqcst; |
| 560 |
seqcst_fallback = atomic_umax_seqcst; |
| 561 |
} |
| 562 |
select_atomic_rmw! { |
| 563 |
unsafe fn atomic_min(dst: *mut u64, val: u64) -> u64 { |
| 564 |
|x| { |
| 565 |
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)] |
| 566 |
{ core::cmp::min(x as i64, val as i64) as u64 } |
| 567 |
} |
| 568 |
} |
| 569 |
zacas = atomic_min_zacas; |
| 570 |
non_seqcst_fallback = atomic_min_non_seqcst; |
| 571 |
seqcst_fallback = atomic_min_seqcst; |
| 572 |
} |
| 573 |
select_atomic_rmw! { |
| 574 |
unsafe fn atomic_umin(dst: *mut u64, val: u64) -> u64 { |
| 575 |
|x| core::cmp::min(x, val) |
| 576 |
} |
| 577 |
zacas = atomic_umin_zacas; |
| 578 |
non_seqcst_fallback = atomic_umin_non_seqcst; |
| 579 |
seqcst_fallback = atomic_umin_seqcst; |
| 580 |
} |
| 581 |
select_atomic_rmw! { |
| 582 |
unsafe fn atomic_not(dst: *mut u64) -> u64 { |
| 583 |
|x| !x |
| 584 |
} |
| 585 |
zacas = atomic_not_zacas; |
| 586 |
non_seqcst_fallback = atomic_not_non_seqcst; |
| 587 |
seqcst_fallback = atomic_not_seqcst; |
| 588 |
} |
| 589 |
select_atomic_rmw! { |
| 590 |
unsafe fn atomic_neg(dst: *mut u64) -> u64 { |
| 591 |
|x| x.wrapping_neg() |
| 592 |
} |
| 593 |
zacas = atomic_neg_zacas; |
| 594 |
non_seqcst_fallback = atomic_neg_non_seqcst; |
| 595 |
seqcst_fallback = atomic_neg_seqcst; |
| 596 |
} |
| 597 |
|
| 598 |
#[inline] |
| 599 |
fn is_lock_free() -> bool { |
| 600 |
#[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))] |
| 601 |
{ |
| 602 |
// zacas is available at compile-time. |
| 603 |
true |
| 604 |
} |
| 605 |
#[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))] |
| 606 |
{ |
| 607 |
detect::detect().zacas() |
| 608 |
} |
| 609 |
} |
| 610 |
const IS_ALWAYS_LOCK_FREE: bool = |
| 611 |
cfg!(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")); |
| 612 |
|
| 613 |
atomic64!(AtomicI64, i64, atomic_max, atomic_min); |
| 614 |
atomic64!(AtomicU64, u64, atomic_umax, atomic_umin); |
| 615 |
|
| 616 |
#[cfg(test)] |
| 617 |
mod tests { |
| 618 |
use super::*; |
| 619 |
|
| 620 |
test_atomic_int!(i64); |
| 621 |
test_atomic_int!(u64); |
| 622 |
|
| 623 |
// load/store/swap implementation is not affected by signedness, so it is |
| 624 |
// enough to test only unsigned types. |
| 625 |
stress_test!(u64); |
| 626 |
} |