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>
impl<T: Primitive> AtomicMaybeUninit<T>
Sourcepub const fn new(v: MaybeUninit<T>) -> Self
pub const fn new(v: MaybeUninit<T>) -> Self
Creates a new atomic value from a potentially uninitialized value.
§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);Sourcepub const unsafe fn from_ptr<'a>(ptr: *mut MaybeUninit<T>) -> &'a Self
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
ptrmust be aligned toalign_of::<AtomicMaybeUninit<T>>()(note that on some platforms this can be bigger thanalign_of::<MaybeUninit<T>>()).ptrmust be valid for both reads and writes for the whole lifetime'a.- Non-atomic accesses to the value behind
ptrmust 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
ptris 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.
Sourcepub const fn get_mut(&mut self) -> &mut MaybeUninit<T>
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) }Sourcepub const fn into_inner(self) -> MaybeUninit<T>
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.
§Examples
use atomic_maybe_uninit::AtomicMaybeUninit;
let v = AtomicMaybeUninit::from(5_i32);
unsafe { assert_eq!(v.into_inner().assume_init(), 5) }Sourcepub fn load(&self, order: Ordering) -> MaybeUninit<T>where
T: AtomicLoad,
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) }Sourcepub fn store(&self, val: MaybeUninit<T>, order: Ordering)where
T: AtomicStore,
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) }Sourcepub fn swap(&self, val: MaybeUninit<T>, order: Ordering) -> MaybeUninit<T>where
T: AtomicSwap,
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);
}Sourcepub fn compare_exchange(
&self,
current: MaybeUninit<T>,
new: MaybeUninit<T>,
success: Ordering,
failure: Ordering,
) -> Result<MaybeUninit<T>, MaybeUninit<T>>where
T: AtomicCompareExchange,
pub fn compare_exchange(
&self,
current: MaybeUninit<T>,
new: MaybeUninit<T>,
success: Ordering,
failure: Ordering,
) -> Result<MaybeUninit<T>, MaybeUninit<T>>where
T: AtomicCompareExchange,
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, ¤t) {
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);
}Sourcepub fn compare_exchange_weak(
&self,
current: MaybeUninit<T>,
new: MaybeUninit<T>,
success: Ordering,
failure: Ordering,
) -> Result<MaybeUninit<T>, MaybeUninit<T>>where
T: AtomicCompareExchange,
pub fn compare_exchange_weak(
&self,
current: MaybeUninit<T>,
new: MaybeUninit<T>,
success: Ordering,
failure: Ordering,
) -> Result<MaybeUninit<T>, MaybeUninit<T>>where
T: AtomicCompareExchange,
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,
}
}
}Sourcepub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<MaybeUninit<T>, MaybeUninit<T>>
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);
}Sourcepub const fn as_ptr(&self) -> *mut MaybeUninit<T>
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.
Source§impl AtomicMaybeUninit<i8>
impl AtomicMaybeUninit<i8>
Sourcepub const fn const_new(v: MaybeUninit<i8>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<i8>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<u8>
impl AtomicMaybeUninit<u8>
Sourcepub const fn const_new(v: MaybeUninit<u8>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<u8>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<i16>
impl AtomicMaybeUninit<i16>
Sourcepub const fn const_new(v: MaybeUninit<i16>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<i16>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<u16>
impl AtomicMaybeUninit<u16>
Sourcepub const fn const_new(v: MaybeUninit<u16>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<u16>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<i32>
impl AtomicMaybeUninit<i32>
Sourcepub const fn const_new(v: MaybeUninit<i32>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<i32>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<u32>
impl AtomicMaybeUninit<u32>
Sourcepub const fn const_new(v: MaybeUninit<u32>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<u32>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<i64>
impl AtomicMaybeUninit<i64>
Sourcepub const fn const_new(v: MaybeUninit<i64>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<i64>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<u64>
impl AtomicMaybeUninit<u64>
Sourcepub const fn const_new(v: MaybeUninit<u64>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<u64>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<i128>
impl AtomicMaybeUninit<i128>
Sourcepub const fn const_new(v: MaybeUninit<i128>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<i128>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<u128>
impl AtomicMaybeUninit<u128>
Sourcepub const fn const_new(v: MaybeUninit<u128>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<u128>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<isize>
impl AtomicMaybeUninit<isize>
Sourcepub const fn const_new(v: MaybeUninit<isize>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<isize>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Source§impl AtomicMaybeUninit<usize>
impl AtomicMaybeUninit<usize>
Sourcepub const fn const_new(v: MaybeUninit<usize>) -> Self
👎Deprecated since 0.3.10: use new instead because it is now always const fn
pub const fn const_new(v: MaybeUninit<usize>) -> Self
new instead because it is now always const fnCreates a new atomic value from a potentially uninitialized value.
Trait Implementations§
Source§impl<T: Primitive> Debug for AtomicMaybeUninit<T>
impl<T: Primitive> Debug for AtomicMaybeUninit<T>
Source§impl<T: Primitive> From<MaybeUninit<T>> for AtomicMaybeUninit<T>
impl<T: Primitive> From<MaybeUninit<T>> for AtomicMaybeUninit<T>
Source§fn from(v: MaybeUninit<T>) -> Self
fn from(v: MaybeUninit<T>) -> Self
Creates a new atomic value from a potentially uninitialized value.