#[repr(transparent)]pub struct AtomicUsize(AtomicUsize);Expand description
An integer type which can be safely shared between threads.
This type has the same
size and bit validity
as the underlying integer type, [usize].
However, the alignment of this type is always equal to its size, even on targets where [usize] has a lesser alignment.
For more about the differences between atomic types and non-atomic types as well as information about the portability of this type, please see the [module-level documentation].
Note: This type is only available on platforms that support
atomic loads and stores of [usize].
Tuple Fields§
§0: AtomicUsizeImplementations§
Source§impl AtomicUsize
impl AtomicUsize
Sourcepub const fn new(v: usize) -> Self
pub const fn new(v: usize) -> Self
Creates a new atomic integer.
§Examples
use keos::atomic::AtomicUsize;
let atomic_forty_two = AtomicUsize::new(42);Sourcepub fn get_mut(&mut self) -> &mut usize
pub fn get_mut(&mut self) -> &mut usize
Returns a mutable reference to the underlying integer.
This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.
§Examples
use keos::atomic::AtomicUsize;
let mut some_var = AtomicUsize::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(), 5);Sourcepub const fn into_inner(self) -> usize
pub const fn into_inner(self) -> usize
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 keos::atomic::AtomicUsize;
let some_var = AtomicUsize::new(5);
assert_eq!(some_var.into_inner(), 5);Sourcepub fn load(&self) -> usize
pub fn load(&self) -> usize
Loads a value from the atomic integer.
§Examples
use keos::atomic::AtomicUsize;
let some_var = AtomicUsize::new(5);
assert_eq!(some_var.load(d), 5);Sourcepub fn store(&self, val: usize)
pub fn store(&self, val: usize)
Stores a value into the atomic integer.
§Examples
use keos::atomic::AtomicUsize;
let some_var = AtomicUsize::new(5);
some_var.store(10d);
assert_eq!(some_var.load(d), 10);Sourcepub fn swap(&self, val: usize) -> usize
pub fn swap(&self, val: usize) -> usize
Stores a value into the atomic integer, returning the previous value.
§Examples
use keos::atomic::AtomicUsize;
let some_var = AtomicUsize::new(5);
assert_eq!(some_var.swap(10d), 5);Sourcepub fn compare_exchange(
&self,
current: usize,
new: usize,
) -> Result<usize, usize>
pub fn compare_exchange( &self, current: usize, new: usize, ) -> Result<usize, usize>
Stores a value into the atomic integer if the current value is the same as
the current value.
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 keos::atomic::AtomicUsize;
let some_var = AtomicUsize::new(5);
assert_eq!(some_var.compare_exchange(5, 10, e), Ok(5));
assert_eq!(some_var.load(d), 10);
assert_eq!(some_var.compare_exchange(6, 12, e), Err(10));
assert_eq!(some_var.load(d), 10);Sourcepub fn fetch_add(&self, val: usize) -> usize
pub fn fetch_add(&self, val: usize) -> usize
Adds to the current value, returning the previous value.
This operation wraps around on overflow.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(0);
assert_eq!(foo.fetch_add(10), 0);
assert_eq!(foo.load(), 10);Sourcepub fn fetch_sub(&self, val: usize) -> usize
pub fn fetch_sub(&self, val: usize) -> usize
Subtracts from the current value, returning the previous value.
This operation wraps around on overflow.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(20);
assert_eq!(foo.fetch_sub(10), 20);
assert_eq!(foo.load(), 10);Sourcepub fn fetch_and(&self, val: usize) -> usize
pub fn fetch_and(&self, val: usize) -> usize
Bitwise “and” with the current value.
Performs a bitwise “and” operation on the current value and the argument val, and
sets the new value to the result.
Returns the previous value.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(0b101101);
assert_eq!(foo.fetch_and(0b110011), 0b101101);
assert_eq!(foo.load(), 0b100001);Sourcepub fn fetch_nand(&self, val: usize) -> usize
pub fn fetch_nand(&self, val: usize) -> usize
Bitwise “nand” with the current value.
Performs a bitwise “nand” operation on the current value and the argument val, and
sets the new value to the result.
Returns the previous value.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(0x13);
assert_eq!(foo.fetch_nand(0x31), 0x13);
assert_eq!(foo.load(), !(0x13 & 0x31));Sourcepub fn fetch_or(&self, val: usize) -> usize
pub fn fetch_or(&self, val: usize) -> usize
Bitwise “or” with the current value.
Performs a bitwise “or” operation on the current value and the argument val, and
sets the new value to the result.
Returns the previous value.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(0b101101);
assert_eq!(foo.fetch_or(0b110011), 0b101101);
assert_eq!(foo.load(), 0b111111);Sourcepub fn fetch_xor(&self, val: usize) -> usize
pub fn fetch_xor(&self, val: usize) -> usize
Bitwise “xor” with the current value.
Performs a bitwise “xor” operation on the current value and the argument val, and
sets the new value to the result.
Returns the previous value.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011), 0b101101);
assert_eq!(foo.load(), 0b011110);Sourcepub fn fetch_update<F>(&self, f: F) -> Result<usize, usize>where
F: FnMut(usize) -> Option<usize>,
pub fn fetch_update<F>(&self, f: F) -> Result<usize, usize>where
F: FnMut(usize) -> Option<usize>,
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 keos::atomic::AtomicUsize;
let x = AtomicUsize::new(7);
assert_eq!(x.fetch_update(|_| None), Err(7));
assert_eq!(x.fetch_update(|x| Some(x + 1)), Ok(7));
assert_eq!(x.fetch_update(|x| Some(x + 1)), Ok(8));
assert_eq!(x.load(), 9);Sourcepub fn fetch_max(&self, val: usize) -> usize
pub fn fetch_max(&self, val: usize) -> usize
Maximum with the current value.
Finds the maximum of the current value and the argument val, and
sets the new value to the result.
Returns the previous value.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(23);
assert_eq!(foo.fetch_max(42), 23);
assert_eq!(foo.load(), 42);If you want to obtain the maximum value in one step, you can use the following:
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar).max(bar);
assert!(max_foo == 42);Sourcepub fn fetch_min(&self, val: usize) -> usize
pub fn fetch_min(&self, val: usize) -> usize
Minimum with the current value.
Finds the minimum of the current value and the argument val, and
sets the new value to the result.
Returns the previous value.
§Examples
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(23);
assert_eq!(foo.fetch_min(42d), 23);
assert_eq!(foo.load(d), 23);
assert_eq!(foo.fetch_min(22d), 23);
assert_eq!(foo.load(d), 22);If you want to obtain the minimum value in one step, you can use the following:
use keos::atomic::AtomicUsize;
let foo = AtomicUsize::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar).min(bar);
assert_eq!(min_foo, 12);Sourcepub const fn as_ptr(&self) -> *mut usize
pub const fn as_ptr(&self) -> *mut usize
Returns a mutable pointer to the underlying integer.
Doing non-atomic reads and writes on the resulting integer can be a data race.
This method is mostly useful for FFI, where the function signature may use
*mut usize instead of &AtomicUsize.
Returning an *mut pointer from a shared reference to this atomic is safe because the
atomic types work with interior mutability. All modifications of an atomic change the value
through a shared reference, and can do so safely as long as they use atomic operations. Any
use of the returned raw pointer requires an unsafe block and still has to uphold the same
restriction: operations on it must be atomic.
§Examples
use keos::atomic::AtomicUsize;
extern "C" {
fn my_atomic_op(arg: *mut usize);
}
let atomic = AtomicUsize::new(1);
// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
my_atomic_op(atomic.as_ptr());
}