pub trait Atomic {
type Value;
// Required methods
fn load(&self) -> Self::Value;
fn store(&self, value: Self::Value);
fn swap(&self, value: Self::Value) -> Self::Value;
fn compare_set(
&self,
current: Self::Value,
new: Self::Value,
) -> Result<(), Self::Value>;
fn compare_set_weak(
&self,
current: Self::Value,
new: Self::Value,
) -> Result<(), Self::Value>;
fn compare_exchange(
&self,
current: Self::Value,
new: Self::Value,
) -> Self::Value;
fn compare_exchange_weak(
&self,
current: Self::Value,
new: Self::Value,
) -> Self::Value;
fn fetch_update<F>(&self, f: F) -> Self::Value
where F: Fn(Self::Value) -> Self::Value;
}Expand description
Common trait for all atomic types.
Provides basic atomic operations including load, store, swap, compare-and-set, and functional updates.
§Author
Haixing Hu
Required Associated Types§
Required Methods§
Sourcefn compare_set(
&self,
current: Self::Value,
new: Self::Value,
) -> Result<(), Self::Value>
fn compare_set( &self, current: Self::Value, new: Self::Value, ) -> Result<(), Self::Value>
Compares and sets the value atomically.
If the current value equals current, sets it to new and
returns Ok(()). Otherwise, returns Err(actual) where
actual is the current value.
Uses AcqRel ordering on success and Acquire ordering on
failure.
§Parameters
current- The expected current value.new- The new value to set if current matches.
§Returns
Ok(()) on success, or Err(actual) on failure where
actual is the real current value.
Sourcefn compare_set_weak(
&self,
current: Self::Value,
new: Self::Value,
) -> Result<(), Self::Value>
fn compare_set_weak( &self, current: Self::Value, new: Self::Value, ) -> Result<(), Self::Value>
Weak version of compare-and-set.
May spuriously fail even when the comparison succeeds. Should be used in a loop.
Uses AcqRel ordering on success and Acquire ordering on
failure.
§Parameters
current- The expected current value.new- The new value to set if current matches.
§Returns
Ok(()) on success, or Err(actual) on failure.
Sourcefn compare_exchange(
&self,
current: Self::Value,
new: Self::Value,
) -> Self::Value
fn compare_exchange( &self, current: Self::Value, new: Self::Value, ) -> Self::Value
Compares and exchanges the value atomically, returning the previous value.
If the current value equals current, sets it to new and
returns the old value. Otherwise, returns the actual current
value.
This is similar to compare_set but always returns the actual
value instead of a Result, which can be more convenient in CAS
loops.
Uses AcqRel ordering on success and Acquire ordering on
failure.
§Parameters
current- The expected current value.new- The new value to set if current matches.
§Returns
The value before the operation. If it equals current, the
operation succeeded.
Sourcefn compare_exchange_weak(
&self,
current: Self::Value,
new: Self::Value,
) -> Self::Value
fn compare_exchange_weak( &self, current: Self::Value, new: Self::Value, ) -> Self::Value
Weak version of compare-and-exchange.
May spuriously fail even when the comparison succeeds. Should be used in a loop.
Uses AcqRel ordering on success and Acquire ordering on
failure.
§Parameters
current- The expected current value.new- The new value to set if current matches.
§Returns
The value before the operation.
Sourcefn fetch_update<F>(&self, f: F) -> Self::Value
fn fetch_update<F>(&self, f: F) -> Self::Value
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.