atomic_maybe_uninit

Struct AtomicMaybeUninit

Source
#[repr(C)]
pub struct AtomicMaybeUninit<T: Primitive> { /* private fields */ }
Expand description

A potentially uninitialized integer type which can be safely shared between threads.

This type has the same in-memory representation as the underlying value type, MaybeUninit<T>.

Implementations§

Source§

impl<T: Primitive> AtomicMaybeUninit<T>

Source

pub const fn new(v: MaybeUninit<T>) -> Self

Creates a new atomic value from a potentially uninitialized value.

This is const fn on Rust 1.61+. See also const_new function, which is always const fn.

§Examples
use std::mem::MaybeUninit;

use atomic_maybe_uninit::AtomicMaybeUninit;

let v = AtomicMaybeUninit::new(MaybeUninit::new(5_i32));

// Equivalent to:
let v = AtomicMaybeUninit::from(5_i32);
Source

pub const unsafe fn from_ptr<'a>(ptr: *mut MaybeUninit<T>) -> &'a Self

Creates a new reference to an atomic value from a pointer.

This is const fn on Rust 1.83+.

§Safety
  • ptr must be aligned to align_of::<AtomicMaybeUninit<T>>() (note that on some platforms this can be bigger than align_of::<MaybeUninit<T>>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • Non-atomic accesses to the value behind ptr must have a happens-before relationship with atomic accesses via the returned value (or vice-versa).
    • In other words, time periods where the value is accessed atomically may not overlap with periods where the value is accessed non-atomically.
    • This requirement is trivially satisfied if ptr is never used non-atomically for the duration of lifetime 'a. Most use cases should be able to follow this guideline.
    • This requirement is also trivially satisfied if all accesses (atomic or not) are done from the same thread.
  • This method must not be used to create overlapping or mixed-size atomic accesses, as these are not supported by the memory model.
Source

pub const fn get_mut(&mut self) -> &mut MaybeUninit<T>

Returns a mutable reference to the underlying value.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

This is const fn on Rust 1.83+.

§Examples
use std::mem::MaybeUninit;

use atomic_maybe_uninit::AtomicMaybeUninit;

let mut v = AtomicMaybeUninit::from(5_i32);
unsafe { assert_eq!((*v.get_mut()).assume_init(), 5) }
*v.get_mut() = MaybeUninit::new(10);
unsafe { assert_eq!((*v.get_mut()).assume_init(), 10) }
Source

pub const fn into_inner(self) -> MaybeUninit<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.

This is const fn on Rust 1.61+.

§Examples
use atomic_maybe_uninit::AtomicMaybeUninit;

let v = AtomicMaybeUninit::from(5_i32);
unsafe { assert_eq!(v.into_inner().assume_init(), 5) }
Source

pub fn load(&self, order: Ordering) -> MaybeUninit<T>
where T: AtomicLoad,

Loads a value from the atomic value.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

§Panics

Panics if order is Release or AcqRel.

§Examples
use std::sync::atomic::Ordering;

use atomic_maybe_uninit::AtomicMaybeUninit;

let v = AtomicMaybeUninit::from(5_i32);
unsafe { assert_eq!(v.load(Ordering::Relaxed).assume_init(), 5) }
Source

pub fn store(&self, val: MaybeUninit<T>, order: Ordering)
where T: AtomicStore,

Stores a value into the atomic value.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

§Panics

Panics if order is Acquire or AcqRel.

§Examples
use std::{mem::MaybeUninit, sync::atomic::Ordering};

use atomic_maybe_uninit::AtomicMaybeUninit;

let v = AtomicMaybeUninit::from(5_i32);
v.store(MaybeUninit::new(10), Ordering::Relaxed);
unsafe { assert_eq!(v.load(Ordering::Relaxed).assume_init(), 10) }
Source

pub fn swap(&self, val: MaybeUninit<T>, order: Ordering) -> MaybeUninit<T>
where T: AtomicSwap,

Stores a value into the atomic value, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

§Examples
use std::{mem::MaybeUninit, sync::atomic::Ordering};

use atomic_maybe_uninit::AtomicMaybeUninit;

let v = AtomicMaybeUninit::from(5_i32);
unsafe {
    assert_eq!(v.swap(MaybeUninit::new(10), Ordering::Relaxed).assume_init(), 5);
    assert_eq!(v.load(Ordering::Relaxed).assume_init(), 10);
}
Source

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

Stores a value into the atomic value if the current value is the same as the current value. Here, “the same” is determined using byte-wise equality, not PartialEq.

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.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

§Panics

Panics if failure is Release, AcqRel.

§Notes

Comparison of two values containing uninitialized bytes may fail even if they are equivalent as Rust’s type, because values can be byte-wise inequal even when they are equal as Rust values.

For example, the following example could be an infinite loop:

use std::{
    mem::{self, MaybeUninit},
    sync::atomic::Ordering,
};

use atomic_maybe_uninit::AtomicMaybeUninit;

#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C, align(4))]
struct Test(u8, u16);

unsafe {
    let x = mem::transmute::<Test, MaybeUninit<u32>>(Test(0, 0));
    let v = AtomicMaybeUninit::new(x);
    while v
        .compare_exchange(
            mem::transmute::<Test, MaybeUninit<u32>>(Test(0, 0)),
            mem::transmute::<Test, MaybeUninit<u32>>(Test(1, 0)),
            Ordering::AcqRel,
            Ordering::Acquire,
        )
        .is_err()
    {}
}

To work around this problem, you need to use a helper like the following.

// Adapted from https://github.com/crossbeam-rs/crossbeam/blob/crossbeam-utils-0.8.10/crossbeam-utils/src/atomic/atomic_cell.rs#L1081-L1110
unsafe fn atomic_compare_exchange(
    v: &AtomicMaybeUninit<u32>,
    mut current: Test,
    new: Test,
) -> Result<Test, Test> {
    let mut current_raw = mem::transmute::<Test, MaybeUninit<u32>>(current);
    let new_raw = mem::transmute::<Test, MaybeUninit<u32>>(new);
    loop {
        match v.compare_exchange_weak(current_raw, new_raw, Ordering::AcqRel, Ordering::Acquire)
        {
            Ok(_) => {
                // The values are byte-wise equal; for `Test` we know this implies they are `PartialEq`-equal.
                break Ok(current);
            }
            Err(previous_raw) => {
                let previous = mem::transmute::<MaybeUninit<u32>, Test>(previous_raw);

                if !Test::eq(&previous, &current) {
                    break Err(previous);
                }

                // The compare-exchange operation has failed and didn't store `new`. The
                // failure is either spurious, or `previous` was semantically equal to
                // `current` but not byte-equal. Let's retry with `previous` as the new
                // `current`.
                current = previous;
                current_raw = previous_raw;
            }
        }
    }
}

unsafe {
    let x = mem::transmute::<Test, MaybeUninit<u32>>(Test(0, 0));
    let v = AtomicMaybeUninit::new(x);
    while atomic_compare_exchange(&v, Test(0, 0), Test(1, 0)).is_err() {}
}

Also, Valgrind reports “Conditional jump or move depends on uninitialized value(s)” error if there is such a comparison – which is correct, that’s exactly what the implementation does, but we are doing this inside inline assembly so it should be fine. (Effectively we are adding partial freeze capabilities to Rust via inline assembly. This pattern has not been blessed by the language team, but is also not known to cause any problems.)

§Examples
use std::{mem::MaybeUninit, sync::atomic::Ordering};

use atomic_maybe_uninit::AtomicMaybeUninit;

unsafe {
    let v = AtomicMaybeUninit::from(5_i32);

    assert_eq!(
        v.compare_exchange(
            MaybeUninit::new(5),
            MaybeUninit::new(10),
            Ordering::Acquire,
            Ordering::Relaxed
        )
        .unwrap()
        .assume_init(),
        5
    );
    assert_eq!(v.load(Ordering::Relaxed).assume_init(), 10);

    assert_eq!(
        v.compare_exchange(
            MaybeUninit::new(6),
            MaybeUninit::new(12),
            Ordering::SeqCst,
            Ordering::Acquire
        )
        .unwrap_err()
        .assume_init(),
        10
    );
    assert_eq!(v.load(Ordering::Relaxed).assume_init(), 10);
}
Source

pub fn compare_exchange_weak( &self, current: MaybeUninit<T>, new: MaybeUninit<T>, success: Ordering, failure: Ordering, ) -> Result<MaybeUninit<T>, MaybeUninit<T>>

Stores a value into the atomic value if the current value is the same as the current value. Here, “the same” is determined using byte-wise equality, not PartialEq.

This function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

§Panics

Panics if failure is Release, AcqRel.

§Notes

Comparison of two values containing uninitialized bytes may fail even if they are equivalent as Rust’s type, because values can be byte-wise inequal even when they are equal as Rust values.

See compare_exchange for details.

§Examples
use std::{mem::MaybeUninit, sync::atomic::Ordering};

use atomic_maybe_uninit::AtomicMaybeUninit;

let v = AtomicMaybeUninit::from(5_i32);

unsafe {
    let mut old = v.load(Ordering::Relaxed);
    loop {
        let new = old.assume_init() * 2;
        match v.compare_exchange_weak(
            old,
            MaybeUninit::new(new),
            Ordering::SeqCst,
            Ordering::Relaxed,
        ) {
            Ok(_) => break,
            Err(x) => old = x,
        }
    }
}
Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<MaybeUninit<T>, MaybeUninit<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.

fetch_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

§Panics

Panics if fetch_order is Release, AcqRel.

§Considerations

This method is not magic; it is not provided by the hardware. It is implemented in terms of compare_exchange_weak, and suffers from the same drawbacks. In particular, this method will not circumvent the ABA Problem.

§Examples
use std::{mem::MaybeUninit, sync::atomic::Ordering};

use atomic_maybe_uninit::AtomicMaybeUninit;

unsafe {
    let v = AtomicMaybeUninit::from(5_i32);
    assert_eq!(
        v.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None).unwrap_err().assume_init(),
        5
    );
    assert_eq!(
        v.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(MaybeUninit::new(
            x.assume_init() + 1
        )))
        .unwrap()
        .assume_init(),
        5
    );
    assert_eq!(v.load(Ordering::SeqCst).assume_init(), 6);
}
Source

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

Returns a mutable pointer to the underlying value.

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.

This is const fn on Rust 1.61+.

Source§

impl AtomicMaybeUninit<i8>

Source

pub const fn const_new(v: MaybeUninit<i8>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<u8>

Source

pub const fn const_new(v: MaybeUninit<u8>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<i16>

Source

pub const fn const_new(v: MaybeUninit<i16>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<u16>

Source

pub const fn const_new(v: MaybeUninit<u16>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<i32>

Source

pub const fn const_new(v: MaybeUninit<i32>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<u32>

Source

pub const fn const_new(v: MaybeUninit<u32>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<i64>

Source

pub const fn const_new(v: MaybeUninit<i64>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<u64>

Source

pub const fn const_new(v: MaybeUninit<u64>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<i128>

Source

pub const fn const_new(v: MaybeUninit<i128>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<u128>

Source

pub const fn const_new(v: MaybeUninit<u128>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<isize>

Source

pub const fn const_new(v: MaybeUninit<isize>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Source§

impl AtomicMaybeUninit<usize>

Source

pub const fn const_new(v: MaybeUninit<usize>) -> Self

Creates a new atomic value from a potentially uninitialized value. Unlike new, this is always const fn.

Trait Implementations§

Source§

impl<T: Primitive> Debug for AtomicMaybeUninit<T>

Source§

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

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

impl<T: Primitive> From<MaybeUninit<T>> for AtomicMaybeUninit<T>

Source§

fn from(v: MaybeUninit<T>) -> Self

Creates a new atomic value from a potentially uninitialized value.

Source§

impl<T: Primitive> From<T> for AtomicMaybeUninit<T>

Source§

fn from(v: T) -> Self

Creates a new atomic value from an initialized value.

Source§

impl<T: Primitive> RefUnwindSafe for AtomicMaybeUninit<T>

Source§

impl<T: Primitive> Sync for AtomicMaybeUninit<T>

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.