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§
Sourceunsafe 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( 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 ofSelf
. (For example, ifSelf
isu128
,dst
must be aligned to 16-byte even if the alignment ofu128
is 8-byte.)success
must beSeqCst
,AcqRel
,Acquire
,Release
, orRelaxed
.failure
must beSeqCst
,Acquire
, orRelaxed
.
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§
Sourceunsafe fn atomic_compare_exchange_weak(
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)
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 ofSelf
. (For example, ifSelf
isu128
,dst
must be aligned to 16-byte even if the alignment ofu128
is 8-byte.)success
must beSeqCst
,AcqRel
,Acquire
,Release
, orRelaxed
.failure
must beSeqCst
,Acquire
, orRelaxed
.
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.