| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Fallback implementation based on disabling interrupts or critical-section |
| 5 |
|
| 6 |
- mod.rs contains critical section based fallback implementations. |
| 7 |
- Each architecture modules contain implementations of disabling interrupts. |
| 8 |
|
| 9 |
See README.md of this directory for details. |
| 10 |
*/ |
| 11 |
|
| 12 |
#[cfg(not(feature = "critical-section"))] |
| 13 |
#[cfg_attr( |
| 14 |
all( |
| 15 |
target_arch = "arm", |
| 16 |
any(target_feature = "mclass", portable_atomic_target_feature = "mclass"), |
| 17 |
), |
| 18 |
path = "armv6m.rs" |
| 19 |
)] |
| 20 |
#[cfg_attr( |
| 21 |
all( |
| 22 |
target_arch = "arm", |
| 23 |
not(any(target_feature = "mclass", portable_atomic_target_feature = "mclass")), |
| 24 |
), |
| 25 |
path = "armv4t.rs" |
| 26 |
)] |
| 27 |
#[cfg_attr(target_arch = "avr", path = "avr.rs")] |
| 28 |
#[cfg_attr(target_arch = "msp430", path = "msp430.rs")] |
| 29 |
#[cfg_attr(any(target_arch = "riscv32", target_arch = "riscv64"), path = "riscv.rs")] |
| 30 |
#[cfg_attr(target_arch = "xtensa", path = "xtensa.rs")] |
| 31 |
pub(super) mod arch; |
| 32 |
|
| 33 |
#[cfg(not(all( |
| 34 |
portable_atomic_unsafe_assume_privileged, |
| 35 |
not(portable_atomic_unsafe_assume_single_core), |
| 36 |
)))] |
| 37 |
items!({ |
| 38 |
use core::{cell::UnsafeCell, sync::atomic::Ordering}; |
| 39 |
|
| 40 |
// critical-section implementations might use locks internally. |
| 41 |
#[cfg(feature = "critical-section")] |
| 42 |
const IS_ALWAYS_LOCK_FREE: bool = false; |
| 43 |
// Consider atomic operations based on disabling interrupts on single-core |
| 44 |
// systems are lock-free. (We consider the pre-v6 Arm Linux's atomic operations |
| 45 |
// provided in a similar way by the Linux kernel to be lock-free.) |
| 46 |
#[cfg(not(feature = "critical-section"))] |
| 47 |
const IS_ALWAYS_LOCK_FREE: bool = true; |
| 48 |
|
| 49 |
// Put this in its own module to prevent guard creation. |
| 50 |
use self::guard::disable; |
| 51 |
mod guard { |
| 52 |
// Note: The caller must NOT explicitly modify registers containing fields modified by disable/restore. |
| 53 |
// (Fields modified as side effects of other operations are covered by the absence of preserves_flags, |
| 54 |
// so they are fine -- see msp430.rs for more.) |
| 55 |
#[inline(always)] |
| 56 |
pub(super) fn disable() -> Guard { |
| 57 |
Guard { |
| 58 |
#[cfg(feature = "critical-section")] |
| 59 |
// SAFETY: the state will be restored in the subsequent `release`. |
| 60 |
state: unsafe { critical_section::acquire() }, |
| 61 |
#[cfg(not(feature = "critical-section"))] |
| 62 |
// Get current interrupt state and disable interrupts. |
| 63 |
state: super::arch::disable(), |
| 64 |
} |
| 65 |
} |
| 66 |
pub(super) struct Guard { |
| 67 |
#[cfg(feature = "critical-section")] |
| 68 |
state: critical_section::RestoreState, |
| 69 |
#[cfg(not(feature = "critical-section"))] |
| 70 |
state: super::arch::State, |
| 71 |
} |
| 72 |
impl Drop for Guard { |
| 73 |
#[inline(always)] |
| 74 |
fn drop(&mut self) { |
| 75 |
#[cfg(feature = "critical-section")] |
| 76 |
// SAFETY: the state was retrieved by the previous `acquire`. |
| 77 |
unsafe { |
| 78 |
critical_section::release(self.state); |
| 79 |
} |
| 80 |
#[cfg(not(feature = "critical-section"))] |
| 81 |
// Restore interrupt state. |
| 82 |
// SAFETY: the state was retrieved by the previous `disable`. |
| 83 |
unsafe { |
| 84 |
super::arch::restore(self.state); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
macro_rules! atomic_base { |
| 91 |
(base, $([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $align:literal) => { |
| 92 |
#[repr(C, align($align))] |
| 93 |
pub(crate) struct $atomic_type $(<$($generics)*>)? { |
| 94 |
v: UnsafeCell<$value_type>, |
| 95 |
} |
| 96 |
|
| 97 |
// Send is implicitly implemented for atomic integers, but not for atomic pointers. |
| 98 |
// SAFETY: any data races are prevented by disabling interrupts (or |
| 99 |
// atomic intrinsics) or critical-section (see module-level comments). |
| 100 |
unsafe impl $(<$($generics)*>)? Send for $atomic_type $(<$($generics)*>)? {} |
| 101 |
// SAFETY: any data races are prevented by disabling interrupts (or |
| 102 |
// atomic intrinsics) or critical-section (see module-level comments). |
| 103 |
unsafe impl $(<$($generics)*>)? Sync for $atomic_type $(<$($generics)*>)? {} |
| 104 |
|
| 105 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 106 |
#[inline] |
| 107 |
pub(crate) const fn new(v: $value_type) -> Self { |
| 108 |
Self { v: UnsafeCell::new(v) } |
| 109 |
} |
| 110 |
|
| 111 |
#[inline] |
| 112 |
pub(crate) fn is_lock_free() -> bool { |
| 113 |
Self::IS_ALWAYS_LOCK_FREE |
| 114 |
} |
| 115 |
pub(crate) const IS_ALWAYS_LOCK_FREE: bool = IS_ALWAYS_LOCK_FREE; |
| 116 |
|
| 117 |
#[inline] |
| 118 |
fn read(&self, _guard: &guard::Guard) -> $value_type { |
| 119 |
// SAFETY: any data races are prevented by disabling interrupts or critical-section (see |
| 120 |
// module-level comments) and the raw pointer is valid because we got it |
| 121 |
// from a reference. |
| 122 |
unsafe { self.v.get().read() } |
| 123 |
} |
| 124 |
#[inline] |
| 125 |
fn write(&self, val: $value_type, _guard: &guard::Guard) { |
| 126 |
// SAFETY: any data races are prevented by disabling interrupts or critical-section (see |
| 127 |
// module-level comments) and the raw pointer is valid because we got it |
| 128 |
// from a reference. |
| 129 |
unsafe { self.v.get().write(val) } |
| 130 |
} |
| 131 |
|
| 132 |
// As for CAS, there is no corresponding atomic operation on all architectures that use this code. |
| 133 |
// (If the CAS instruction exists, all atomic operations can be implemented by it, so this code will not be used.) |
| 134 |
#[inline] |
| 135 |
#[cfg_attr( |
| 136 |
all(debug_assertions, not(portable_atomic_no_track_caller)), |
| 137 |
track_caller |
| 138 |
)] |
| 139 |
pub(crate) fn compare_exchange( |
| 140 |
&self, |
| 141 |
current: $value_type, |
| 142 |
new: $value_type, |
| 143 |
success: Ordering, |
| 144 |
failure: Ordering, |
| 145 |
) -> Result<$value_type, $value_type> { |
| 146 |
crate::utils::assert_compare_exchange_ordering(success, failure); |
| 147 |
let guard = disable(); |
| 148 |
let prev = self.read(&guard); |
| 149 |
if prev == current { |
| 150 |
self.write(new, &guard); |
| 151 |
Ok(prev) |
| 152 |
} else { |
| 153 |
Err(prev) |
| 154 |
} |
| 155 |
} |
| 156 |
#[inline] |
| 157 |
#[cfg_attr( |
| 158 |
all(debug_assertions, not(portable_atomic_no_track_caller)), |
| 159 |
track_caller |
| 160 |
)] |
| 161 |
pub(crate) fn compare_exchange_weak( |
| 162 |
&self, |
| 163 |
current: $value_type, |
| 164 |
new: $value_type, |
| 165 |
success: Ordering, |
| 166 |
failure: Ordering, |
| 167 |
) -> Result<$value_type, $value_type> { |
| 168 |
self.compare_exchange(current, new, success, failure) |
| 169 |
} |
| 170 |
|
| 171 |
#[inline] |
| 172 |
pub(crate) const fn as_ptr(&self) -> *mut $value_type { |
| 173 |
self.v.get() |
| 174 |
} |
| 175 |
} |
| 176 |
}; |
| 177 |
(native_load_store, $([$($generics:tt)*])? $atomic_type:ident, $value_type:ty) => { |
| 178 |
impl $(<$($generics)*>)? core::ops::Deref for $atomic_type $(<$($generics)*>)? { |
| 179 |
type Target = atomic::$atomic_type $(<$($generics)*>)?; |
| 180 |
#[inline(always)] |
| 181 |
fn deref(&self) -> &Self::Target { |
| 182 |
// SAFETY: $atomic_type and atomic::$atomic_type have the same layout and |
| 183 |
// guarantee atomicity in a compatible way. (see module-level comments) |
| 184 |
unsafe { |
| 185 |
&*(self as *const Self as *const atomic::$atomic_type $(<$($generics)*>)?) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
}; |
| 190 |
(emulate_load_store, $([$($generics:tt)*])? $atomic_type:ident, $value_type:ty) => { |
| 191 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 192 |
#[inline] |
| 193 |
#[cfg_attr( |
| 194 |
all(debug_assertions, not(portable_atomic_no_track_caller)), |
| 195 |
track_caller |
| 196 |
)] |
| 197 |
pub(crate) fn load(&self, order: Ordering) -> $value_type { |
| 198 |
crate::utils::assert_load_ordering(order); |
| 199 |
let guard = disable(); |
| 200 |
self.read(&guard) |
| 201 |
} |
| 202 |
#[inline] |
| 203 |
#[cfg_attr( |
| 204 |
all(debug_assertions, not(portable_atomic_no_track_caller)), |
| 205 |
track_caller |
| 206 |
)] |
| 207 |
pub(crate) fn store(&self, val: $value_type, order: Ordering) { |
| 208 |
crate::utils::assert_store_ordering(order); |
| 209 |
let guard = disable(); |
| 210 |
self.write(val, &guard); |
| 211 |
} |
| 212 |
} |
| 213 |
}; |
| 214 |
(emulate_swap, $([$($generics:tt)*])? $atomic_type:ident, $value_type:ty) => { |
| 215 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 216 |
#[inline] |
| 217 |
pub(crate) fn swap(&self, val: $value_type, _order: Ordering) -> $value_type { |
| 218 |
let guard = disable(); |
| 219 |
let prev = self.read(&guard); |
| 220 |
self.write(val, &guard); |
| 221 |
prev |
| 222 |
} |
| 223 |
} |
| 224 |
}; |
| 225 |
} |
| 226 |
|
| 227 |
#[cfg_attr( |
| 228 |
portable_atomic_no_cfg_target_has_atomic, |
| 229 |
cfg(any( |
| 230 |
test, |
| 231 |
target_arch = "avr", |
| 232 |
target_arch = "msp430", |
| 233 |
portable_atomic_no_atomic_cas |
| 234 |
)) |
| 235 |
)] |
| 236 |
#[cfg_attr( |
| 237 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 238 |
cfg(any( |
| 239 |
test, |
| 240 |
target_arch = "avr", |
| 241 |
target_arch = "msp430", |
| 242 |
not(target_has_atomic = "ptr") |
| 243 |
)) |
| 244 |
)] |
| 245 |
items!({ |
| 246 |
#[cfg(target_pointer_width = "16")] |
| 247 |
atomic_base!(base, [T] AtomicPtr, *mut T, 2); |
| 248 |
#[cfg(target_pointer_width = "32")] |
| 249 |
atomic_base!(base, [T] AtomicPtr, *mut T, 4); |
| 250 |
#[cfg(target_pointer_width = "64")] |
| 251 |
atomic_base!(base, [T] AtomicPtr, *mut T, 8); |
| 252 |
#[cfg(target_pointer_width = "128")] |
| 253 |
atomic_base!(base, [T] AtomicPtr, *mut T, 16); |
| 254 |
|
| 255 |
impl_default_bit_opts!(AtomicPtr, usize); |
| 256 |
|
| 257 |
cfg_sel!({ |
| 258 |
#[cfg(any(target_arch = "avr", feature = "critical-section"))] |
| 259 |
{ |
| 260 |
atomic_base!(emulate_load_store, [T] AtomicPtr, *mut T); |
| 261 |
} |
| 262 |
#[cfg(else)] |
| 263 |
{ |
| 264 |
atomic_base!(native_load_store, [T] AtomicPtr, *mut T); |
| 265 |
} |
| 266 |
}); |
| 267 |
|
| 268 |
#[cfg(not(all( |
| 269 |
any(target_arch = "riscv32", target_arch = "riscv64"), |
| 270 |
not(feature = "critical-section"), |
| 271 |
any( |
| 272 |
portable_atomic_force_amo, |
| 273 |
target_feature = "zaamo", |
| 274 |
portable_atomic_target_feature = "zaamo", |
| 275 |
), |
| 276 |
)))] |
| 277 |
items!({ |
| 278 |
atomic_base!(emulate_swap, [T] AtomicPtr, *mut T); |
| 279 |
impl<T> AtomicPtr<T> { |
| 280 |
#[inline] |
| 281 |
pub(crate) fn fetch_byte_add(&self, val: usize, _order: Ordering) -> *mut T { |
| 282 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 283 |
use crate::utils::ptr::PtrExt as _; |
| 284 |
let guard = disable(); |
| 285 |
let prev = self.read(&guard); |
| 286 |
self.write(prev.with_addr(prev.addr().wrapping_add(val)), &guard); |
| 287 |
prev |
| 288 |
} |
| 289 |
#[inline] |
| 290 |
pub(crate) fn fetch_byte_sub(&self, val: usize, _order: Ordering) -> *mut T { |
| 291 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 292 |
use crate::utils::ptr::PtrExt as _; |
| 293 |
let guard = disable(); |
| 294 |
let prev = self.read(&guard); |
| 295 |
self.write(prev.with_addr(prev.addr().wrapping_sub(val)), &guard); |
| 296 |
prev |
| 297 |
} |
| 298 |
#[inline] |
| 299 |
pub(crate) fn fetch_and(&self, val: usize, _order: Ordering) -> *mut T { |
| 300 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 301 |
use crate::utils::ptr::PtrExt as _; |
| 302 |
let guard = disable(); |
| 303 |
let prev = self.read(&guard); |
| 304 |
self.write(prev.with_addr(prev.addr() & val), &guard); |
| 305 |
prev |
| 306 |
} |
| 307 |
#[inline] |
| 308 |
pub(crate) fn fetch_or(&self, val: usize, _order: Ordering) -> *mut T { |
| 309 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 310 |
use crate::utils::ptr::PtrExt as _; |
| 311 |
let guard = disable(); |
| 312 |
let prev = self.read(&guard); |
| 313 |
self.write(prev.with_addr(prev.addr() | val), &guard); |
| 314 |
prev |
| 315 |
} |
| 316 |
#[inline] |
| 317 |
pub(crate) fn fetch_xor(&self, val: usize, _order: Ordering) -> *mut T { |
| 318 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 319 |
use crate::utils::ptr::PtrExt as _; |
| 320 |
let guard = disable(); |
| 321 |
let prev = self.read(&guard); |
| 322 |
self.write(prev.with_addr(prev.addr() ^ val), &guard); |
| 323 |
prev |
| 324 |
} |
| 325 |
} |
| 326 |
}); |
| 327 |
|
| 328 |
impl<T> AtomicPtr<T> { |
| 329 |
#[cfg(test)] |
| 330 |
#[inline] |
| 331 |
fn fetch_ptr_add(&self, val: usize, order: Ordering) -> *mut T { |
| 332 |
self.fetch_byte_add(val.wrapping_mul(core::mem::size_of::<T>()), order) |
| 333 |
} |
| 334 |
#[cfg(test)] |
| 335 |
#[inline] |
| 336 |
fn fetch_ptr_sub(&self, val: usize, order: Ordering) -> *mut T { |
| 337 |
self.fetch_byte_sub(val.wrapping_mul(core::mem::size_of::<T>()), order) |
| 338 |
} |
| 339 |
} |
| 340 |
}); |
| 341 |
|
| 342 |
macro_rules! atomic_int { |
| 343 |
(base, $atomic_type:ident, $int_type:ty, $align:literal) => { |
| 344 |
atomic_base!(base, $atomic_type, $int_type, $align); |
| 345 |
// As for nand and neg, there is no corresponding atomic operation on all architectures that use this code. |
| 346 |
impl $atomic_type { |
| 347 |
#[inline] |
| 348 |
pub(crate) fn fetch_nand(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 349 |
let guard = disable(); |
| 350 |
let prev = self.read(&guard); |
| 351 |
self.write(!(prev & val), &guard); |
| 352 |
prev |
| 353 |
} |
| 354 |
#[inline] |
| 355 |
pub(crate) fn fetch_neg(&self, _order: Ordering) -> $int_type { |
| 356 |
let guard = disable(); |
| 357 |
let prev = self.read(&guard); |
| 358 |
self.write(prev.wrapping_neg(), &guard); |
| 359 |
prev |
| 360 |
} |
| 361 |
#[inline] |
| 362 |
pub(crate) fn neg(&self, order: Ordering) { |
| 363 |
self.fetch_neg(order); |
| 364 |
} |
| 365 |
} |
| 366 |
}; |
| 367 |
(load_store_atomic $([$kind:ident])?, $atomic_type:ident, $int_type:ty, $align:literal) => { |
| 368 |
cfg_sel!({ |
| 369 |
#[cfg(feature = "critical-section")] |
| 370 |
{ |
| 371 |
atomic_int!(all_critical_session, $atomic_type, $int_type, $align); |
| 372 |
} |
| 373 |
#[cfg(else)] |
| 374 |
{ |
| 375 |
atomic_int!(base, $atomic_type, $int_type, $align); |
| 376 |
impl_default_bit_opts!($atomic_type, $int_type); |
| 377 |
// load/store |
| 378 |
cfg_sel!({ |
| 379 |
// AVR with very old rustc |
| 380 |
#[cfg(all(target_arch = "avr", portable_atomic_no_asm))] |
| 381 |
{ |
| 382 |
atomic_base!(emulate_load_store, $atomic_type, $int_type); |
| 383 |
} |
| 384 |
#[cfg(else)] |
| 385 |
{ |
| 386 |
atomic_base!(native_load_store, $atomic_type, $int_type); |
| 387 |
} |
| 388 |
}); |
| 389 |
// RMW |
| 390 |
cfg_sel!({ |
| 391 |
// AVR 8-bit RMW with RMW instructions |
| 392 |
#[cfg(all( |
| 393 |
target_arch = "avr", |
| 394 |
not(portable_atomic_no_asm), |
| 395 |
any(target_feature = "rmw", portable_atomic_target_feature = "rmw"), |
| 396 |
))] |
| 397 |
{ |
| 398 |
atomic_int!(emulate_arithmetic, $atomic_type, $int_type); |
| 399 |
} |
| 400 |
// RISC-V RMW with Zaamo extension |
| 401 |
#[cfg(all( |
| 402 |
any(target_arch = "riscv32", target_arch = "riscv64"), |
| 403 |
any( |
| 404 |
portable_atomic_force_amo, |
| 405 |
target_feature = "zaamo", |
| 406 |
portable_atomic_target_feature = "zaamo", |
| 407 |
), |
| 408 |
))] |
| 409 |
{ |
| 410 |
atomic_int!(cas $([$kind])?, $atomic_type, $int_type); |
| 411 |
} |
| 412 |
#[cfg(else)] |
| 413 |
{ |
| 414 |
atomic_int!(cas[emulate], $atomic_type, $int_type); |
| 415 |
} |
| 416 |
}); |
| 417 |
// RMW (no-fetch) |
| 418 |
#[cfg(not(target_arch = "msp430"))] |
| 419 |
items!({ |
| 420 |
impl_default_no_fetch_ops!($atomic_type, $int_type); |
| 421 |
impl $atomic_type { |
| 422 |
#[inline] |
| 423 |
pub(crate) fn not(&self, order: Ordering) { |
| 424 |
self.fetch_not(order); |
| 425 |
} |
| 426 |
} |
| 427 |
}); |
| 428 |
} |
| 429 |
}); |
| 430 |
}; |
| 431 |
(all_critical_session, $atomic_type:ident, $int_type:ty, $align:literal) => { |
| 432 |
atomic_int!(base, $atomic_type, $int_type, $align); |
| 433 |
atomic_base!(emulate_load_store, $atomic_type, $int_type); |
| 434 |
atomic_int!(cas[emulate], $atomic_type, $int_type); |
| 435 |
impl_default_no_fetch_ops!($atomic_type, $int_type); |
| 436 |
impl_default_bit_opts!($atomic_type, $int_type); |
| 437 |
impl $atomic_type { |
| 438 |
#[inline] |
| 439 |
pub(crate) fn not(&self, order: Ordering) { |
| 440 |
self.fetch_not(order); |
| 441 |
} |
| 442 |
} |
| 443 |
}; |
| 444 |
(emulate_arithmetic, $atomic_type:ident, $int_type:ty) => { |
| 445 |
impl $atomic_type { |
| 446 |
#[inline] |
| 447 |
pub(crate) fn fetch_add(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 448 |
let guard = disable(); |
| 449 |
let prev = self.read(&guard); |
| 450 |
self.write(prev.wrapping_add(val), &guard); |
| 451 |
prev |
| 452 |
} |
| 453 |
#[inline] |
| 454 |
pub(crate) fn fetch_sub(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 455 |
let guard = disable(); |
| 456 |
let prev = self.read(&guard); |
| 457 |
self.write(prev.wrapping_sub(val), &guard); |
| 458 |
prev |
| 459 |
} |
| 460 |
#[inline] |
| 461 |
pub(crate) fn fetch_max(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 462 |
let guard = disable(); |
| 463 |
let prev = self.read(&guard); |
| 464 |
self.write(core::cmp::max(prev, val), &guard); |
| 465 |
prev |
| 466 |
} |
| 467 |
#[inline] |
| 468 |
pub(crate) fn fetch_min(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 469 |
let guard = disable(); |
| 470 |
let prev = self.read(&guard); |
| 471 |
self.write(core::cmp::min(prev, val), &guard); |
| 472 |
prev |
| 473 |
} |
| 474 |
} |
| 475 |
}; |
| 476 |
(emulate_bit, $atomic_type:ident, $int_type:ty) => { |
| 477 |
impl $atomic_type { |
| 478 |
#[inline] |
| 479 |
pub(crate) fn fetch_and(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 480 |
let guard = disable(); |
| 481 |
let prev = self.read(&guard); |
| 482 |
self.write(prev & val, &guard); |
| 483 |
prev |
| 484 |
} |
| 485 |
#[inline] |
| 486 |
pub(crate) fn fetch_or(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 487 |
let guard = disable(); |
| 488 |
let prev = self.read(&guard); |
| 489 |
self.write(prev | val, &guard); |
| 490 |
prev |
| 491 |
} |
| 492 |
#[inline] |
| 493 |
pub(crate) fn fetch_xor(&self, val: $int_type, _order: Ordering) -> $int_type { |
| 494 |
let guard = disable(); |
| 495 |
let prev = self.read(&guard); |
| 496 |
self.write(prev ^ val, &guard); |
| 497 |
prev |
| 498 |
} |
| 499 |
#[inline] |
| 500 |
pub(crate) fn fetch_not(&self, _order: Ordering) -> $int_type { |
| 501 |
let guard = disable(); |
| 502 |
let prev = self.read(&guard); |
| 503 |
self.write(!prev, &guard); |
| 504 |
prev |
| 505 |
} |
| 506 |
} |
| 507 |
}; |
| 508 |
(cas[emulate], $atomic_type:ident, $int_type:ty) => { |
| 509 |
atomic_base!(emulate_swap, $atomic_type, $int_type); |
| 510 |
atomic_int!(emulate_arithmetic, $atomic_type, $int_type); |
| 511 |
atomic_int!(emulate_bit, $atomic_type, $int_type); |
| 512 |
}; |
| 513 |
// RISC-V 32-bit(RV32)/{32,64}-bit(RV64) RMW with Zaamo extension |
| 514 |
// RISC-V 8-bit/16-bit RMW with Zabha extension |
| 515 |
(cas, $atomic_type:ident, $int_type:ty) => {}; |
| 516 |
// RISC-V 8-bit/16-bit RMW with Zaamo extension |
| 517 |
(cas[sub_word], $atomic_type:ident, $int_type:ty) => { |
| 518 |
// RISC-V 8-bit/16-bit RMW with Zaamo+Zabha extension |
| 519 |
#[cfg(any(target_feature = "zabha", portable_atomic_target_feature = "zabha"))] |
| 520 |
atomic_int!(cas, $atomic_type, $int_type); |
| 521 |
|
| 522 |
// RISC-V 8-bit/16-bit RMW with Zaamo extension |
| 523 |
#[cfg(not(any(target_feature = "zabha", portable_atomic_target_feature = "zabha")))] |
| 524 |
atomic_base!(emulate_swap, $atomic_type, $int_type); |
| 525 |
#[cfg(not(any(target_feature = "zabha", portable_atomic_target_feature = "zabha")))] |
| 526 |
atomic_int!(emulate_arithmetic, $atomic_type, $int_type); |
| 527 |
}; |
| 528 |
} |
| 529 |
|
| 530 |
#[cfg_attr( |
| 531 |
portable_atomic_no_cfg_target_has_atomic, |
| 532 |
cfg(any( |
| 533 |
test, |
| 534 |
target_arch = "avr", |
| 535 |
target_arch = "msp430", |
| 536 |
portable_atomic_no_atomic_cas, |
| 537 |
)) |
| 538 |
)] |
| 539 |
#[cfg_attr( |
| 540 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 541 |
cfg(any( |
| 542 |
test, |
| 543 |
target_arch = "avr", |
| 544 |
target_arch = "msp430", |
| 545 |
not(target_has_atomic = "ptr"), |
| 546 |
)) |
| 547 |
)] |
| 548 |
items!({ |
| 549 |
// On some platforms, atomic load/store can be implemented in a more efficient |
| 550 |
// way than disabling interrupts. On MSP430, some RMWs that do not return the |
| 551 |
// previous value can also be optimized. |
| 552 |
// |
| 553 |
// Note: On single-core systems, it is okay to use critical session-based |
| 554 |
// CAS together with atomic load/store. The load/store will not be |
| 555 |
// called while interrupts are disabled, and since the load/store is |
| 556 |
// atomic, it is not affected by interrupts even if interrupts are enabled. |
| 557 |
#[cfg(not(any( |
| 558 |
all(target_arch = "avr", portable_atomic_no_asm), |
| 559 |
feature = "critical-section", |
| 560 |
)))] |
| 561 |
use self::arch::atomic; |
| 562 |
|
| 563 |
#[cfg(target_pointer_width = "16")] |
| 564 |
#[cfg(not(target_arch = "avr"))] |
| 565 |
atomic_int!(load_store_atomic, AtomicIsize, isize, 2); |
| 566 |
#[cfg(target_pointer_width = "16")] |
| 567 |
#[cfg(not(target_arch = "avr"))] |
| 568 |
atomic_int!(load_store_atomic, AtomicUsize, usize, 2); |
| 569 |
#[cfg(target_arch = "avr")] |
| 570 |
atomic_int!(all_critical_session, AtomicIsize, isize, 2); |
| 571 |
#[cfg(target_arch = "avr")] |
| 572 |
atomic_int!(all_critical_session, AtomicUsize, usize, 2); |
| 573 |
#[cfg(target_pointer_width = "32")] |
| 574 |
atomic_int!(load_store_atomic, AtomicIsize, isize, 4); |
| 575 |
#[cfg(target_pointer_width = "32")] |
| 576 |
atomic_int!(load_store_atomic, AtomicUsize, usize, 4); |
| 577 |
#[cfg(target_pointer_width = "64")] |
| 578 |
atomic_int!(load_store_atomic, AtomicIsize, isize, 8); |
| 579 |
#[cfg(target_pointer_width = "64")] |
| 580 |
atomic_int!(load_store_atomic, AtomicUsize, usize, 8); |
| 581 |
#[cfg(target_pointer_width = "128")] |
| 582 |
atomic_int!(load_store_atomic, AtomicIsize, isize, 16); |
| 583 |
#[cfg(target_pointer_width = "128")] |
| 584 |
atomic_int!(load_store_atomic, AtomicUsize, usize, 16); |
| 585 |
|
| 586 |
#[cfg(not(all(target_arch = "avr", portable_atomic_no_asm)))] |
| 587 |
atomic_int!(load_store_atomic[sub_word], AtomicI8, i8, 1); |
| 588 |
#[cfg(not(all(target_arch = "avr", portable_atomic_no_asm)))] |
| 589 |
atomic_int!(load_store_atomic[sub_word], AtomicU8, u8, 1); |
| 590 |
#[cfg(all(target_arch = "avr", portable_atomic_no_asm))] |
| 591 |
atomic_int!(all_critical_session, AtomicI8, i8, 1); |
| 592 |
#[cfg(all(target_arch = "avr", portable_atomic_no_asm))] |
| 593 |
atomic_int!(all_critical_session, AtomicU8, u8, 1); |
| 594 |
#[cfg(not(target_arch = "avr"))] |
| 595 |
atomic_int!(load_store_atomic[sub_word], AtomicI16, i16, 2); |
| 596 |
#[cfg(not(target_arch = "avr"))] |
| 597 |
atomic_int!(load_store_atomic[sub_word], AtomicU16, u16, 2); |
| 598 |
#[cfg(target_arch = "avr")] |
| 599 |
atomic_int!(all_critical_session, AtomicI16, i16, 2); |
| 600 |
#[cfg(target_arch = "avr")] |
| 601 |
atomic_int!(all_critical_session, AtomicU16, u16, 2); |
| 602 |
|
| 603 |
#[cfg(not(target_pointer_width = "16"))] |
| 604 |
atomic_int!(load_store_atomic, AtomicI32, i32, 4); |
| 605 |
#[cfg(not(target_pointer_width = "16"))] |
| 606 |
atomic_int!(load_store_atomic, AtomicU32, u32, 4); |
| 607 |
|
| 608 |
cfg_has_fast_atomic_64!({ |
| 609 |
atomic_int!(load_store_atomic, AtomicI64, i64, 8); |
| 610 |
atomic_int!(load_store_atomic, AtomicU64, u64, 8); |
| 611 |
}); |
| 612 |
}); |
| 613 |
|
| 614 |
// Double or more width atomics (require fallback feature for consistency with other situations). |
| 615 |
#[cfg(target_pointer_width = "16")] |
| 616 |
#[cfg(any(test, feature = "fallback"))] |
| 617 |
items!({ |
| 618 |
atomic_int!(all_critical_session, AtomicI32, i32, 4); |
| 619 |
atomic_int!(all_critical_session, AtomicU32, u32, 4); |
| 620 |
}); |
| 621 |
#[cfg(any( |
| 622 |
test, |
| 623 |
all( |
| 624 |
feature = "fallback", |
| 625 |
not(all( |
| 626 |
target_arch = "riscv32", |
| 627 |
not(any(miri, portable_atomic_sanitize_thread)), |
| 628 |
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm), |
| 629 |
any( |
| 630 |
target_feature = "zacas", |
| 631 |
portable_atomic_target_feature = "zacas", |
| 632 |
all( |
| 633 |
feature = "fallback", |
| 634 |
not(portable_atomic_no_outline_atomics), |
| 635 |
any(target_os = "linux", target_os = "android"), |
| 636 |
), |
| 637 |
), |
| 638 |
)), |
| 639 |
), |
| 640 |
))] |
| 641 |
#[cfg_attr( |
| 642 |
portable_atomic_no_cfg_target_has_atomic, |
| 643 |
cfg(any(test, portable_atomic_no_atomic_64)) |
| 644 |
)] |
| 645 |
#[cfg_attr( |
| 646 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 647 |
cfg(any(test, not(target_has_atomic = "64"))) |
| 648 |
)] |
| 649 |
cfg_no_fast_atomic_64!({ |
| 650 |
atomic_int!(all_critical_session, AtomicI64, i64, 8); |
| 651 |
atomic_int!(all_critical_session, AtomicU64, u64, 8); |
| 652 |
}); |
| 653 |
#[cfg(any( |
| 654 |
test, |
| 655 |
all( |
| 656 |
feature = "fallback", |
| 657 |
not(all( |
| 658 |
target_arch = "riscv64", |
| 659 |
not(any(miri, portable_atomic_sanitize_thread)), |
| 660 |
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm), |
| 661 |
any( |
| 662 |
target_feature = "zacas", |
| 663 |
portable_atomic_target_feature = "zacas", |
| 664 |
all( |
| 665 |
feature = "fallback", |
| 666 |
not(portable_atomic_no_outline_atomics), |
| 667 |
any(target_os = "linux", target_os = "android"), |
| 668 |
), |
| 669 |
), |
| 670 |
)), |
| 671 |
), |
| 672 |
))] |
| 673 |
items!({ |
| 674 |
atomic_int!(all_critical_session, AtomicI128, i128, 16); |
| 675 |
atomic_int!(all_critical_session, AtomicU128, u128, 16); |
| 676 |
}); |
| 677 |
}); |
| 678 |
|
| 679 |
#[cfg(test)] |
| 680 |
mod tests { |
| 681 |
use super::*; |
| 682 |
|
| 683 |
test_atomic_ptr_single_thread!(); |
| 684 |
test_atomic_int_single_thread!(i8); |
| 685 |
test_atomic_int_single_thread!(u8); |
| 686 |
test_atomic_int_single_thread!(i16); |
| 687 |
test_atomic_int_single_thread!(u16); |
| 688 |
test_atomic_int_single_thread!(i32); |
| 689 |
test_atomic_int_single_thread!(u32); |
| 690 |
test_atomic_int_single_thread!(i64); |
| 691 |
test_atomic_int_single_thread!(u64); |
| 692 |
test_atomic_int_single_thread!(i128); |
| 693 |
test_atomic_int_single_thread!(u128); |
| 694 |
test_atomic_int_single_thread!(isize); |
| 695 |
test_atomic_int_single_thread!(usize); |
| 696 |
} |