#![no_std]
#![doc(test(
no_crate_inject,
attr(
deny(warnings, rust_2018_idioms, single_use_lifetimes),
allow(dead_code, unused_variables)
)
))]
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
single_use_lifetimes,
unreachable_pub
)]
#![cfg_attr(not(portable_atomic_no_unsafe_op_in_unsafe_fn), warn(unsafe_op_in_unsafe_fn))] #![cfg_attr(portable_atomic_no_unsafe_op_in_unsafe_fn, allow(unused_unsafe))]
#![warn(
clippy::pedantic,
// lints for public library
clippy::alloc_instead_of_core,
clippy::exhaustive_enums,
clippy::exhaustive_structs,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
// lints that help writing unsafe code
clippy::default_union_representation,
clippy::trailing_empty_array,
clippy::transmute_undefined_repr,
clippy::undocumented_unsafe_blocks,
// misc
clippy::inline_asm_x86_att_syntax,
clippy::missing_inline_in_public_items,
)]
#![allow(
clippy::box_default,
clippy::cast_lossless,
clippy::doc_markdown,
clippy::float_cmp,
clippy::inline_always,
clippy::missing_errors_doc,
clippy::module_inception,
clippy::similar_names,
clippy::single_match,
clippy::type_complexity
)]
#![cfg_attr(
all(
portable_atomic_nightly,
target_arch = "x86_64",
any(
test,
all(
portable_atomic_cmpxchg16b_dynamic,
not(any(
target_feature = "cmpxchg16b",
portable_atomic_target_feature = "cmpxchg16b",
))
)
)
),
feature(cmpxchg16b_target_feature)
)]
#![cfg_attr(
all(
portable_atomic_nightly,
not(portable_atomic_no_asm),
any(
target_arch = "avr",
target_arch = "msp430",
all(
portable_atomic_asm_experimental_arch,
target_arch = "powerpc64",
any(
target_feature = "quadword-atomics",
portable_atomic_target_feature = "quadword-atomics"
)
),
all(portable_atomic_asm_experimental_arch, target_arch = "s390x"),
),
),
feature(asm_experimental_arch)
)]
#![cfg_attr(portable_atomic_unstable_cfg_target_has_atomic, feature(cfg_target_has_atomic))]
#![cfg_attr(
all(
portable_atomic_unstable_asm,
any(
all(
any(target_arch = "arm", target_arch = "riscv32", target_arch = "riscv64"),
not(target_has_atomic = "ptr")
),
target_arch = "aarch64",
target_arch = "x86",
target_arch = "x86_64",
),
),
feature(asm)
)]
#![cfg_attr(
all(any(target_arch = "avr", target_arch = "msp430"), portable_atomic_no_asm),
feature(llvm_asm)
)]
#![cfg_attr(
all(
portable_atomic_unstable_isa_attribute,
any(test, portable_atomic_unsafe_assume_single_core),
target_arch = "arm",
not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
not(target_has_atomic = "ptr"),
),
feature(isa_attribute)
)]
#![cfg_attr(
all(
any(target_arch = "aarch64", target_arch = "powerpc64", target_arch = "s390x"),
any(all(test, portable_atomic_nightly), miri, portable_atomic_sanitize_thread)
),
feature(core_intrinsics)
)]
#![cfg_attr(
all(
target_arch = "x86_64",
any(all(test, portable_atomic_nightly), miri, portable_atomic_sanitize_thread)
),
feature(stdsimd)
)]
#![cfg_attr(
all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr),
feature(strict_provenance_atomic_ptr)
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(not(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64",
)))]
compile_error!(
"portable-atomic currently only supports targets with {16,32,64}-bit pointer width; \
if you need support for others, \
please submit an issue at <https://github.com/taiki-e/portable-atomic>"
);
#[cfg(portable_atomic_unsafe_assume_single_core)]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
not(any(
target_arch = "arm",
target_arch = "avr",
target_arch = "msp430",
target_arch = "riscv32",
target_arch = "riscv64",
))
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
not(any(
target_arch = "arm",
target_arch = "avr",
target_arch = "msp430",
target_arch = "riscv32",
target_arch = "riscv64",
))
))
)]
compile_error!(
"cfg(portable_atomic_unsafe_assume_single_core) does not compatible with this target; \
if you need cfg(portable_atomic_unsafe_assume_single_core) support for this target, \
please submit an issue at <https://github.com/taiki-e/portable-atomic>"
);
#[cfg(portable_atomic_no_outline_atomics)]
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
compile_error!("cfg(portable_atomic_no_outline_atomics) does not compatible with this target");
#[cfg(portable_atomic_disable_fiq)]
#[cfg(not(all(
target_arch = "arm",
not(any(target_feature = "mclass", portable_atomic_target_feature = "mclass"))
)))]
compile_error!("cfg(portable_atomic_disable_fiq) does not compatible with this target");
#[cfg(portable_atomic_s_mode)]
#[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64")))]
compile_error!("cfg(portable_atomic_s_mode) does not compatible with this target");
#[cfg(portable_atomic_disable_fiq)]
#[cfg(not(portable_atomic_unsafe_assume_single_core))]
compile_error!(
"cfg(portable_atomic_disable_fiq) may only be used together with cfg(portable_atomic_unsafe_assume_single_core)"
);
#[cfg(portable_atomic_s_mode)]
#[cfg(not(portable_atomic_unsafe_assume_single_core))]
compile_error!(
"cfg(portable_atomic_s_mode) may only be used together with cfg(portable_atomic_unsafe_assume_single_core)"
);
#[cfg(all(portable_atomic_unsafe_assume_single_core, feature = "critical-section"))]
compile_error!(
"you may not enable feature `critical-section` and cfg(portable_atomic_unsafe_assume_single_core) at the same time"
);
#[cfg(any(test, feature = "std"))]
extern crate std;
#[macro_use]
mod utils;
#[cfg(test)]
#[macro_use]
mod tests;
#[doc(no_inline)]
pub use core::sync::atomic::Ordering;
#[doc(no_inline)]
#[cfg(not(target_arch = "msp430"))]
pub use core::sync::atomic::{compiler_fence, fence};
#[cfg(target_arch = "msp430")]
pub use imp::msp430::{compiler_fence, fence};
mod imp;
pub mod hint {
#[doc(no_inline)]
pub use core::hint::*;
#[inline]
pub fn spin_loop() {
#[allow(deprecated)]
core::sync::atomic::spin_loop_hint();
}
}
#[cfg(doc)]
use core::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed, Release, SeqCst};
use core::{fmt, marker::PhantomData, ptr};
use crate::utils::NoRefUnwindSafe;
#[repr(C, align(1))]
pub struct AtomicBool {
inner: imp::AtomicBool,
_marker: PhantomData<NoRefUnwindSafe>,
}
static_assert_layout!(AtomicBool, bool);
impl Default for AtomicBool {
#[inline]
fn default() -> Self {
Self::new(false)
}
}
impl From<bool> for AtomicBool {
#[inline]
fn from(b: bool) -> Self {
Self::new(b)
}
}
impl fmt::Debug for AtomicBool {
#[allow(clippy::missing_inline_in_public_items)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.load(Ordering::Relaxed), f)
}
}
#[cfg(not(portable_atomic_no_core_unwind_safe))]
impl core::panic::RefUnwindSafe for AtomicBool {}
#[cfg(all(portable_atomic_no_core_unwind_safe, feature = "std"))]
impl std::panic::RefUnwindSafe for AtomicBool {}
serde_impls!(AtomicBool);
impl AtomicBool {
#[inline]
#[must_use]
pub const fn new(v: bool) -> Self {
Self { inner: imp::AtomicBool::new(v), _marker: PhantomData }
}
#[inline]
#[must_use]
pub fn is_lock_free() -> bool {
imp::AtomicBool::is_lock_free()
}
#[inline]
#[must_use]
pub const fn is_always_lock_free() -> bool {
imp::AtomicBool::is_always_lock_free()
}
#[inline]
pub fn get_mut(&mut self) -> &mut bool {
self.inner.get_mut()
}
#[inline]
pub fn into_inner(self) -> bool {
self.inner.into_inner()
}
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn load(&self, order: Ordering) -> bool {
self.inner.load(order)
}
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn store(&self, val: bool, order: Ordering) {
self.inner.store(val, order);
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430",
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn swap(&self, val: bool, order: Ordering) -> bool {
self.inner.swap(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn compare_exchange(
&self,
current: bool,
new: bool,
success: Ordering,
failure: Ordering,
) -> Result<bool, bool> {
self.inner.compare_exchange(current, new, success, failure)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn compare_exchange_weak(
&self,
current: bool,
new: bool,
success: Ordering,
failure: Ordering,
) -> Result<bool, bool> {
self.inner.compare_exchange_weak(current, new, success, failure)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
self.inner.fetch_and(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn and(&self, val: bool, order: Ordering) {
self.inner.and(val, order);
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
self.inner.fetch_nand(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
self.inner.fetch_or(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn or(&self, val: bool, order: Ordering) {
self.inner.or(val, order);
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
self.inner.fetch_xor(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn xor(&self, val: bool, order: Ordering) {
self.inner.xor(val, order);
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_not(&self, order: Ordering) -> bool {
self.fetch_xor(true, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn not(&self, order: Ordering) {
self.xor(true, order);
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> Result<bool, bool>
where
F: FnMut(bool) -> Option<bool>,
{
let mut prev = self.load(fetch_order);
while let Some(next) = f(prev) {
match self.compare_exchange_weak(prev, next, set_order, fetch_order) {
x @ Ok(_) => return x,
Err(next_prev) => prev = next_prev,
}
}
Err(prev)
}
}
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
#[cfg_attr(target_pointer_width = "128", repr(C, align(16)))]
pub struct AtomicPtr<T> {
inner: imp::AtomicPtr<T>,
_marker: PhantomData<NoRefUnwindSafe>,
}
static_assert_layout!(AtomicPtr<()>, *mut ());
impl<T> Default for AtomicPtr<T> {
#[inline]
fn default() -> Self {
Self::new(ptr::null_mut())
}
}
impl<T> From<*mut T> for AtomicPtr<T> {
#[inline]
fn from(p: *mut T) -> Self {
Self::new(p)
}
}
impl<T> fmt::Debug for AtomicPtr<T> {
#[allow(clippy::missing_inline_in_public_items)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.load(Ordering::Relaxed), f)
}
}
impl<T> fmt::Pointer for AtomicPtr<T> {
#[allow(clippy::missing_inline_in_public_items)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.load(Ordering::Relaxed), f)
}
}
#[cfg(not(portable_atomic_no_core_unwind_safe))]
impl<T> core::panic::RefUnwindSafe for AtomicPtr<T> {}
#[cfg(all(portable_atomic_no_core_unwind_safe, feature = "std"))]
impl<T> std::panic::RefUnwindSafe for AtomicPtr<T> {}
impl<T> AtomicPtr<T> {
#[inline]
#[must_use]
pub const fn new(p: *mut T) -> Self {
Self { inner: imp::AtomicPtr::new(p), _marker: PhantomData }
}
#[inline]
#[must_use]
pub fn is_lock_free() -> bool {
<imp::AtomicPtr<T>>::is_lock_free()
}
#[inline]
#[must_use]
pub const fn is_always_lock_free() -> bool {
<imp::AtomicPtr<T>>::is_always_lock_free()
}
#[inline]
pub fn get_mut(&mut self) -> &mut *mut T {
self.inner.get_mut()
}
#[inline]
pub fn into_inner(self) -> *mut T {
self.inner.into_inner()
}
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn load(&self, order: Ordering) -> *mut T {
self.inner.load(order)
}
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn store(&self, ptr: *mut T, order: Ordering) {
self.inner.store(ptr, order);
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
self.inner.swap(ptr, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn compare_exchange(
&self,
current: *mut T,
new: *mut T,
success: Ordering,
failure: Ordering,
) -> Result<*mut T, *mut T> {
self.inner.compare_exchange(current, new, success, failure)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn compare_exchange_weak(
&self,
current: *mut T,
new: *mut T,
success: Ordering,
failure: Ordering,
) -> Result<*mut T, *mut T> {
self.inner.compare_exchange_weak(current, new, success, failure)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> Result<*mut T, *mut T>
where
F: FnMut(*mut T) -> Option<*mut T>,
{
let mut prev = self.load(fetch_order);
while let Some(next) = f(prev) {
match self.compare_exchange_weak(prev, next, set_order, fetch_order) {
x @ Ok(_) => return x,
Err(next_prev) => prev = next_prev,
}
}
Err(prev)
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_ptr_add(&self, val: usize, order: Ordering) -> *mut T {
self.fetch_byte_add(val.wrapping_mul(core::mem::size_of::<T>()), order)
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_ptr_sub(&self, val: usize, order: Ordering) -> *mut T {
self.fetch_byte_sub(val.wrapping_mul(core::mem::size_of::<T>()), order)
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_byte_add(&self, val: usize, order: Ordering) -> *mut T {
#[cfg(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr))]
{
self.inner.fetch_byte_add(val, order)
}
#[cfg(not(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr)))]
{
self.as_atomic_usize().fetch_add(val, order) as *mut T
}
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_byte_sub(&self, val: usize, order: Ordering) -> *mut T {
#[cfg(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr))]
{
self.inner.fetch_byte_sub(val, order)
}
#[cfg(not(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr)))]
{
self.as_atomic_usize().fetch_sub(val, order) as *mut T
}
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_or(&self, val: usize, order: Ordering) -> *mut T {
#[cfg(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr))]
{
self.inner.fetch_or(val, order)
}
#[cfg(not(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr)))]
{
self.as_atomic_usize().fetch_or(val, order) as *mut T
}
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_and(&self, val: usize, order: Ordering) -> *mut T {
#[cfg(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr))]
{
self.inner.fetch_and(val, order)
}
#[cfg(not(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr)))]
{
self.as_atomic_usize().fetch_and(val, order) as *mut T
}
}
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
pub fn fetch_xor(&self, val: usize, order: Ordering) -> *mut T {
#[cfg(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr))]
{
self.inner.fetch_xor(val, order)
}
#[cfg(not(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr)))]
{
self.as_atomic_usize().fetch_xor(val, order) as *mut T
}
}
#[cfg(not(all(miri, portable_atomic_unstable_strict_provenance_atomic_ptr)))]
#[inline]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
fn as_atomic_usize(&self) -> &AtomicUsize {
let [] = [(); core::mem::size_of::<AtomicPtr<()>>() - core::mem::size_of::<AtomicUsize>()];
let [] =
[(); core::mem::align_of::<AtomicPtr<()>>() - core::mem::align_of::<AtomicUsize>()];
unsafe { &*(self as *const AtomicPtr<T> as *const AtomicUsize) }
}
}
macro_rules! atomic_int {
(AtomicU8, $int_type:ident, $align:expr) => {
atomic_int!(uint, AtomicU8, $int_type, $align);
};
(AtomicU16, $int_type:ident, $align:expr) => {
atomic_int!(uint, AtomicU16, $int_type, $align);
};
(AtomicU32, $int_type:ident, $align:expr) => {
atomic_int!(uint, AtomicU32, $int_type, $align);
#[cfg(feature = "float")]
atomic_int!(float, AtomicF32, f32, AtomicU32, $int_type, $align);
};
(AtomicU64, $int_type:ident, $align:expr) => {
atomic_int!(uint, AtomicU64, $int_type, $align);
#[cfg(feature = "float")]
atomic_int!(float, AtomicF64, f64, AtomicU64, $int_type, $align);
};
(AtomicU128, $int_type:ident, $align:expr) => {
atomic_int!(uint, AtomicU128, $int_type, $align);
};
(AtomicUsize, $int_type:ident, $align:expr) => {
atomic_int!(uint, AtomicUsize, $int_type, $align);
};
($atomic_type:ident, $int_type:ident, $align:expr) => {
atomic_int!(int, $atomic_type, $int_type, $align);
};
(uint,
$atomic_type:ident, $int_type:ident, $align:expr
) => {
doc_comment! {
concat!("An integer type which can be safely shared between threads.
This type has the same in-memory representation as the underlying integer type,
[`", stringify!($int_type), "`].
If the compiler and the platform support atomic loads and stores of [`", stringify!($int_type),
"`], this type is a wrapper for the standard library's `", stringify!($atomic_type),
"`. If the platform supports it but the compiler does not, atomic operations are implemented using
inline assembly. Otherwise synchronizes using global locks.
You can call [`", stringify!($atomic_type), "::is_lock_free()`] to check whether
atomic instructions or locks will be used.
"
),
#[repr(C, align($align))]
pub struct $atomic_type {
inner: imp::$atomic_type,
_marker: PhantomData<NoRefUnwindSafe>,
}
}
static_assert_layout!($atomic_type, $int_type);
impl Default for $atomic_type {
#[inline]
fn default() -> Self {
Self::new($int_type::default())
}
}
impl From<$int_type> for $atomic_type {
#[inline]
fn from(v: $int_type) -> Self {
Self::new(v)
}
}
impl fmt::Debug for $atomic_type {
#[allow(clippy::missing_inline_in_public_items)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.load(Ordering::Relaxed), f)
}
}
#[cfg(not(portable_atomic_no_core_unwind_safe))]
impl core::panic::RefUnwindSafe for $atomic_type {}
#[cfg(all(portable_atomic_no_core_unwind_safe, feature = "std"))]
impl std::panic::RefUnwindSafe for $atomic_type {}
serde_impls!($atomic_type);
impl $atomic_type {
doc_comment! {
concat!(
"Creates a new atomic integer.
# Examples
```
use portable_atomic::", stringify!($atomic_type), ";
let atomic_forty_two = ", stringify!($atomic_type), "::new(42);
```"
),
#[inline]
#[must_use]
pub const fn new(v: $int_type) -> Self {
Self { inner: imp::$atomic_type::new(v), _marker: PhantomData }
}
}
doc_comment! {
concat!("Returns `true` if operations on values of this type are lock-free.
If the compiler or the platform doesn't support the necessary
atomic instructions, global locks for every potentially
concurrent atomic operation will be used.
# Examples
```
use portable_atomic::", stringify!($atomic_type), ";
let is_lock_free = ", stringify!($atomic_type), "::is_lock_free();
```"),
#[inline]
#[must_use]
pub fn is_lock_free() -> bool {
<imp::$atomic_type>::is_lock_free()
}
}
doc_comment! {
concat!("Returns `true` if operations on values of this type are lock-free.
If the compiler or the platform doesn't support the necessary
atomic instructions, global locks for every potentially
concurrent atomic operation will be used.
**Note:** If the atomic operation relies on dynamic CPU feature detection,
this type may be lock-free even if the function returns false.
# Examples
```
use portable_atomic::", stringify!($atomic_type), ";
const IS_ALWAYS_LOCK_FREE: bool = ", stringify!($atomic_type), "::is_always_lock_free();
```"),
#[inline]
#[must_use]
pub const fn is_always_lock_free() -> bool {
<imp::$atomic_type>::is_always_lock_free()
}
}
doc_comment! {
concat!("Returns a mutable reference to the underlying integer.\n
This is safe because the mutable reference guarantees that no other threads are
concurrently accessing the atomic data.
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let mut some_var = ", stringify!($atomic_type), "::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
```"),
#[inline]
pub fn get_mut(&mut self) -> &mut $int_type {
self.inner.get_mut()
}
}
doc_comment! {
concat!("Consumes the atomic and returns the contained value.
This is safe because passing `self` by value guarantees that no other threads are
concurrently accessing the atomic data.
# Examples
```
use portable_atomic::", stringify!($atomic_type), ";
let some_var = ", stringify!($atomic_type), "::new(5);
assert_eq!(some_var.into_inner(), 5);
```"),
#[inline]
pub fn into_inner(self) -> $int_type {
self.inner.into_inner()
}
}
doc_comment! {
concat!("Loads a value from the atomic integer.
`load` takes an [`Ordering`] argument which describes the memory ordering of this operation.
Possible values are [`SeqCst`], [`Acquire`] and [`Relaxed`].
# Panics
Panics if `order` is [`Release`] or [`AcqRel`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let some_var = ", stringify!($atomic_type), "::new(5);
assert_eq!(some_var.load(Ordering::Relaxed), 5);
```"),
#[inline]
#[cfg_attr(
all(debug_assertions, not(portable_atomic_no_track_caller)),
track_caller
)]
pub fn load(&self, order: Ordering) -> $int_type {
self.inner.load(order)
}
}
doc_comment! {
concat!("Stores a value into the atomic integer.
`store` takes an [`Ordering`] argument which describes the memory ordering of this operation.
Possible values are [`SeqCst`], [`Release`] and [`Relaxed`].
# Panics
Panics if `order` is [`Acquire`] or [`AcqRel`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let some_var = ", stringify!($atomic_type), "::new(5);
some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
```"),
#[inline]
#[cfg_attr(
all(debug_assertions, not(portable_atomic_no_track_caller)),
track_caller
)]
pub fn store(&self, val: $int_type, order: Ordering) {
self.inner.store(val, order)
}
}
doc_comment! {
concat!("Stores a value into the atomic integer, returning the previous value.
`swap` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let some_var = ", stringify!($atomic_type), "::new(5);
assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn swap(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.swap(val, order)
}
}
doc_comment! {
concat!("Stores a value into the atomic integer if the current value is the same as
the `current` value.
The return value is a result indicating whether the new value was written and
containing the previous value. On success this value is guaranteed to be equal to
`current`.
`compare_exchange` takes two [`Ordering`] arguments to describe the memory
ordering of this operation. `success` describes the required ordering for the
read-modify-write operation that takes place if the comparison with `current` succeeds.
`failure` describes the required ordering for the load operation that takes place when
the comparison fails. Using [`Acquire`] as success ordering makes the store part
of this operation [`Relaxed`], and using [`Release`] makes the successful load
[`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
# Panics
Panics if `failure` is [`Release`], [`AcqRel`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let some_var = ", stringify!($atomic_type), "::new(5);
assert_eq!(
some_var.compare_exchange(5, 10, Ordering::Acquire, Ordering::Relaxed),
Ok(5),
);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
assert_eq!(
some_var.compare_exchange(6, 12, Ordering::SeqCst, Ordering::Acquire),
Err(10),
);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(
all(debug_assertions, not(portable_atomic_no_track_caller)),
track_caller
)]
pub fn compare_exchange(
&self,
current: $int_type,
new: $int_type,
success: Ordering,
failure: Ordering,
) -> Result<$int_type, $int_type> {
self.inner.compare_exchange(current, new, success, failure)
}
}
doc_comment! {
concat!("Stores a value into the atomic integer if the current value is the same as
the `current` value.
Unlike [`compare_exchange`](Self::compare_exchange)
this function is allowed to spuriously fail even
when the comparison succeeds, which can result in more efficient code on some
platforms. The return value is a result indicating whether the new value was
written and containing the previous value.
`compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
ordering of this operation. `success` describes the required ordering for the
read-modify-write operation that takes place if the comparison with `current` succeeds.
`failure` describes the required ordering for the load operation that takes place when
the comparison fails. Using [`Acquire`] as success ordering makes the store part
of this operation [`Relaxed`], and using [`Release`] makes the successful load
[`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
# Panics
Panics if `failure` is [`Release`], [`AcqRel`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let val = ", stringify!($atomic_type), "::new(4);
let mut old = val.load(Ordering::Relaxed);
loop {
let new = old * 2;
match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
Ok(_) => break,
Err(x) => old = x,
}
}
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(
all(debug_assertions, not(portable_atomic_no_track_caller)),
track_caller
)]
pub fn compare_exchange_weak(
&self,
current: $int_type,
new: $int_type,
success: Ordering,
failure: Ordering,
) -> Result<$int_type, $int_type> {
self.inner.compare_exchange_weak(current, new, success, failure)
}
}
doc_comment! {
concat!("Adds to the current value, returning the previous value.
This operation wraps around on overflow.
`fetch_add` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_add(val, order)
}
}
doc_comment! {
concat!("Adds to the current value.
This operation wraps around on overflow.
Unlike `fetch_add`, this does not return the previous value.
`add` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_add` on some platforms.
- MSP430: `add` instead of disabling interrupts (only {8,16}-bit atomics)
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0);
foo.add(10, Ordering::SeqCst);
assert_eq!(foo.load(Ordering::SeqCst), 10);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn add(&self, val: $int_type, order: Ordering) {
self.inner.add(val, order);
}
}
doc_comment! {
concat!("Subtracts from the current value, returning the previous value.
This operation wraps around on overflow.
`fetch_sub` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_sub(val, order)
}
}
doc_comment! {
concat!("Subtracts from the current value.
This operation wraps around on overflow.
Unlike `fetch_sub`, this does not return the previous value.
`sub` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_sub` on some platforms.
- MSP430: `sub` instead of disabling interrupts (only {8,16}-bit atomics)
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(20);
foo.sub(10, Ordering::SeqCst);
assert_eq!(foo.load(Ordering::SeqCst), 10);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn sub(&self, val: $int_type, order: Ordering) {
self.inner.sub(val, order);
}
}
doc_comment! {
concat!("Bitwise \"and\" with the current value.
Performs a bitwise \"and\" operation on the current value and the argument `val`, and
sets the new value to the result.
Returns the previous value.
`fetch_and` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_and(val, order)
}
}
doc_comment! {
concat!("Bitwise \"and\" with the current value.
Performs a bitwise \"and\" operation on the current value and the argument `val`, and
sets the new value to the result.
Unlike `fetch_and`, this does not return the previous value.
`and` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_and` on some platforms.
- x86: `lock and` instead of `cmpxchg` loop (only {8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)
- MSP430: `and` instead of disabling interrupts (only {8,16}-bit atomics)
Note: On x86, the use of either function should not usually
affect the generated code, because LLVM can properly optimize the case
where the result is unused.
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn and(&self, val: $int_type, order: Ordering) {
self.inner.and(val, order);
}
}
doc_comment! {
concat!("Bitwise \"nand\" with the current value.
Performs a bitwise \"nand\" operation on the current value and the argument `val`, and
sets the new value to the result.
Returns the previous value.
`fetch_nand` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_nand(val, order)
}
}
doc_comment! {
concat!("Bitwise \"or\" with the current value.
Performs a bitwise \"or\" operation on the current value and the argument `val`, and
sets the new value to the result.
Returns the previous value.
`fetch_or` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_or(val, order)
}
}
doc_comment! {
concat!("Bitwise \"or\" with the current value.
Performs a bitwise \"or\" operation on the current value and the argument `val`, and
sets the new value to the result.
Unlike `fetch_or`, this does not return the previous value.
`or` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_or` on some platforms.
- x86: `lock or` instead of `cmpxchg` loop (only {8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)
- MSP430: `or` instead of disabling interrupts (only {8,16}-bit atomics)
Note: On x86, the use of either function should not usually
affect the generated code, because LLVM can properly optimize the case
where the result is unused.
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn or(&self, val: $int_type, order: Ordering) {
self.inner.or(val, order);
}
}
doc_comment! {
concat!("Bitwise \"xor\" with the current value.
Performs a bitwise \"xor\" operation on the current value and the argument `val`, and
sets the new value to the result.
Returns the previous value.
`fetch_xor` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_xor(val, order)
}
}
doc_comment! {
concat!("Bitwise \"xor\" with the current value.
Performs a bitwise \"xor\" operation on the current value and the argument `val`, and
sets the new value to the result.
Unlike `fetch_xor`, this does not return the previous value.
`xor` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_xor` on some platforms.
- x86: `lock xor` instead of `cmpxchg` loop (only {8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)
- MSP430: `xor` instead of disabling interrupts (only {8,16}-bit atomics)
Note: On x86, the use of either function should not usually
affect the generated code, because LLVM can properly optimize the case
where the result is unused.
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0b101101);
foo.xor(0b110011, Ordering::SeqCst);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn xor(&self, val: $int_type, order: Ordering) {
self.inner.xor(val, order);
}
}
doc_comment! {
concat!("Fetches the value, and applies a function to it that returns an optional
new value. Returns a `Result` of `Ok(previous_value)` if the function returned `Some(_)`, else
`Err(previous_value)`.
Note: This may call the function multiple times if the value has been changed from other threads in
the meantime, as long as the function returns `Some(_)`, but the function will have been applied
only once to the stored value.
`fetch_update` takes two [`Ordering`] arguments to describe the memory ordering of this operation.
The first describes the required ordering for when the operation finally succeeds while the second
describes the required ordering for loads. These correspond to the success and failure orderings of
[`compare_exchange`](Self::compare_exchange) respectively.
Using [`Acquire`] as success ordering makes the store part
of this operation [`Relaxed`], and using [`Release`] makes the final successful load
[`Relaxed`]. The (failed) load ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
# Panics
Panics if `fetch_order` is [`Release`], [`AcqRel`].
# Considerations
This method is not magic; it is not provided by the hardware.
It is implemented in terms of [`compare_exchange_weak`](Self::compare_exchange_weak),
and suffers from the same drawbacks.
In particular, this method will not circumvent the [ABA Problem].
[ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
# Examples
```rust
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let x = ", stringify!($atomic_type), "::new(7);
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(
all(debug_assertions, not(portable_atomic_no_track_caller)),
track_caller
)]
pub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> Result<$int_type, $int_type>
where
F: FnMut($int_type) -> Option<$int_type>,
{
let mut prev = self.load(fetch_order);
while let Some(next) = f(prev) {
match self.compare_exchange_weak(prev, next, set_order, fetch_order) {
x @ Ok(_) => return x,
Err(next_prev) => prev = next_prev,
}
}
Err(prev)
}
}
doc_comment! {
concat!("Maximum with the current value.
Finds the maximum of the current value and the argument `val`, and
sets the new value to the result.
Returns the previous value.
`fetch_max` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);
```
If you want to obtain the maximum value in one step, you can use the following:
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_max(val, order)
}
}
doc_comment! {
concat!("Minimum with the current value.
Finds the minimum of the current value and the argument `val`, and
sets the new value to the result.
Returns the previous value.
`fetch_min` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);
```
If you want to obtain the minimum value in one step, you can use the following:
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type {
self.inner.fetch_min(val, order)
}
}
doc_comment! {
concat!("Logical negates the current value, and sets the new value to the result.
Returns the previous value.
`fetch_not` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0);
assert_eq!(foo.fetch_not(Ordering::Relaxed), 0);
assert_eq!(foo.load(Ordering::Relaxed), !0);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_not(&self, order: Ordering) -> $int_type {
self.inner.fetch_not(order)
}
doc_comment! {
concat!("Logical negates the current value, and sets the new value to the result.
Unlike `fetch_not`, this does not return the previous value.
`not` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_not` on some platforms.
- x86: `lock not` instead of `cmpxchg` loop (only {8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)
- MSP430: `inv` instead of disabling interrupts (only {8,16}-bit atomics)
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(0);
foo.not(Ordering::Relaxed);
assert_eq!(foo.load(Ordering::Relaxed), !0);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn not(&self, order: Ordering) {
self.inner.not(order);
}
}
}
}
};
(int,
$atomic_type:ident, $int_type:ident, $align:expr
) => {
atomic_int!(uint, $atomic_type, $int_type, $align);
impl $atomic_type {
doc_comment! {
concat!("Negates the current value, and sets the new value to the result.
Returns the previous value.
`fetch_neg` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(5);
assert_eq!(foo.fetch_neg(Ordering::Relaxed), 5);
assert_eq!(foo.load(Ordering::Relaxed), -5);
assert_eq!(foo.fetch_neg(Ordering::Relaxed), -5);
assert_eq!(foo.load(Ordering::Relaxed), 5);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_neg(&self, order: Ordering) -> $int_type {
self.inner.fetch_neg(order)
}
doc_comment! {
concat!("Negates the current value, and sets the new value to the result.
Unlike `fetch_neg`, this does not return the previous value.
`neg` takes an [`Ordering`] argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
[`Acquire`] makes the store part of this operation [`Relaxed`], and
using [`Release`] makes the load part [`Relaxed`].
This function may generate more efficient code than `fetch_neg` on some platforms.
- x86: `lock neg` instead of `cmpxchg` loop (only {8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)
# Examples
```
use portable_atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(5);
foo.neg(Ordering::Relaxed);
assert_eq!(foo.load(Ordering::Relaxed), -5);
foo.neg(Ordering::Relaxed);
assert_eq!(foo.load(Ordering::Relaxed), 5);
```"),
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn neg(&self, order: Ordering) {
self.inner.neg(order);
}
}
}
}
};
(float,
$atomic_type:ident, $float_type:ident, $atomic_int_type:ident, $int_type:ident, $align:expr
) => {
doc_comment! {
concat!("A floating point type which can be safely shared between threads.
This type has the same in-memory representation as the underlying floating point type,
[`", stringify!($float_type), "`].
"
),
#[cfg_attr(docsrs, doc(cfg(feature = "float")))]
#[repr(C, align($align))]
pub struct $atomic_type {
inner: imp::float::$atomic_type,
}
}
static_assert_layout!($atomic_type, $float_type);
impl Default for $atomic_type {
#[inline]
fn default() -> Self {
Self::new($float_type::default())
}
}
impl From<$float_type> for $atomic_type {
#[inline]
fn from(v: $float_type) -> Self {
Self::new(v)
}
}
impl fmt::Debug for $atomic_type {
#[allow(clippy::missing_inline_in_public_items)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.load(Ordering::Relaxed), f)
}
}
unsafe impl Sync for $atomic_type {}
#[cfg(not(portable_atomic_no_core_unwind_safe))]
impl core::panic::RefUnwindSafe for $atomic_type {}
#[cfg(all(portable_atomic_no_core_unwind_safe, feature = "std"))]
impl std::panic::RefUnwindSafe for $atomic_type {}
serde_impls!($atomic_type);
impl $atomic_type {
#[inline]
#[must_use]
pub const fn new(v: $float_type) -> Self {
Self { inner: imp::float::$atomic_type::new(v) }
}
#[inline]
#[must_use]
pub fn is_lock_free() -> bool {
<imp::float::$atomic_type>::is_lock_free()
}
#[inline]
#[must_use]
pub const fn is_always_lock_free() -> bool {
<imp::float::$atomic_type>::is_always_lock_free()
}
#[inline]
pub fn get_mut(&mut self) -> &mut $float_type {
self.inner.get_mut()
}
#[inline]
pub fn into_inner(self) -> $float_type {
self.inner.into_inner()
}
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn load(&self, order: Ordering) -> $float_type {
self.inner.load(order)
}
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn store(&self, val: $float_type, order: Ordering) {
self.inner.store(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn swap(&self, val: $float_type, order: Ordering) -> $float_type {
self.inner.swap(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn compare_exchange(
&self,
current: $float_type,
new: $float_type,
success: Ordering,
failure: Ordering,
) -> Result<$float_type, $float_type> {
self.inner.compare_exchange(current, new, success, failure)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(docsrs, doc(alias = "compare_and_swap"))]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn compare_exchange_weak(
&self,
current: $float_type,
new: $float_type,
success: Ordering,
failure: Ordering,
) -> Result<$float_type, $float_type> {
self.inner.compare_exchange_weak(current, new, success, failure)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_add(&self, val: $float_type, order: Ordering) -> $float_type {
self.inner.fetch_add(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_sub(&self, val: $float_type, order: Ordering) -> $float_type {
self.inner.fetch_sub(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> Result<$float_type, $float_type>
where
F: FnMut($float_type) -> Option<$float_type>,
{
let mut prev = self.load(fetch_order);
while let Some(next) = f(prev) {
match self.compare_exchange_weak(prev, next, set_order, fetch_order) {
x @ Ok(_) => return x,
Err(next_prev) => prev = next_prev,
}
}
Err(prev)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_max(&self, val: $float_type, order: Ordering) -> $float_type {
self.inner.fetch_max(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_min(&self, val: $float_type, order: Ordering) -> $float_type {
self.inner.fetch_min(val, order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_neg(&self, order: Ordering) -> $float_type {
self.inner.fetch_neg(order)
}
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[inline]
pub fn fetch_abs(&self, order: Ordering) -> $float_type {
self.inner.fetch_abs(order)
}
doc_comment! {
concat!("Raw transmutation to `", stringify!($atomic_int_type), "`.
See [`", stringify!($float_type) ,"::from_bits`] for some discussion of the
portability of this operation (there are almost no issues)."),
#[inline]
pub fn as_bits(&self) -> &crate::$atomic_int_type {
self.inner.as_bits()
}
}
}
};
}
#[cfg(target_pointer_width = "16")]
atomic_int!(AtomicIsize, isize, 2);
#[cfg(target_pointer_width = "16")]
atomic_int!(AtomicUsize, usize, 2);
#[cfg(target_pointer_width = "32")]
atomic_int!(AtomicIsize, isize, 4);
#[cfg(target_pointer_width = "32")]
atomic_int!(AtomicUsize, usize, 4);
#[cfg(target_pointer_width = "64")]
atomic_int!(AtomicIsize, isize, 8);
#[cfg(target_pointer_width = "64")]
atomic_int!(AtomicUsize, usize, 8);
#[cfg(target_pointer_width = "128")]
atomic_int!(AtomicIsize, isize, 16);
#[cfg(target_pointer_width = "128")]
atomic_int!(AtomicUsize, usize, 16);
atomic_int!(AtomicI8, i8, 1);
atomic_int!(AtomicU8, u8, 1);
atomic_int!(AtomicI16, i16, 2);
atomic_int!(AtomicU16, u16, 2);
#[cfg(any(not(target_pointer_width = "16"), feature = "fallback"))]
atomic_int!(AtomicI32, i32, 4);
#[cfg(any(not(target_pointer_width = "16"), feature = "fallback"))]
atomic_int!(AtomicU32, u32, 4);
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
all(
feature = "fallback",
any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
)
),
not(portable_atomic_no_atomic_64),
not(any(target_pointer_width = "16", target_pointer_width = "32")),
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
all(
feature = "fallback",
any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
)
),
target_has_atomic = "64",
not(any(target_pointer_width = "16", target_pointer_width = "32")),
))
)]
atomic_int!(AtomicI64, i64, 8);
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
all(
feature = "fallback",
any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
)
),
not(portable_atomic_no_atomic_64),
not(any(target_pointer_width = "16", target_pointer_width = "32")),
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(
all(
feature = "fallback",
any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
)
),
target_has_atomic = "64",
not(any(target_pointer_width = "16", target_pointer_width = "32")),
))
)]
atomic_int!(AtomicU64, u64, 8);
#[cfg_attr(
not(feature = "fallback"),
cfg(any(
all(
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
target_arch = "aarch64"
),
all(
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
any(
target_feature = "cmpxchg16b",
portable_atomic_target_feature = "cmpxchg16b",
portable_atomic_cmpxchg16b_dynamic
),
target_arch = "x86_64",
),
all(
portable_atomic_asm_experimental_arch,
any(
target_feature = "quadword-atomics",
portable_atomic_target_feature = "quadword-atomics"
),
target_arch = "powerpc64"
),
all(portable_atomic_asm_experimental_arch, target_arch = "s390x"),
))
)]
#[cfg_attr(
all(feature = "fallback", portable_atomic_no_cfg_target_has_atomic),
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
all(feature = "fallback", not(portable_atomic_no_cfg_target_has_atomic)),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
atomic_int!(AtomicI128, i128, 16);
#[cfg_attr(
not(feature = "fallback"),
cfg(any(
all(
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
target_arch = "aarch64"
),
all(
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
any(
target_feature = "cmpxchg16b",
portable_atomic_target_feature = "cmpxchg16b",
portable_atomic_cmpxchg16b_dynamic
),
target_arch = "x86_64",
),
all(
portable_atomic_asm_experimental_arch,
any(
target_feature = "quadword-atomics",
portable_atomic_target_feature = "quadword-atomics"
),
target_arch = "powerpc64"
),
all(portable_atomic_asm_experimental_arch, target_arch = "s390x"),
))
)]
#[cfg_attr(
all(feature = "fallback", portable_atomic_no_cfg_target_has_atomic),
cfg(any(
not(portable_atomic_no_atomic_cas),
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
#[cfg_attr(
all(feature = "fallback", not(portable_atomic_no_cfg_target_has_atomic)),
cfg(any(
target_has_atomic = "ptr",
portable_atomic_unsafe_assume_single_core,
feature = "critical-section",
target_arch = "avr",
target_arch = "msp430"
))
)]
atomic_int!(AtomicU128, u128, 16);