Skip to main content

AtomicCompareExchange

Trait AtomicCompareExchange 

Source
pub trait AtomicCompareExchange: AtomicLoad + AtomicStore {
    // Provided methods
    unsafe fn atomic_compare_exchange(
        dst: *mut MaybeUninit<Self>,
        current: MaybeUninit<Self>,
        new: MaybeUninit<Self>,
        success: Ordering,
        failure: Ordering,
    ) -> (MaybeUninit<Self>, bool) { ... }
    unsafe fn atomic_compare_exchange_weak(
        dst: *mut MaybeUninit<Self>,
        current: MaybeUninit<Self>,
        new: MaybeUninit<Self>,
        success: Ordering,
        failure: Ordering,
    ) -> (MaybeUninit<Self>, bool) { ... }
}
Expand description

Atomic compare and exchange.

This trait is sealed and cannot be implemented for types outside of atomic-maybe-uninit.

Provided Methods§

Source

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, current: MaybeUninit<Self>, new: MaybeUninit<Self>, success: Ordering, failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Stores a value into dst if the current value is the same as the current value. Here, “the same” is determined using byte-wise equality, not PartialEq.

The return value is a tuple of the previous value and the result indicating whether the new value was written and containing the previous value. On success, the returned value is guaranteed to be equal to the value at current.

atomic_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.

§Safety

Behavior is undefined if any of the following conditions are violated:

  • dst must be valid for both reads and writes.
  • dst must be aligned to size_of::<MaybeUninit<T>>() (note that on some platforms this can be bigger than align_of::<MaybeUninit<T>>()).
  • success must be SeqCst, AcqRel, Acquire, Release, or Relaxed.
  • failure must be SeqCst, Acquire, or Relaxed.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
§Notes

Comparison of two values containing uninitialized bytes may fail even if they are equivalent as Rust’s type, because values can be byte-wise inequal even when they are equal as Rust values.

See AtomicMaybeUninit::compare_exchange for details.

Source

unsafe fn atomic_compare_exchange_weak( dst: *mut MaybeUninit<Self>, current: MaybeUninit<Self>, new: MaybeUninit<Self>, success: Ordering, failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Stores a value into dst if the current value is the same as the current value. Here, “the same” is determined using byte-wise equality, not PartialEq.

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 tuple of the previous value and the result indicating whether the new value was written and containing the previous value.

atomic_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.

§Safety

Behavior is undefined if any of the following conditions are violated:

  • dst must be valid for both reads and writes.
  • dst must be aligned to size_of::<MaybeUninit<T>>() (note that on some platforms this can be bigger than align_of::<MaybeUninit<T>>()).
  • success must be SeqCst, AcqRel, Acquire, Release, or Relaxed.
  • failure must be SeqCst, Acquire, or Relaxed.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
§Notes

Comparison of two values containing uninitialized bytes may fail even if they are equivalent as Rust’s type, because values can be byte-wise inequal even when they are equal as Rust values.

See AtomicMaybeUninit::compare_exchange for details.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl AtomicCompareExchange for i8

Source§

impl AtomicCompareExchange for i16

Source§

impl AtomicCompareExchange for i32

Source§

impl AtomicCompareExchange for i64

Source§

impl AtomicCompareExchange for i128

Source§

impl AtomicCompareExchange for isize

Source§

impl AtomicCompareExchange for u8

Source§

impl AtomicCompareExchange for u16

Source§

impl AtomicCompareExchange for u32

Source§

impl AtomicCompareExchange for u64

Source§

impl AtomicCompareExchange for u128

Source§

impl AtomicCompareExchange for usize

Implementors§