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 (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 Apple targets), 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 the atomic128 module's readme for details.

Optional features

Optional cfg

One of the ways to enable cfg is to set rustflags in the cargo config:

```toml

.cargo/config.toml

[target.] rustflags = ["--cfg", "portableatomicnooutlineatomics"] ```

Or set environment variable:

sh RUSTFLAGS="--cfg portable_atomic_no_outline_atomics" cargo ...

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.