• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# portable-atomic
2
3[![crates.io](https://img.shields.io/crates/v/portable-atomic?style=flat-square&logo=rust)](https://crates.io/crates/portable-atomic)
4[![docs.rs](https://img.shields.io/badge/docs.rs-portable--atomic-blue?style=flat-square&logo=docs.rs)](https://docs.rs/portable-atomic)
5[![license](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue?style=flat-square)](#license)
6[![msrv](https://img.shields.io/badge/msrv-1.34-blue?style=flat-square&logo=rust)](https://www.rust-lang.org)
7[![github actions](https://img.shields.io/github/actions/workflow/status/taiki-e/portable-atomic/ci.yml?branch=main&style=flat-square&logo=github)](https://github.com/taiki-e/portable-atomic/actions)
8[![cirrus ci](https://img.shields.io/cirrus/github/taiki-e/portable-atomic/main?style=flat-square&logo=cirrusci)](https://cirrus-ci.com/github/taiki-e/portable-atomic)
9
10<!-- tidy:crate-doc:start -->
11Portable atomic types including support for 128-bit atomics, atomic float, etc.
12
13- 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)
14- Provide `AtomicI128` and `AtomicU128`.
15- Provide `AtomicF32` and `AtomicF64`. ([optional, requires the `float` feature](#optional-features-float))
16- Provide atomic load/store for targets where atomic is not available at all in the standard library. (RISC-V without A-extension, MSP430, AVR)
17- Provide atomic CAS for targets where atomic CAS is not available in the standard library. (thumbv6m, pre-v6 Arm, RISC-V without A-extension, MSP430, AVR, Xtensa, etc.) (always enabled for MSP430 and AVR, [optional](#optional-features-critical-section) otherwise)
18- Provide stable equivalents of the standard library's atomic types' unstable APIs, such as [`AtomicPtr::fetch_*`](https://github.com/rust-lang/rust/issues/99108).
19- Make features that require newer compilers, such as [`fetch_{max,min}`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html#method.fetch_max), [`fetch_update`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html#method.fetch_update), [`as_ptr`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html#method.as_ptr), [`from_ptr`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html#method.from_ptr), [`AtomicBool::fetch_not`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.fetch_not) and [stronger CAS failure ordering](https://github.com/rust-lang/rust/pull/98383) available on Rust 1.34+.
20- Provide workaround for bugs in the standard library's atomic-related APIs, such as [rust-lang/rust#100650], `fence`/`compiler_fence` on MSP430 that cause LLVM error, etc.
21
22<!-- TODO:
23- mention Atomic{I,U}*::fetch_neg, Atomic{I*,U*,Ptr}::bit_*, etc.
24- mention optimizations not available in the standard library's equivalents
25-->
26
27portable-atomic version of `std::sync::Arc` is provided by the [portable-atomic-util](https://github.com/taiki-e/portable-atomic/tree/HEAD/portable-atomic-util) crate.
28
29## Usage
30
31Add this to your `Cargo.toml`:
32
33```toml
34[dependencies]
35portable-atomic = "1"
36```
37
38The default features are mainly for users who use atomics larger than the pointer width.
39If you don't need them, disabling the default features may reduce code size and compile time slightly.
40
41```toml
42[dependencies]
43portable-atomic = { version = "1", default-features = false }
44```
45
46If your crate supports no-std environment and requires atomic CAS, enabling the `require-cas` feature will allow the `portable-atomic` to display a [helpful error message](https://github.com/taiki-e/portable-atomic/pull/100) to users on targets requiring additional action on the user side to provide atomic CAS.
47
48```toml
49[dependencies]
50portable-atomic = { version = "1.3", default-features = false, features = ["require-cas"] }
51```
52
53## 128-bit atomics support
54
55Native 128-bit atomic operations are available on x86_64 (Rust 1.59+), AArch64 (Rust 1.59+), riscv64 (Rust 1.82+), powerpc64 (nightly only), s390x (nightly only), and Arm64EC (nightly only), otherwise the fallback implementation is used.
56
57On x86_64, even if `cmpxchg16b` is not available at compile-time (note: `cmpxchg16b` target feature is enabled by default only on Apple and Windows (except Windows 7) 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`](#optional-cfg-no-outline-atomics) cfg.
58
59They 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.
60
61See the [`atomic128` module's readme](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/atomic128/README.md) for details.
62
63## Optional features
64
65- **`fallback`** *(enabled by default)*<br>
66  Enable fallback implementations.
67
68  Disabling this allows only atomic types for which the platform natively supports atomic operations.
69
70- <a name="optional-features-float"></a>**`float`**<br>
71  Provide `AtomicF{32,64}`.
72
73  Note that most of `fetch_*` operations of atomic floats are implemented using CAS loops, which can be slower than equivalent operations of atomic integers. ([GPU targets have atomic instructions for float, so we plan to use these instructions for GPU targets in the future.](https://github.com/taiki-e/portable-atomic/issues/34))
74
75- **`std`**<br>
76  Use `std`.
77
78- <a name="optional-features-require-cas"></a>**`require-cas`**<br>
79  Emit compile error if atomic CAS is not available. See [Usage](#usage) section and [#100](https://github.com/taiki-e/portable-atomic/pull/100) for more.
80
81- <a name="optional-features-serde"></a>**`serde`**<br>
82  Implement `serde::{Serialize,Deserialize}` for atomic types.
83
84  Note:
85  - The MSRV when this feature is enabled depends on the MSRV of [serde].
86
87- <a name="optional-features-critical-section"></a>**`critical-section`**<br>
88  When this feature is enabled, this crate uses [critical-section] to provide atomic CAS for targets where
89  it is not natively available. When enabling it, you should provide a suitable critical section implementation
90  for the current target, see the [critical-section] documentation for details on how to do so.
91
92  `critical-section` support is useful to get atomic CAS when the [`unsafe-assume-single-core` feature](#optional-features-unsafe-assume-single-core) can't be used,
93  such as multi-core targets, unprivileged code running under some RTOS, or environments where disabling interrupts
94  needs extra care due to e.g. real-time requirements.
95
96  Note that with the `critical-section` feature, critical sections are taken for all atomic operations, while with
97  [`unsafe-assume-single-core` feature](#optional-features-unsafe-assume-single-core) some operations don't require disabling interrupts (loads and stores, but
98  additionally on MSP430 `add`, `sub`, `and`, `or`, `xor`, `not`). Therefore, for better performance, if
99  all the `critical-section` implementation for your target does is disable interrupts, prefer using
100  `unsafe-assume-single-core` feature instead.
101
102  Note:
103  - The MSRV when this feature is enabled depends on the MSRV of [critical-section].
104  - It is usually *not* recommended to always enable this feature in dependencies of the library.
105
106    Enabling this feature will prevent the end user from having the chance to take advantage of other (potentially) efficient implementations ([Implementations provided by `unsafe-assume-single-core` feature, default implementations on MSP430 and AVR](#optional-features-unsafe-assume-single-core), implementation proposed in [#60], etc. Other systems may also be supported in the future).
107
108    The recommended approach for libraries is to leave it up to the end user whether or not to enable this feature. (However, it may make sense to enable this feature by default for libraries specific to a platform where other implementations are known not to work.)
109
110    As an example, the end-user's `Cargo.toml` that uses a crate that provides a critical-section implementation and a crate that depends on portable-atomic as an option would be expected to look like this:
111
112    ```toml
113    [dependencies]
114    portable-atomic = { version = "1", default-features = false, features = ["critical-section"] }
115    crate-provides-critical-section-impl = "..."
116    crate-uses-portable-atomic-as-feature = { version = "...", features = ["portable-atomic"] }
117    ```
118
119- <a name="optional-features-unsafe-assume-single-core"></a>**`unsafe-assume-single-core`**<br>
120  Assume that the target is single-core.
121  When this feature is enabled, this crate provides atomic CAS for targets where atomic CAS is not available in the standard library by disabling interrupts.
122
123  This feature is `unsafe`, and note the following safety requirements:
124  - Enabling this feature for multi-core systems is always **unsound**.
125  - This uses privileged instructions to disable interrupts, so it usually doesn't work on unprivileged mode.
126    Enabling this feature in an environment where privileged instructions are not available, or if the instructions used are not sufficient to disable interrupts in the system, it is also usually considered **unsound**, although the details are system-dependent.
127
128    The following are known cases:
129    - On pre-v6 Arm, this disables only IRQs by default. For many systems (e.g., GBA) this is enough. If the system need to disable both IRQs and FIQs, you need to enable the `disable-fiq` feature together.
130    - On RISC-V without A-extension, this generates code for machine-mode (M-mode) by default. If you enable the `s-mode` together, this generates code for supervisor-mode (S-mode). In particular, `qemu-system-riscv*` uses [OpenSBI](https://github.com/riscv-software-src/opensbi) as the default firmware.
131
132    See also the [`interrupt` module's readme](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/interrupt/README.md).
133
134  Consider using the [`critical-section` feature](#optional-features-critical-section) for systems that cannot use this feature.
135
136  It is **very strongly discouraged** to enable this feature in libraries that depend on `portable-atomic`. The recommended approach for libraries is to leave it up to the end user whether or not to enable this feature. (However, it may make sense to enable this feature by default for libraries specific to a platform where it is guaranteed to always be sound, for example in a hardware abstraction layer targeting a single-core chip.)
137
138  Armv6-M (thumbv6m), pre-v6 Arm (e.g., thumbv4t, thumbv5te), RISC-V without A-extension, and Xtensa are currently supported.
139
140  Since all MSP430 and AVR are single-core, we always provide atomic CAS for them without this feature.
141
142  Enabling this feature for targets that have atomic CAS will result in a compile error.
143
144  Feel free to submit an issue if your target is not supported yet.
145
146## Optional cfg
147
148One of the ways to enable cfg is to set [rustflags in the cargo config](https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags):
149
150```toml
151# .cargo/config.toml
152[target.<target>]
153rustflags = ["--cfg", "portable_atomic_no_outline_atomics"]
154```
155
156Or set environment variable:
157
158```sh
159RUSTFLAGS="--cfg portable_atomic_no_outline_atomics" cargo ...
160```
161
162- <a name="optional-cfg-unsafe-assume-single-core"></a>**`--cfg portable_atomic_unsafe_assume_single_core`**<br>
163  Since 1.4.0, this cfg is an alias of [`unsafe-assume-single-core` feature](#optional-features-unsafe-assume-single-core).
164
165  Originally, we were providing these as cfgs instead of features, but based on a strong request from the embedded ecosystem, we have agreed to provide them as features as well. See [#94](https://github.com/taiki-e/portable-atomic/pull/94) for more.
166
167- <a name="optional-cfg-no-outline-atomics"></a>**`--cfg portable_atomic_no_outline_atomics`**<br>
168  Disable dynamic dispatching by run-time CPU feature detection.
169
170  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 (x86_64) and FEAT_LSE/FEAT_LSE2 (AArch64).
171
172  Note:
173  - Dynamic detection is currently only enabled in Rust 1.59+ for x86_64 and AArch64, Rust 1.82+ for RISC-V (disabled by default), nightly only for powerpc64 (disabled by default) and Arm64EC, otherwise it works the same as when this cfg is set.
174  - If the required target features are enabled at compile-time, the atomic operations are inlined.
175  - This is compatible with no-std (as with all features except `std`).
176  - On some targets, run-time detection is disabled by default mainly for compatibility with older versions of operating systems or incomplete build environments, and can be enabled by `--cfg portable_atomic_outline_atomics`. (When both cfg are enabled, `*_no_*` cfg is preferred.)
177  - Some AArch64 targets enable LLVM's `outline-atomics` target feature by default, so if you set this cfg, you may want to disable that as well. (portable-atomic's outline-atomics does not depend on the compiler-rt symbols, so even if you need to disable LLVM's outline-atomics, you may not need to disable portable-atomic's outline-atomics.)
178
179  See also the [`atomic128` module's readme](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/atomic128/README.md).
180
181## Related Projects
182
183- [atomic-maybe-uninit]: Atomic operations on potentially uninitialized integers.
184- [atomic-memcpy]: Byte-wise atomic memcpy.
185
186[#60]: https://github.com/taiki-e/portable-atomic/issues/60
187[atomic-maybe-uninit]: https://github.com/taiki-e/atomic-maybe-uninit
188[atomic-memcpy]: https://github.com/taiki-e/atomic-memcpy
189[critical-section]: https://github.com/rust-embedded/critical-section
190[rust-lang/rust#100650]: https://github.com/rust-lang/rust/issues/100650
191[serde]: https://github.com/serde-rs/serde
192
193<!-- tidy:crate-doc:end -->
194
195## License
196
197Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
198[MIT license](LICENSE-MIT) at your option.
199
200Unless you explicitly state otherwise, any contribution intentionally submitted
201for inclusion in the work by you, as defined in the Apache-2.0 license, shall
202be dual licensed as above, without any additional terms or conditions.
203