atomic_maybe_uninit::raw

Trait AtomicCompareExchange

Source
pub trait AtomicCompareExchange: AtomicLoad + AtomicStore {
    // Required method
    unsafe fn atomic_compare_exchange(
        dst: *mut MaybeUninit<Self>,
        current: MaybeUninit<Self>,
        new: MaybeUninit<Self>,
        success: Ordering,
        failure: Ordering,
    ) -> (MaybeUninit<Self>, bool);

    // Provided method
    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.

Required 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 properly aligned to the size of Self. (For example, if Self is u128, dst must be aligned to 16-byte even if the alignment of u128 is 8-byte.)
  • success must be SeqCst, AcqRel, Acquire, Release, or Relaxed.
  • failure must be SeqCst, Acquire, or Relaxed.

The rules for the validity of the pointer follow the rules applied to functions exposed by the standard library’s ptr module, except that concurrent atomic operations on dst are allowed if the pointer go through UnsafeCell::get.

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

Provided Methods§

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 properly aligned to the size of Self. (For example, if Self is u128, dst must be aligned to 16-byte even if the alignment of u128 is 8-byte.)
  • success must be SeqCst, AcqRel, Acquire, Release, or Relaxed.
  • failure must be SeqCst, Acquire, or Relaxed.

The rules for the validity of the pointer follow the rules applied to functions exposed by the standard library’s ptr module, except that concurrent atomic operations on dst are allowed if the pointer go through UnsafeCell::get.

§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", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AtomicCompareExchange for i8

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for i16

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for i32

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for i64

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for isize

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for u8

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for u16

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for u32

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for u64

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Source§

impl AtomicCompareExchange for usize

Source§

unsafe fn atomic_compare_exchange( dst: *mut MaybeUninit<Self>, old: MaybeUninit<Self>, new: MaybeUninit<Self>, _success: Ordering, _failure: Ordering, ) -> (MaybeUninit<Self>, bool)

Implementors§