Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Critical Section Portable Atomic Knowledge Patch?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/nevaberry-critical-section-portable-atomic-knowledge-patch)
---
name: critical-section-portable-atomic-knowledge-patch
description: critical-section / portable-atomic
version: "1.15.0"
license: MIT
metadata:
author: Nevaberry
---
# critical-section / portable-atomic compatibility
Load this skill when migrating from `atomic-polyfill`, selecting an atomic
backend, checking compiler or CPU support, or running Loom tests with the
built-in `critical-section` implementation.
## Reference index
| Reference | Topics |
| --- | --- |
| [Backend selection](references/backend-selection.md) | Migration, incompatible backend options, interrupt masking, privileged fallback, interrupt modes, early CAS diagnostics |
| [Atomic support and runtime detection](references/atomic-support-and-runtime-detection.md) | Floating-point atomics, compiler requirements, older-compiler APIs, CPU detection, dispatch controls |
| [Loom testing](references/loom-testing.md) | The built-in `std` implementation, Loom configuration, test command |
## Migration and incompatible backend options
For a drop-in replacement for `atomic-polyfill`, enable `portable-atomic`'s
`critical-section` feature and link a critical-section implementation for the
target.
```toml
[dependencies]
portable-atomic = { version = "1", features = ["critical-section"] }
```
Libraries should leave backend selection to the application.
The build rejects `critical-section` combined with either
`unsafe-assume-single-core` or `unsafe-assume-privileged`, including their cfg
forms.
Both unsafe backends are rejected on targets such as Linux where the required
privileged instructions are unavailable. If both unsafe options are enabled,
`unsafe-assume-single-core` takes precedence.
## Single-core interrupt masking
On Arm, RISC-V, and Xtensa, `unsafe-assume-single-core`
(`portable_atomic_unsafe_assume_single_core`) supplies missing CAS operations.
With the default `fallback` feature, it also emulates wider atomics by masking
interrupts.
It requires privileged instructions that can mask the interrupts the system
needs masked and is unsound on multicore systems. MSP430 and AVR get CAS
automatically.
```toml
# .cargo/config.toml for a privileged single-core target
[target.thumbv6m-none-eabi]
rustflags = ["--cfg", "portable_atomic_unsafe_assume_single_core"]
```
## Privileged multicore fallback
On Arm, RISC-V, and Xtensa targets with atomic CAS,
`unsafe-assume-privileged` (`portable_atomic_unsafe_assume_privileged`)
combines global locks with interrupt masking for wider atomics when `fallback`
is enabled, including on multicore systems.
The target's CAS support and the enabled `fallback` feature are conditions of
this wider-atomic fallback.
## Interrupt modes for unsafe backends
On Arm outside M-profile, both unsafe backends mask IRQs by default.
Enable `disable-fiq` (`portable_atomic_disable_fiq`) to mask FIQs as well.
On RISC-V, both unsafe backends use machine-mode instructions by default.
Enable `s-mode` (`portable_atomic_s_mode`) for supervisor-mode firmware.
```toml
# .cargo/config.toml for privileged supervisor-mode firmware with CAS
[target.riscv64imac-unknown-none-elf]
rustflags = ["--cfg", "portable_atomic_unsafe_assume_privileged", "--cfg", "portable_atomic_s_mode"]
```
## Early CAS diagnostics
A `no_std` library that requires CAS can enable `require-cas` to fail early
when the application builds for a target without CAS and has yet to select a
backend that provides it.
```toml
[dependencies]
portable-atomic = { version = "1.3", default-features = false, features = ["require-cas"] }
```
Starting with `portable-atomic` 1.8 on Rust 1.78 or later, the crate can also
report missing CAS without this feature. `require-cas` still rejects the build
earlier.
## Native 128-bit atomic requirements
| Architecture | Required Rust version for native 128-bit atomics |
| --- | --- |
| x86_64, AArch64, riscv64 | 1.59 or later |
| Arm64EC, s390x | 1.84 or later |
| powerpc64 | 1.95 or later |
Other architectures and older compilers use the fallback implementation when
available.
On x86_64, the crate checks for `CMPXCHG16B` at run time when it is unavailable
at compile time. It uses the fallback if neither path provides it.
## Runtime CPU detection controls
Set `portable_atomic_no_outline_atomics` to disable runtime CPU-feature
dispatch. Set `portable_atomic_outline_atomics` to enable it on supported
targets where it defaults to off.
The disabling cfg takes precedence when both are set, works in `no_std`
builds, and is independent of LLVM's AArch64 `outline-atomics` target feature.
```toml
# .cargo/config.toml
[build]
rustflags = ["--cfg", "portable_atomic_no_outline_atomics"]
```
## Runtime detection by platform
### RISC-V on Linux and Android
Support for the RISC-V Zacas compare-and-swap extension is stable in
`portable-atomic`. On Linux and Android, the crate detects Zacas at run time
by default.
### AArch64 on FreeBSD
The crate can detect `FEAT_LRCPC3` and `FEAT_LSE128` at run time and use the
corresponding atomic instructions when available.
### PowerPC64 on AIX
The crate can detect hardware quadword (128-bit) atomics at run time.
Detection is experimental and disabled by default.
## Unstable floating-point atomics
`AtomicF16` and `AtomicF128` provide atomic operations for Rust's unstable
`f16` and `f128` types. The atomic types are gated behind unstable cfgs.
## Atomic APIs on older compilers
Rust 1.34 builds using `portable-atomic` can use newer APIs such as `from_ptr`
and the `AtomicPtr` bitwise fetch operations.
On that compiler, the crate also accepts CAS failure orderings stronger than
the success ordering.
## Loom tests with the built-in implementation
With the `std` Cargo feature enabled, the built-in `critical-section`
implementation uses Loom synchronization when compiled with `cfg(loom)`.
Run the project's Loom tests with this configuration:
```sh
RUSTFLAGS="--cfg loom" cargo test
```