Debian Codegraph @

debian/rust-vendor/portable-atomic/README.md

raw
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
9 <!-- tidy:sync-markdown-to-rustdoc:start:src/lib.rs -->
10
11 Portable 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 `AtomicF16` and `AtomicF128` for [unstable `f16` and `f128`](https://github.com/rust-lang/rust/issues/116909). ([optional, requires the `float` feature and unstable cfgs](#optional-features-float))
17 - Provide atomic load/store for targets where atomic is not available at all in the standard library. (RISC-V without A-extension, MSP430, AVR)
18 - 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)
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), [`AtomicPtr::fetch_*`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicPtr.html#method.fetch_and), 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/compiler-builtins#1234], [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
27 portable-atomic version of `std::sync::Arc` and `std::task::Wake` is provided by the [portable-atomic-util] crate.
28
29 ## Usage
30
31 Add this to your `Cargo.toml`:
32
33 ```toml
34 [dependencies]
35 portable-atomic = "1"
36 ```
37
38 The default features are mainly for users who use atomics larger than the pointer width.
39 If you don't need them, disabling the default features may reduce code size and compile time slightly.
40
41 ```toml
42 [dependencies]
43 portable-atomic = { version = "1", default-features = false }
44 ```
45
46 If 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]
50 portable-atomic = { version = "1.3", default-features = false, features = ["require-cas"] }
51 ```
52
53 (Since 1.8, portable-atomic can display a [helpful error message](https://github.com/taiki-e/portable-atomic/pull/181) even without the `require-cas` feature when the rustc version is 1.78+. However, the `require-cas` feature also allows rejecting builds at an earlier stage, we recommend enabling it unless enabling it causes [problems](https://github.com/matklad/once_cell/pull/267).)
54
55 ## 128-bit atomics support
56
57 Native 128-bit atomic operations are available on x86_64 (Rust 1.59+), AArch64 (Rust 1.59+), riscv64 (Rust 1.59+), Arm64EC (Rust 1.84+), s390x (Rust 1.84+), and powerpc64 (Rust 1.95+), otherwise the fallback implementation is used.
58
59 On x86_64, even if `cmpxchg16b` is not available at compile-time (Note: `cmpxchg16b` target feature is enabled by default only on Apple, Windows (except Windows 7), and Fuchsia 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.
60
61 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.
62
63 See the [`atomic128` module's readme](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/atomic128/README.md) for details.
64
65 ## <a name="optional-features"></a><a name="optional-cfg"></a>Optional features/cfgs
66
67 portable-atomic provides features and cfgs to allow enabling specific APIs and customizing its behavior.
68
69 Some options have both a feature and a cfg. When both exist, it indicates that the feature does not follow Cargo's recommendation that [features should be additive](https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-unification). Therefore, the maintainer's recommendation is to use cfg instead of feature. However, in the embedded ecosystem, it is very common to use features in such places, so these options provide both so you can choose based on your preference.
70
71 <details>
72 <summary>How to enable cfg (click to show)</summary>
73
74 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):
75
76 ```toml
77 # .cargo/config.toml
78 [target.<target>]
79 rustflags = ["--cfg", "portable_atomic_unsafe_assume_single_core"]
80 ```
81
82 Or set environment variable:
83
84 ```sh
85 RUSTFLAGS="--cfg portable_atomic_unsafe_assume_single_core" cargo ...
86 ```
87
88 </details>
89
90 - <a name="optional-features-fallback"></a>**`fallback` feature** *(enabled by default)*<br>
91 Enable fallback implementations.
92
93 This enables atomic types with larger than the width supported by atomic instructions available on the current target. If the current target [supports 128-bit atomics](#128-bit-atomics-support), this is no-op.
94
95 This uses fallback implementation that using global locks by default. The following features/cfgs change this behavior:
96 - [`unsafe-assume-single-core` feature / `portable_atomic_unsafe_assume_single_core` cfg](#optional-features-unsafe-assume-single-core): Use fallback implementations that disabling interrupts instead of using global locks.
97 - If your target is single-core and calling interrupt disable instructions is safe, this is a safer and more efficient option.
98 - [`unsafe-assume-privileged` feature / `portable_atomic_unsafe_assume_privileged` cfg](#optional-features-unsafe-assume-privileged): Use fallback implementations that using global locks with disabling interrupts.
99 - If your target is multi-core and calling interrupt disable instructions is safe, this is a safer option.
100
101 - <a name="optional-features-float"></a>**`float` feature**<br>
102 Provide `AtomicF{32,64}`.
103
104 If you want atomic types for unstable float types ([`f16` and `f128`](https://github.com/rust-lang/rust/issues/116909)), enable unstable cfg (`portable_atomic_unstable_f16` cfg for `AtomicF16`, `portable_atomic_unstable_f128` cfg for `AtomicF128`, [there is no possibility that both feature and cfg will be provided for unstable options.](https://github.com/taiki-e/portable-atomic/pull/200#issuecomment-2682252991)).
105
106 > [!NOTE]
107 > - Atomic float's `fetch_{add,sub,min,max}` are usually implemented using CAS loops, which can be slower than equivalent operations of atomic integers. As an exception, AArch64 with FEAT_LSFE and GPU targets have atomic float instructions and we use them on AArch64 when `lsfe` target feature is available at compile-time. We [plan to use atomic float instructions for GPU targets as well in the future.](https://github.com/taiki-e/portable-atomic/issues/34)
108 > - Unstable cfgs are outside of the normal semver guarantees and minor or patch versions of portable-atomic may make breaking changes to them at any time.
109
110 - <a name="optional-features-std"></a>**`std` feature**<br>
111 Use `std`.
112
113 - <a name="optional-features-require-cas"></a>**`require-cas` feature**<br>
114 Emit compile error if atomic CAS is not available. See [Usage](#usage) section for usage of this feature.
115
116 - <a name="optional-features-serde"></a>**`serde` feature**<br>
117 Implement `serde::{Serialize,Deserialize}` for atomic types.
118
119 Note:
120 - The MSRV when this feature is enabled depends on the MSRV of [serde].
121
122 - <a name="optional-features-critical-section"></a>**`critical-section` feature**<br>
123 Use [critical-section] to provide atomic CAS for targets where atomic CAS is not available in the standard library.
124
125 `critical-section` support is useful to get atomic CAS when the [`unsafe-assume-single-core` feature (or `portable_atomic_unsafe_assume_single_core` cfg)](#optional-features-unsafe-assume-single-core) can't be used,
126 such as multi-core targets, unprivileged code running under some RTOS, or environments where disabling interrupts
127 needs extra care due to e.g. real-time requirements.
128
129 > [!NOTE]
130 > - When enabling this feature, you should provide a suitable critical section implementation for the current target, see the [critical-section] documentation for details on how to do so.
131 > - With this feature, critical sections are taken for all atomic operations, while with `unsafe-assume-single-core` feature [some operations](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/interrupt/README.md#no-disable-interrupts) don't require disabling interrupts. Therefore, for better performance, if all the `critical-section` implementation for your target does is disable interrupts, prefer using `unsafe-assume-single-core` feature (or `portable_atomic_unsafe_assume_single_core` cfg) instead.
132 > - It is usually **discouraged** to always enable this feature in libraries that depend on `portable-atomic`.
133 >
134 > 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 mentioned above, implementation proposed in [#60], etc.). Also, targets that are currently unsupported may be supported in the future.
135 >
136 > 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.)
137 >
138 > See also [this comment](https://github.com/matklad/once_cell/issues/264#issuecomment-2352654806).
139 >
140 > 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:
141 >
142 > ```toml
143 > [dependencies]
144 > portable-atomic = { version = "1", default-features = false, features = ["critical-section"] }
145 > crate-provides-critical-section-impl = "..."
146 > crate-uses-portable-atomic-as-feature = { version = "...", features = ["portable-atomic"] }
147 > ```
148 >
149 > - Enabling both this feature and `unsafe-assume-single-core` feature (or `portable_atomic_unsafe_assume_single_core` cfg) will result in a compile error.
150 > - Enabling both this feature and `unsafe-assume-privileged` feature (or `portable_atomic_unsafe_assume_privileged` cfg) will result in a compile error.
151 > - The MSRV when this feature is enabled depends on the MSRV of [critical-section].
152
153 - <a name="optional-features-unsafe-assume-single-core"></a><a name="optional-cfg-unsafe-assume-single-core"></a>**`unsafe-assume-single-core` feature / `portable_atomic_unsafe_assume_single_core` cfg**<br>
154 Assume that the target is single-core and privileged instructions required to disable interrupts are available.
155
156 - When this feature/cfg is enabled, this crate provides atomic CAS for targets where atomic CAS is not available in the standard library by disabling interrupts.
157 - When both this feature/cfg and enabled-by-default `fallback` feature is enabled, this crate provides atomic types with larger than the width supported by native instructions by disabling interrupts.
158
159 > [!WARNING]
160 > This feature/cfg is `unsafe`, and note the following safety requirements:
161 > - Enabling this feature/cfg for multi-core systems is always **unsound**.
162 >
163 > - This uses privileged instructions to disable interrupts, so it usually doesn't work on unprivileged mode.
164 >
165 > Enabling this feature/cfg 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.
166 >
167 > The following are known cases:
168 > - On Arm (except for M-Profile architectures), 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 (or `portable_atomic_disable_fiq` cfg) together.
169 > - On RISC-V, this generates code for machine-mode (M-mode) by default. If you enable the `s-mode` feature (or `portable_atomic_s_mode` cfg) 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.
170
171 Consider using the [`unsafe-assume-privileged` feature (or `portable_atomic_unsafe_assume_privileged` cfg)](#optional-features-unsafe-assume-privileged) for multi-core systems with atomic CAS.
172
173 Consider using the [`critical-section` feature](#optional-features-critical-section) for systems that cannot use this feature/cfg.
174
175 See also the [`interrupt` module's readme](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/interrupt/README.md).
176
177 > [!NOTE]
178 > - It is **very strongly discouraged** to enable this feature/cfg in libraries that depend on `portable-atomic`.
179 >
180 > The recommended approach for libraries is to leave it up to the end user whether or not to enable this feature/cfg. (However, it may make sense to enable this feature/cfg 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.)
181 > - Enabling this feature/cfg for unsupported architectures will result in a compile error.
182 > - Arm, RISC-V, and Xtensa are currently supported. (Since all MSP430 and AVR are single-core, we always provide atomic CAS for them without this feature/cfg.)
183 > - Feel free to [submit an issue](https://github.com/taiki-e/portable-atomic/issues/new) if your target is not supported yet.
184 > - Enabling this feature/cfg for targets where privileged instructions are obviously unavailable (e.g., Linux) will result in a compile error.
185 > - Feel free to [submit an issue](https://github.com/taiki-e/portable-atomic/issues/new) if your target supports privileged instructions but the build rejected.
186 > - Enabling both this feature/cfg and `critical-section` feature will result in a compile error.
187 > - When both this feature/cfg and `unsafe-assume-privileged` feature (or `portable_atomic_unsafe_assume_privileged` cfg) are enabled, this feature/cfg is preferred.
188
189 - <a name="optional-features-unsafe-assume-privileged"></a><a name="optional-cfg-unsafe-assume-privileged"></a>**`unsafe-assume-privileged` feature / `portable_atomic_unsafe_assume_privileged` cfg**<br>
190 Similar to `unsafe-assume-single-core` feature / `portable_atomic_unsafe_assume_single_core` cfg, but only assumes about availability of privileged instructions required to disable interrupts.
191
192 - When both this feature/cfg and enabled-by-default `fallback` feature is enabled, this crate provides atomic types with larger than the width supported by native instructions by using global locks with disabling interrupts.
193
194 > [!WARNING]
195 > This feature/cfg is `unsafe`, and except for being sound in multi-core systems, this has the same safety requirements as [`unsafe-assume-single-core` feature / `portable_atomic_unsafe_assume_single_core` cfg](#optional-features-unsafe-assume-single-core).
196
197 > [!NOTE]
198 > - It is **very strongly discouraged** to enable this feature/cfg in libraries that depend on `portable-atomic`.
199 >
200 > The recommended approach for libraries is to leave it up to the end user whether or not to enable this feature/cfg. (However, it may make sense to enable this feature/cfg by default for libraries specific to a platform where it is guaranteed to always be sound, for example in a hardware abstraction layer.)
201 > - Enabling this feature/cfg for unsupported targets will result in a compile error.
202 > - This requires atomic CAS (`cfg(target_has_atomic = "ptr")` or `cfg_no_atomic_cas!`).
203 > - Arm, RISC-V, and Xtensa are currently supported.
204 > - Feel free to [submit an issue](https://github.com/taiki-e/portable-atomic/issues/new) if your target is not supported yet.
205 > - Enabling this feature/cfg for targets where privileged instructions are obviously unavailable (e.g., Linux) will result in a compile error.
206 > - Feel free to [submit an issue](https://github.com/taiki-e/portable-atomic/issues/new) if your target supports privileged instructions but the build rejected.
207 > - Enabling both this feature/cfg and `critical-section` feature will result in a compile error.
208 > - When both this feature/cfg and `unsafe-assume-single-core` feature (or `portable_atomic_unsafe_assume_single_core` cfg) are enabled, `unsafe-assume-single-core` is preferred.
209
210 - <a name="optional-cfg-no-outline-atomics"></a>**`portable_atomic_no_outline_atomics` cfg**<br>
211 Disable dynamic dispatching by run-time CPU feature detection.
212
213 Dynamic dispatching by run-time CPU feature detection 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).
214
215 See also the [`atomic128` module's readme](https://github.com/taiki-e/portable-atomic/blob/HEAD/src/imp/atomic128/README.md).
216
217 > [!NOTE]
218 > - If the required target features are enabled at compile-time, dynamic dispatching is automatically disabled and the atomic operations are inlined.
219 > - This is compatible with no-std (as with all features except `std`).
220 > - On some targets, run-time detection is disabled by default mainly for compatibility with incomplete build environments or support for it is experimental, and can be enabled by `portable_atomic_outline_atomics` cfg. (When both cfg are enabled, `*_no_*` cfg is preferred.)
221 > - 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. (However, 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.)
222 > - Dynamic detection is currently only supported in x86_64, AArch64, Arm, RISC-V, Arm64EC, and powerpc64. Enabling this cfg for unsupported architectures will be ignored.
223
224 ## Related Projects
225
226 - [portable-atomic-util]: Synchronization primitives built with portable-atomic. This provides portable-atomic version of `std::sync::Arc` and `std::task::Wake`.
227 - [atomic-maybe-uninit]: Atomic operations on potentially uninitialized integers.
228 - [atomic-memcpy]: Byte-wise atomic memcpy.
229 - [asmtest]: A library for tracking generated assemblies.
230
231 [#60]: https://github.com/taiki-e/portable-atomic/issues/60
232 [asmtest]: https://github.com/taiki-e/asmtest
233 [atomic-maybe-uninit]: https://github.com/taiki-e/atomic-maybe-uninit
234 [atomic-memcpy]: https://github.com/taiki-e/atomic-memcpy
235 [critical-section]: https://github.com/rust-embedded/critical-section
236 [portable-atomic-util]: https://github.com/taiki-e/portable-atomic-util
237 [rust-lang/compiler-builtins#1234]: https://github.com/rust-lang/compiler-builtins/pull/1234
238 [rust-lang/rust#100650]: https://github.com/rust-lang/rust/issues/100650
239 [serde]: https://github.com/serde-rs/serde
240
241 <!-- tidy:sync-markdown-to-rustdoc:end -->
242
243 ## License
244
245 Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
246 [MIT license](LICENSE-MIT) at your option.
247
248 Unless you explicitly state otherwise, any contribution intentionally submitted
249 for inclusion in the work by you, as defined in the Apache-2.0 license, shall
250 be dual licensed as above, without any additional terms or conditions.