Struct atomic_maybe_uninit::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
integer type, MaybeUninit<T>.
Implementations
sourceimpl<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 integer.
This is const fn on Rust 1.61+. See also const_new function.
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 fn get_mut(&mut self) -> &mut MaybeUninit<T>
pub fn get_mut(&mut self) -> &mut MaybeUninit<T>
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 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 fn into_inner(self) -> MaybeUninit<T>
pub 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 integer.
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 integer.
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 integer, 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 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.
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 their contents are not frozen
until a pointer to the value containing uninitialized bytes is passed to asm!.
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::<_, MaybeUninit<u32>>(Test(0, 0));
let v = AtomicMaybeUninit::new(x);
while v
.compare_exchange(
mem::transmute::<_, MaybeUninit<u32>>(Test(0, 0)),
mem::transmute::<_, 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::<_, MaybeUninit<u32>>(current);
let new_raw = mem::transmute::<_, MaybeUninit<u32>>(new);
loop {
match v.compare_exchange_weak(current_raw, new_raw, Ordering::AcqRel, Ordering::Acquire)
{
Ok(_) => break Ok(current),
Err(previous_raw) => {
let previous = mem::transmute_copy(&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::<_, MaybeUninit<u32>>(Test(0, 0));
let v = AtomicMaybeUninit::new(x);
while atomic_compare_exchange(&v, Test(0, 0), Test(1, 0)).is_err() {}
}See crossbeam-rs/crossbeam#315 for more details.
Also, Valgrind reports “Conditional jump or move depends on uninitialized value(s)” error if there is such comparison.
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 integer if the current value is the same as
the current value.
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 their contents are not frozen
until a pointer to the value containing uninitialized bytes is passed to asm!.
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>> where
F: FnMut(MaybeUninit<T>) -> Option<MaybeUninit<T>>,
T: AtomicCompareExchange,
pub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F
) -> Result<MaybeUninit<T>, MaybeUninit<T>> where
F: FnMut(MaybeUninit<T>) -> Option<MaybeUninit<T>>,
T: AtomicCompareExchange,
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.
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);
}sourceimpl AtomicMaybeUninit<i8>
impl AtomicMaybeUninit<i8>
sourcepub const fn const_new(v: MaybeUninit<i8>) -> Self
pub const fn const_new(v: MaybeUninit<i8>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<u8>
impl AtomicMaybeUninit<u8>
sourcepub const fn const_new(v: MaybeUninit<u8>) -> Self
pub const fn const_new(v: MaybeUninit<u8>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<i16>
impl AtomicMaybeUninit<i16>
sourcepub const fn const_new(v: MaybeUninit<i16>) -> Self
pub const fn const_new(v: MaybeUninit<i16>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<u16>
impl AtomicMaybeUninit<u16>
sourcepub const fn const_new(v: MaybeUninit<u16>) -> Self
pub const fn const_new(v: MaybeUninit<u16>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<i32>
impl AtomicMaybeUninit<i32>
sourcepub const fn const_new(v: MaybeUninit<i32>) -> Self
pub const fn const_new(v: MaybeUninit<i32>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<u32>
impl AtomicMaybeUninit<u32>
sourcepub const fn const_new(v: MaybeUninit<u32>) -> Self
pub const fn const_new(v: MaybeUninit<u32>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<i64>
impl AtomicMaybeUninit<i64>
sourcepub const fn const_new(v: MaybeUninit<i64>) -> Self
pub const fn const_new(v: MaybeUninit<i64>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<u64>
impl AtomicMaybeUninit<u64>
sourcepub const fn const_new(v: MaybeUninit<u64>) -> Self
pub const fn const_new(v: MaybeUninit<u64>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<i128>
impl AtomicMaybeUninit<i128>
sourcepub const fn const_new(v: MaybeUninit<i128>) -> Self
pub const fn const_new(v: MaybeUninit<i128>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<u128>
impl AtomicMaybeUninit<u128>
sourcepub const fn const_new(v: MaybeUninit<u128>) -> Self
pub const fn const_new(v: MaybeUninit<u128>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<isize>
impl AtomicMaybeUninit<isize>
sourcepub const fn const_new(v: MaybeUninit<isize>) -> Self
pub const fn const_new(v: MaybeUninit<isize>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
sourceimpl AtomicMaybeUninit<usize>
impl AtomicMaybeUninit<usize>
sourcepub const fn const_new(v: MaybeUninit<usize>) -> Self
pub const fn const_new(v: MaybeUninit<usize>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
Unlike new, this is always const fn.
Trait Implementations
sourceimpl<T: Primitive> Debug for AtomicMaybeUninit<T>
impl<T: Primitive> Debug for AtomicMaybeUninit<T>
sourceimpl<T: Primitive> From<MaybeUninit<T>> for AtomicMaybeUninit<T>
impl<T: Primitive> From<MaybeUninit<T>> for AtomicMaybeUninit<T>
sourcefn from(v: MaybeUninit<T>) -> Self
fn from(v: MaybeUninit<T>) -> Self
Creates a new atomic value from a potentially uninitialized integer.
sourceimpl<T: Primitive> From<T> for AtomicMaybeUninit<T>
impl<T: Primitive> From<T> for AtomicMaybeUninit<T>
impl<T: Primitive> RefUnwindSafe for AtomicMaybeUninit<T>
impl<T: Primitive> Sync for AtomicMaybeUninit<T>
Auto Trait Implementations
impl<T> Send for AtomicMaybeUninit<T>
impl<T> Unpin for AtomicMaybeUninit<T>
impl<T> UnwindSafe for AtomicMaybeUninit<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more