AtomicUsize

Struct AtomicUsize 

Source
#[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: AtomicUsize

Implementations§

Source§

impl AtomicUsize

Source

pub const fn new(v: usize) -> Self

Creates a new atomic integer.

§Examples
use keos::atomic::AtomicUsize;

let atomic_forty_two = AtomicUsize::new(42);
Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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());
}

Trait Implementations§

Source§

impl Debug for AtomicUsize

Source§

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

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

impl Default for AtomicUsize

Source§

fn default() -> AtomicUsize

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

impl From<usize> for AtomicUsize

Source§

fn from(v: usize) -> Self

Converts an usize into an AtomicUsize.

Auto Trait Implementations§

§

impl !Freeze for AtomicUsize

§

impl RefUnwindSafe for AtomicUsize

§

impl Send for AtomicUsize

§

impl Sync for AtomicUsize

§

impl Unpin for AtomicUsize

§

impl UnwindSafe for AtomicUsize

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.