Type Alias AtomicF64

Source
pub type AtomicF64 = AtomicCell<f64>;
Expand description

An atomic f64.

This is an alias for crossbeam::atomic::AtomicCell<f64>.

Note that there are no Ordering parameters, atomic loads use Acquire, and atomic stores use Release.

Aliased Type§

struct AtomicF64 { /* private fields */ }

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 8 bytes

Implementations

Source§

impl<T> AtomicCell<T>
where T: Default,

Source

pub fn take(&self) -> T

Takes the value of the atomic cell, leaving Default::default() in its place.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(5);
let five = a.take();

assert_eq!(five, 5);
assert_eq!(a.into_inner(), 0);
Source§

impl<T> AtomicCell<T>
where T: Copy,

Source

pub fn load(&self) -> T

Loads a value from the atomic cell.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(7);

assert_eq!(a.load(), 7);
Source§

impl<T> AtomicCell<T>
where T: Copy + Eq,

Source

pub fn compare_and_swap(&self, current: T, new: T) -> T

👎Deprecated: Use compare_exchange instead

If the current value equals current, stores new into the atomic cell.

The return value is always the previous value. If it is equal to current, then the value was updated.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(1);

assert_eq!(a.compare_and_swap(2, 3), 1);
assert_eq!(a.load(), 1);

assert_eq!(a.compare_and_swap(1, 2), 1);
assert_eq!(a.load(), 2);
Source

pub fn compare_exchange(&self, current: T, new: T) -> Result<T, T>

If the current value equals current, stores new into the atomic cell.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(1);

assert_eq!(a.compare_exchange(2, 3), Err(1));
assert_eq!(a.load(), 1);

assert_eq!(a.compare_exchange(1, 2), Ok(1));
assert_eq!(a.load(), 2);
Source

pub fn fetch_update<F>(&self, f: F) -> Result<T, T>
where F: FnMut(T) -> Option<T>,

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(7);
assert_eq!(a.fetch_update(|_| None), Err(7));
assert_eq!(a.fetch_update(|a| Some(a + 1)), Ok(7));
assert_eq!(a.fetch_update(|a| Some(a + 1)), Ok(8));
assert_eq!(a.load(), 9);
Source§

impl<T> AtomicCell<T>

Source

pub const fn new(val: T) -> AtomicCell<T>

Creates a new atomic cell initialized with val.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(7);
Source

pub fn into_inner(self) -> T

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(7);
let v = a.into_inner();

assert_eq!(v, 7);
Source

pub const fn is_lock_free() -> bool

Returns true if operations on values of this type are lock-free.

If the compiler or the platform doesn’t support the necessary atomic instructions, AtomicCell<T> will use global locks for every potentially concurrent atomic operation.

§Examples
use crossbeam_utils::atomic::AtomicCell;

// This type is internally represented as `AtomicUsize` so we can just use atomic
// operations provided by it.
assert_eq!(AtomicCell::<usize>::is_lock_free(), true);

// A wrapper struct around `isize`.
struct Foo {
    bar: isize,
}
// `AtomicCell<Foo>` will be internally represented as `AtomicIsize`.
assert_eq!(AtomicCell::<Foo>::is_lock_free(), true);

// Operations on zero-sized types are always lock-free.
assert_eq!(AtomicCell::<()>::is_lock_free(), true);

// Very large types cannot be represented as any of the standard atomic types, so atomic
// operations on them will have to use global locks for synchronization.
assert_eq!(AtomicCell::<[u8; 1000]>::is_lock_free(), false);
Source

pub fn store(&self, val: T)

Stores val into the atomic cell.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(7);

assert_eq!(a.load(), 7);
a.store(8);
assert_eq!(a.load(), 8);
Source

pub fn swap(&self, val: T) -> T

Stores val into the atomic cell and returns the previous value.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(7);

assert_eq!(a.load(), 7);
assert_eq!(a.swap(8), 7);
assert_eq!(a.load(), 8);
Source

pub fn as_ptr(&self) -> *mut T

Returns a raw pointer to the underlying data in this atomic cell.

§Examples
use crossbeam_utils::atomic::AtomicCell;

let a = AtomicCell::new(5);

let ptr = a.as_ptr();

Trait Implementations

Source§

impl<T> Debug for AtomicCell<T>
where T: Copy + Debug,

Source§

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

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

impl<T> Default for AtomicCell<T>
where T: Default,

Source§

fn default() -> AtomicCell<T>

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

impl<T> Drop for AtomicCell<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<T> for AtomicCell<T>

Source§

fn from(val: T) -> AtomicCell<T>

Converts to this type from the input type.
Source§

impl<T> RefUnwindSafe for AtomicCell<T>

Source§

impl<T> Send for AtomicCell<T>
where T: Send,

Source§

impl<T> Sync for AtomicCell<T>
where T: Send,

Source§

impl<T> UnwindSafe for AtomicCell<T>