created_at2022-01-29 10:12:02.429178
updated_at2023-08-19 07:18:20.860038
descriptionByte-wise atomic memcpy.
Taiki Endo (taiki-e)



atomic-memcpy license rustc build status

Byte-wise atomic memcpy.

This is an attempt to implement equivalent of C++ "P1478: Byte-wise atomic memcpy" in Rust.

This is expected to allow algorithms such as Seqlock and Chase-Lev deque to be implemented without UB of data races. See P1478 for more.


  • If the alignment of the type being copied is the same as the pointer width, atomic_load is possible to produce an assembly roughly equivalent to the case of using volatile read + atomic fence on many platforms. (e.g., aarch64, riscv64. See tests/asm-test/asm directory for more).
  • If the alignment of the type being copied is smaller than the pointer width, there will be some performance degradation. However, it is implemented in such a way that it does not cause extreme performance degradation at least on x86_64. (See the implementation comments of atomic_load for more.) It is possible that there is still room for improvement, especially on non-x86_64 platforms.
  • Optimization for the case where the alignment of the type being copied is larger than the pointer width has not yet been fully investigated. It is possible that there is still room for improvement.
  • If the type being copied contains pointers it is not compatible with strict provenance because the copy does ptr-to-int transmutes.
  • If the type being copied contains uninitialized bytes (e.g., padding) it is undefined behavior because the copy goes through integers. This problem will probably not be resolved until something like AtomicMaybeUninit is supported.

Related Projects

  • portable-atomic: Portable atomic types including support for 128-bit atomics, atomic float, etc. Using byte-wise atomic memcpy to implement Seqlock, which is used in the fallback implementation.
  • atomic-maybe-uninit: Atomic operations on potentially uninitialized integers.


Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 179

cargo fmt