portable-atomic

crates.io docs.rs license rustc build status build status

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

Usage

Add this to your Cargo.toml:

toml [dependencies] portable-atomic = "1"

The default features are mainly for users who use atomics larger than the pointer width. If you don't need them, disabling the default features may reduce code size and compile time slightly.

toml [dependencies] portable-atomic = { version = "1", default-features = false }

If your crate supports no-std environment and requires atomic CAS, enabling the require-cas feature will allow the portable-atomic to display helpful error messages to users on targets requiring additional action on the user side to provide atomic CAS.

toml [dependencies] portable-atomic = { version = "1.3", default-features = false, features = ["require-cas"] }

Compiler support: requires rustc 1.34+

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, even if cmpxchg16b is not available at compile-time (note: cmpxchg16b target feature is enabled by default only on macOS), run-time detection checks whether cmpxchg16b is available. If cmpxchg16b is not available at either compile-time or run-time detection, the fallback implementation is used. See also portable_atomic_no_outline_atomics cfg.

They are usually implemented using inline assembly, and when using Miri or ThreadSanitizer that do not support inline assembly, core intrinsics are used instead of inline assembly if possible.

See also the atomic128 module's readme.

Optional features

Optional cfg

One of the ways to enable cfg is to set [rustflags in the cargo config](https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags): ```toml

.cargo/config.toml

[target.] rustflags = ["--cfg", "portable_atomic_unsafe_assume_single_core"] ``` Or set environment variable: ```sh RUSTFLAGS="--cfg portable_atomic_unsafe_assume_single_core" cargo ... ``` md5-5977d10af9b566b3d21e15c79b2fd03e

The following are known cases:

See also the interrupt module's readme.

Consider using the critical-section feature for systems that cannot use this cfg.

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.)

ARMv6-M (thumbv6m), pre-v6 ARM (e.g., thumbv4t, thumbv5te), RISC-V without A-extension, and Xtensa are currently supported.

Since all MSP430 and AVR are single-core, we always provide atomic CAS for them without this cfg.

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

Feel free to submit an issue if your target is not supported yet.

  • --cfg portable_atomic_no_outline_atomics
    Disable dynamic dispatching by run-time CPU feature detection.

    If dynamic dispatching by run-time CPU feature detection is enabled, it allows maintaining support for older CPUs while using features that are not supported on older CPUs, such as CMPXCHG16B (x8664) and FEATLSE (aarch64).

    Note:

    See also the atomic128 module's readme.

  • Related Projects

    License

    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.