AtomicF64

Struct AtomicF64 

Source
pub struct AtomicF64 { /* private fields */ }
Expand description

Atomic 64-bit floating point number.

Provides easy-to-use atomic operations with automatic memory ordering selection. Implemented using AtomicU64 with bit conversion.

§Memory Ordering Strategy

This type uses the same memory ordering strategy as atomic integers:

  • Read operations (load): Use Acquire ordering to ensure visibility of prior writes from other threads.

  • Write operations (store): Use Release ordering to ensure visibility of prior writes to other threads.

  • Read-Modify-Write operations (swap, compare_set): Use AcqRel ordering for full synchronization.

  • CAS-based arithmetic (fetch_add, fetch_sub, etc.): Use AcqRel on success and Acquire on failure within the CAS loop. The loop ensures eventual consistency.

§Implementation Details

Since hardware doesn’t provide native atomic floating-point operations, this type is implemented using AtomicU64 with f64::to_bits() and f64::from_bits() conversions. This preserves bit patterns exactly, including special values like NaN and infinity.

§Features

  • Automatic memory ordering selection
  • Arithmetic operations via CAS loops
  • Zero-cost abstraction with inline methods
  • Access to underlying type via inner() for advanced use cases

§Limitations

  • Arithmetic operations use CAS loops (slower than integer operations)
  • NaN values may cause unexpected behavior in CAS operations
  • No max/min operations (complex floating point semantics)

§Example

use prism3_rust_concurrent::atomic::AtomicF64;

let atomic = AtomicF64::new(3.14159);
atomic.add(1.0);
assert_eq!(atomic.load(), 4.14159);

§Author

Haixing Hu

Implementations§

Source§

impl AtomicF64

Source

pub fn new(value: f64) -> Self

Creates a new atomic floating point number.

§Parameters
  • value - The initial value.
§Example
use prism3_rust_concurrent::atomic::AtomicF64;

let atomic = AtomicF64::new(3.14159);
assert_eq!(atomic.load(), 3.14159);
Source

pub fn load(&self) -> f64

Gets the current value.

§Memory Ordering

Uses Acquire ordering on the underlying AtomicU64. This ensures that all writes from other threads that happened before a Release store are visible after this load.

§Returns

The current value.

Source

pub fn store(&self, value: f64)

Sets a new value.

§Memory Ordering

Uses Release ordering on the underlying AtomicU64. This ensures that all prior writes in this thread are visible to other threads that perform an Acquire load.

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

pub fn swap(&self, value: f64) -> f64

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

§Memory Ordering

Uses AcqRel ordering on the underlying AtomicU64. This provides full synchronization for this read-modify-write operation.

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

The old value.

Source

pub fn compare_set(&self, current: f64, new: f64) -> Result<(), f64>

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.

§Memory Ordering
  • Success: Uses AcqRel ordering on the underlying AtomicU64 to ensure full synchronization when the exchange succeeds.
  • Failure: Uses Acquire ordering to observe the actual value written by another thread.
§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

pub fn compare_set_weak(&self, current: f64, new: f64) -> Result<(), f64>

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

pub fn compare_and_exchange(&self, current: f64, new: f64) -> f64

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.

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

pub fn compare_and_exchange_weak(&self, current: f64, new: f64) -> f64

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

pub fn fetch_add(&self, delta: f64) -> f64

Atomically adds a value, returning the old value.

§Memory Ordering

Internally uses a CAS loop with compare_set_weak, which uses AcqRel on success and Acquire on failure. The loop ensures eventual consistency even under high contention.

§Performance

May be slow in high-contention scenarios due to the CAS loop. Consider using atomic integers if performance is critical.

§Parameters
  • delta - The value to add.
§Returns

The old value before adding.

§Example
use prism3_rust_concurrent::atomic::AtomicF64;

let atomic = AtomicF64::new(10.0);
let old = atomic.fetch_add(5.5);
assert_eq!(old, 10.0);
assert_eq!(atomic.load(), 15.5);
Source

pub fn fetch_sub(&self, delta: f64) -> f64

Atomically subtracts a value, returning the old value.

§Memory Ordering

Internally uses a CAS loop with compare_set_weak, which uses AcqRel on success and Acquire on failure. The loop ensures eventual consistency even under high contention.

§Parameters
  • delta - The value to subtract.
§Returns

The old value before subtracting.

§Example
use prism3_rust_concurrent::atomic::AtomicF64;

let atomic = AtomicF64::new(10.0);
let old = atomic.fetch_sub(3.5);
assert_eq!(old, 10.0);
assert_eq!(atomic.load(), 6.5);
Source

pub fn fetch_mul(&self, factor: f64) -> f64

Atomically multiplies by a factor, returning the old value.

§Memory Ordering

Internally uses a CAS loop with compare_set_weak, which uses AcqRel on success and Acquire on failure. The loop ensures eventual consistency even under high contention.

§Parameters
  • factor - The factor to multiply by.
§Returns

The old value before multiplying.

§Example
use prism3_rust_concurrent::atomic::AtomicF64;

let atomic = AtomicF64::new(10.0);
let old = atomic.fetch_mul(2.5);
assert_eq!(old, 10.0);
assert_eq!(atomic.load(), 25.0);
Source

pub fn fetch_div(&self, divisor: f64) -> f64

Atomically divides by a divisor, returning the old value.

§Memory Ordering

Internally uses a CAS loop with compare_set_weak, which uses AcqRel on success and Acquire on failure. The loop ensures eventual consistency even under high contention.

§Parameters
  • divisor - The divisor to divide by.
§Returns

The old value before dividing.

§Example
use prism3_rust_concurrent::atomic::AtomicF64;

let atomic = AtomicF64::new(10.0);
let old = atomic.fetch_div(2.0);
assert_eq!(old, 10.0);
assert_eq!(atomic.load(), 5.0);
Source

pub fn fetch_update<F>(&self, f: F) -> f64
where F: Fn(f64) -> f64,

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

§Memory Ordering

Internally uses a CAS loop with compare_set_weak, which uses AcqRel on success and Acquire on failure. The loop ensures eventual consistency even under high contention.

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

The old value before the update.

Source

pub fn inner(&self) -> &AtomicU64

Gets a reference to the underlying standard library atomic type.

This allows direct access to the standard library’s atomic operations for advanced use cases that require fine-grained control over memory ordering.

§Memory Ordering

When using the returned reference, you have full control over memory ordering. Remember to use f64::to_bits() and f64::from_bits() for conversions.

§Returns

A reference to the underlying std::sync::atomic::AtomicU64.

Trait Implementations§

Source§

impl Atomic for AtomicF64

Source§

type Value = f64

The value type stored in the atomic.
Source§

fn load(&self) -> f64

Loads the current value. Read more
Source§

fn store(&self, value: f64)

Stores a new value. Read more
Source§

fn swap(&self, value: f64) -> f64

Swaps the current value with a new value, returning the old value. Read more
Source§

fn compare_set(&self, current: f64, new: f64) -> Result<(), f64>

Compares and sets the value atomically. Read more
Source§

fn compare_set_weak(&self, current: f64, new: f64) -> Result<(), f64>

Weak version of compare-and-set. Read more
Source§

fn compare_exchange(&self, current: f64, new: f64) -> f64

Compares and exchanges the value atomically, returning the previous value. Read more
Source§

fn compare_exchange_weak(&self, current: f64, new: f64) -> f64

Weak version of compare-and-exchange. Read more
Source§

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

Updates the value using a function, returning the old value. Read more
Source§

impl AtomicNumber for AtomicF64

Source§

fn fetch_add(&self, delta: f64) -> f64

Adds a delta to the value, returning the old value. Read more
Source§

fn fetch_sub(&self, delta: f64) -> f64

Subtracts a delta from the value, returning the old value. Read more
Source§

fn fetch_mul(&self, factor: f64) -> f64

Multiplies the value by a factor, returning the old value. Read more
Source§

fn fetch_div(&self, divisor: f64) -> f64

Divides the value by a divisor, returning the old value. Read more
Source§

impl Debug for AtomicF64

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AtomicF64

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for AtomicF64

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<f64> for AtomicF64

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl Send for AtomicF64

Source§

impl Sync for AtomicF64

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.