pub struct AtomicCell<T> { /* private fields */ }
Expand description

A thread-safe mutable memory location.

This type is equivalent to Cell, except it can also be shared among multiple threads.

Operations on AtomicCells use atomic instructions whenever possible, and synchronize using global locks otherwise. You can call AtomicCell::<T>::is_lock_free() to check whether atomic instructions or locks will be used.

Implementations§

Creates a new atomic cell initialized with val.

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(7);

Returns a mutable reference to the inner value.

Examples
use crossbeam::atomic::AtomicCell;

let mut a = AtomicCell::new(7);
*a.get_mut() += 1;

assert_eq!(a.load(), 8);

Unwraps the atomic cell and returns its inner value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(v, 7);

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::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);

Stores val into the atomic cell.

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(7);

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

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

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(7);

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

Loads a value.

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(7);

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

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::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);

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::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);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Increments the current value by val and returns the previous value.

The addition wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);

Decrements the current value by val and returns the previous value.

The subtraction wraps on overflow.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);

Applies bitwise “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);

Applies bitwise “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_or(16), 7);
assert_eq!(a.load(), 23);

Applies bitwise “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

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

assert_eq!(a.fetch_xor(2), 7);
assert_eq!(a.load(), 5);

Applies logical “and” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(true);

assert_eq!(a.fetch_and(true), true);
assert_eq!(a.load(), true);

assert_eq!(a.fetch_and(false), true);
assert_eq!(a.load(), false);

Applies logical “or” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(false);

assert_eq!(a.fetch_or(false), false);
assert_eq!(a.load(), false);

assert_eq!(a.fetch_or(true), false);
assert_eq!(a.load(), true);

Applies logical “xor” to the current value and returns the previous value.

Examples
use crossbeam::atomic::AtomicCell;

let a = AtomicCell::new(true);

assert_eq!(a.fetch_xor(false), true);
assert_eq!(a.load(), true);

assert_eq!(a.fetch_xor(true), true);
assert_eq!(a.load(), false);

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.