Skip to main content

atomic_store

Function atomic_store 

Source
pub unsafe fn atomic_store<T>(dst: *mut T, val: T, order: Ordering)
Expand description

Byte-wise atomic store.

§Safety

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

  • dst must be valid for writes.
  • dst must be properly aligned.
  • dst must go through UnsafeCell::get.
  • T must not contain uninitialized bytes.
  • There are no concurrent non-atomic operations.
  • There are no concurrent atomic operations of different granularity. The granularity of atomic operations is an implementation detail, so the concurrent operation that can always safely be used is only atomic_load.

If there are concurrent write operations, the resulting value at *dst may contain a mixture of bytes written by this thread and bytes written by another thread. If T is not valid for all bit patterns, using the value at *dst can violate memory safety.

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

§Panics

Panics if order is Acquire or AcqRel.

§Examples

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

let v = UnsafeCell::new([0_u8; 64]);
unsafe {
    atomic_memcpy::atomic_store(v.get(), [1; 64], Ordering::Release);
}
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() }, [1; 64]);