Atomic

Trait Atomic 

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

Source

type Value

The value type stored in the atomic.

Required Methods§

Source

fn load(&self) -> Self::Value

Loads the current value.

Uses Acquire ordering by default.

§Returns

The current value.

Source

fn store(&self, value: Self::Value)

Stores a new value.

Uses Release ordering by default.

§Parameters
  • value - The new value to store.
Source

fn swap(&self, value: Self::Value) -> Self::Value

Swaps the current value with a new value, returning the old value.

Uses AcqRel ordering by default.

§Parameters
  • value - The new value to swap in.
§Returns

The old value.

Source

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.

Source

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.

Source

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.

Source

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.

Source

fn fetch_update<F>(&self, f: F) -> Self::Value
where F: Fn(Self::Value) -> Self::Value,

Updates the value using a function, returning the old value.

Internally uses a CAS loop until the update succeeds.

§Parameters
  • f - A function that takes the current value and returns the new value.
§Returns

The old value before the update.

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.

Implementors§