pub unsafe fn atomic_load<T>(src: *const T, order: Ordering) -> MaybeUninit<T>
Expand description

Byte-wise atomic load.

Safety

Behavior is undefined if any of the following conditions are violated:

  • src must be valid for reads.
  • src must be properly aligned.
  • src must go through UnsafeCell::get.
  • There are no concurrent non-atomic write operations.
  • There are no concurrent atomic write operations of different granularity. The granularity of atomic operations is an implementation detail, so the concurrent write operation that can always safely be used is only atomic_store.

Like ptr::read, atomic_load creates a bitwise copy of T, regardless of whether T is Copy. If T is not Copy, using both the returned value and the value at *src can violate memory safety.

Note that even if T has size 0, the pointer must be non-null.

Returned value

This function returns MaybeUninit<T> instead of T.

  • All bits in the returned value are guaranteed to be copied from src.
  • There is no guarantee that all bits in the return have been copied at the same time, so if src is updated by a concurrent write operation, it is up to the caller to make sure that the returned value is valid as T.

Panics

Panics if order is Release or AcqRel.

Examples

use std::{cell::UnsafeCell, sync::atomic::Ordering};

let v = UnsafeCell::new([0_u8; 64]);
let result = unsafe { atomic_memcpy::atomic_load(v.get(), Ordering::Acquire) };
// SAFETY: there was no concurrent write operations during load.
assert_eq!(unsafe { result.assume_init() }, [0; 64]);