Struct crossbeam_utils::atomic::AtomicCell
source · [−]#[repr(transparent)]pub struct AtomicCell<T: ?Sized> { /* 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.
Atomic loads use the Acquire ordering and atomic stores use the Release ordering.
Implementations
sourceimpl<T> AtomicCell<T>
impl<T> AtomicCell<T>
sourcepub const fn new(val: T) -> AtomicCell<T>
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);sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwraps the atomic cell and returns its inner value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let mut a = AtomicCell::new(7);
let v = a.into_inner();
assert_eq!(v, 7);sourcepub fn is_lock_free() -> bool
pub 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);sourceimpl<T: ?Sized> AtomicCell<T>
impl<T: ?Sized> AtomicCell<T>
sourceimpl<T: Default> AtomicCell<T>
impl<T: Default> AtomicCell<T>
sourceimpl<T: Copy> AtomicCell<T>
impl<T: Copy> AtomicCell<T>
sourceimpl<T: Copy + Eq> AtomicCell<T>
impl<T: Copy + Eq> AtomicCell<T>
sourcepub fn compare_and_swap(&self, current: T, new: T) -> T
pub fn compare_and_swap(&self, current: T, new: T) -> T
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);sourcepub fn compare_exchange(&self, current: T, new: T) -> Result<T, T>
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);sourceimpl AtomicCell<u8>
impl AtomicCell<u8>
sourcepub fn fetch_add(&self, val: u8) -> u8
pub fn fetch_add(&self, val: u8) -> u8
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u8);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: u8) -> u8
pub fn fetch_sub(&self, val: u8) -> u8
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u8);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: u8) -> u8
pub fn fetch_and(&self, val: u8) -> u8
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u8);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<i8>
impl AtomicCell<i8>
sourcepub fn fetch_add(&self, val: i8) -> i8
pub fn fetch_add(&self, val: i8) -> i8
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i8);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: i8) -> i8
pub fn fetch_sub(&self, val: i8) -> i8
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i8);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: i8) -> i8
pub fn fetch_and(&self, val: i8) -> i8
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i8);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<u16>
impl AtomicCell<u16>
sourcepub fn fetch_add(&self, val: u16) -> u16
pub fn fetch_add(&self, val: u16) -> u16
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u16);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: u16) -> u16
pub fn fetch_sub(&self, val: u16) -> u16
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u16);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: u16) -> u16
pub fn fetch_and(&self, val: u16) -> u16
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u16);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<i16>
impl AtomicCell<i16>
sourcepub fn fetch_add(&self, val: i16) -> i16
pub fn fetch_add(&self, val: i16) -> i16
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i16);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: i16) -> i16
pub fn fetch_sub(&self, val: i16) -> i16
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i16);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: i16) -> i16
pub fn fetch_and(&self, val: i16) -> i16
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i16);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<u32>
impl AtomicCell<u32>
sourcepub fn fetch_add(&self, val: u32) -> u32
pub fn fetch_add(&self, val: u32) -> u32
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u32);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: u32) -> u32
pub fn fetch_sub(&self, val: u32) -> u32
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u32);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: u32) -> u32
pub fn fetch_and(&self, val: u32) -> u32
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u32);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<i32>
impl AtomicCell<i32>
sourcepub fn fetch_add(&self, val: i32) -> i32
pub fn fetch_add(&self, val: i32) -> i32
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i32);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: i32) -> i32
pub fn fetch_sub(&self, val: i32) -> i32
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i32);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: i32) -> i32
pub fn fetch_and(&self, val: i32) -> i32
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i32);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<u64>
impl AtomicCell<u64>
sourcepub fn fetch_add(&self, val: u64) -> u64
pub fn fetch_add(&self, val: u64) -> u64
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u64);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: u64) -> u64
pub fn fetch_sub(&self, val: u64) -> u64
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u64);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: u64) -> u64
pub fn fetch_and(&self, val: u64) -> u64
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7u64);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<i64>
impl AtomicCell<i64>
sourcepub fn fetch_add(&self, val: i64) -> i64
pub fn fetch_add(&self, val: i64) -> i64
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i64);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: i64) -> i64
pub fn fetch_sub(&self, val: i64) -> i64
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i64);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: i64) -> i64
pub fn fetch_and(&self, val: i64) -> i64
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7i64);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<usize>
impl AtomicCell<usize>
sourcepub fn fetch_add(&self, val: usize) -> usize
pub fn fetch_add(&self, val: usize) -> usize
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7usize);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: usize) -> usize
pub fn fetch_sub(&self, val: usize) -> usize
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7usize);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: usize) -> usize
pub fn fetch_and(&self, val: usize) -> usize
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7usize);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<isize>
impl AtomicCell<isize>
sourcepub fn fetch_add(&self, val: isize) -> isize
pub fn fetch_add(&self, val: isize) -> isize
Increments the current value by val and returns the previous value.
The addition wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7isize);
assert_eq!(a.fetch_add(3), 7);
assert_eq!(a.load(), 10);sourcepub fn fetch_sub(&self, val: isize) -> isize
pub fn fetch_sub(&self, val: isize) -> isize
Decrements the current value by val and returns the previous value.
The subtraction wraps on overflow.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7isize);
assert_eq!(a.fetch_sub(3), 7);
assert_eq!(a.load(), 4);sourcepub fn fetch_and(&self, val: isize) -> isize
pub fn fetch_and(&self, val: isize) -> isize
Applies bitwise “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::atomic::AtomicCell;
let a = AtomicCell::new(7isize);
assert_eq!(a.fetch_and(3), 7);
assert_eq!(a.load(), 3);sourceimpl AtomicCell<bool>
impl AtomicCell<bool>
sourcepub fn fetch_and(&self, val: bool) -> bool
pub fn fetch_and(&self, val: bool) -> bool
Applies logical “and” to the current value and returns the previous value.
Examples
use crossbeam_utils::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);sourcepub fn fetch_or(&self, val: bool) -> bool
pub fn fetch_or(&self, val: bool) -> bool
Applies logical “or” to the current value and returns the previous value.
Examples
use crossbeam_utils::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);sourcepub fn fetch_xor(&self, val: bool) -> bool
pub fn fetch_xor(&self, val: bool) -> bool
Applies logical “xor” to the current value and returns the previous value.
Examples
use crossbeam_utils::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
sourceimpl<T: Copy + Debug> Debug for AtomicCell<T>
impl<T: Copy + Debug> Debug for AtomicCell<T>
sourceimpl<T: Default> Default for AtomicCell<T>
impl<T: Default> Default for AtomicCell<T>
sourcefn default() -> AtomicCell<T>
fn default() -> AtomicCell<T>
Returns the “default value” for a type. Read more
impl<T> RefUnwindSafe for AtomicCell<T>
impl<T: Send> Send for AtomicCell<T>
impl<T: Send> Sync for AtomicCell<T>
impl<T> UnwindSafe for AtomicCell<T>
Auto Trait Implementations
impl<T: ?Sized> Unpin for AtomicCell<T> where
T: Unpin,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more