Expand description

Portable atomic types including support for 128-bit atomics, atomic float, etc.

  • Provide all atomic integer types (Atomic{I,U}{8,16,32,64}) for all targets that can use atomic CAS. (i.e., all targets that can use std, and most no-std targets)
  • Provide AtomicI128 and AtomicU128.
  • Provide AtomicF32 and AtomicF64. (optional)
  • Provide atomic load/store for targets where atomic is not available at all in the standard library. (thumbv6m, riscv without A-extension, msp430, avr)
  • Provide atomic CAS for targets where atomic CAS is not available in the standard library. (thumbv6m, riscv without A-extension, msp430, avr) (optional, single-core only)

128-bit atomics support

Native 128-bit atomic operations are available on x86_64 (Rust 1.59+), aarch64 (Rust 1.59+), powerpc64 (le or pwr8+, nightly only), and s390x (nightly only), otherwise the fallback implementation is used.

On x86_64, when the outline-atomics optional feature is not enabled and cmpxchg16b target feature is not enabled at compile-time, this uses the fallback implementation. cmpxchg16b target feature is enabled by default only on macOS.

See this list for details.

Optional features

  • fallback (enabled by default)
    Enable fallback implementations.

    Disabling this allows only atomic types for which the platform natively supports atomic operations.

  • outline-atomics
    Enable run-time CPU feature detection.

    This allows maintaining support for older CPUs while using features that are not supported on older CPUs, such as cmpxchg16b (x86_64) and LSE (aarch64).

    Note:

    • Dynamic detection is currently only enabled in Rust 1.61+ for aarch64, in 1.58+ (avx) or nightly (cmpxchg16b) for x86_64, and in nightly for other platforms, otherwise it works the same as the default.
    • If the required target features are enabled at compile-time, the atomic operations are inlined.
    • This is compatible with no-std (as with all features except std).

    See also this list.

  • float
    Provide AtomicF{32,64}. Note that most of fetch_* operations of atomic floats are implemented using CAS loops, which can be slower than equivalent operations of atomic integers.

  • std
    Use std.

  • serde
    Implement serde::{Serialize,Deserialize} for atomic types.

    Note:

    • The MSRV when this feature enables depends on the MSRV of serde.

Optional cfg

  • --cfg portable_atomic_unsafe_assume_single_core
    Assume that the target is single-core. When this cfg is enabled, this crate provides atomic CAS for targets where atomic CAS is not available in the standard library.

    Note: This cfg is unsafe, and enabling this cfg for multi-core systems is unsound.

    This is intentionally not an optional feature. (If this is an optional feature, dependencies can implicitly enable the feature, resulting in the use of unsound code without the end-user being aware of it.)

    Enabling this cfg for targets that have atomic CAS will result in a compile error.

Re-exports

pub use core::sync::atomic::compiler_fence;
pub use core::sync::atomic::fence;
pub use core::sync::atomic::Ordering;

Modules

Re-export of the core::hint module.

Structs

A boolean type which can be safely shared between threads.

A floating point type which can be safely shared between threads.

A floating point type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

A raw pointer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.

An integer type which can be safely shared between threads.