Skip to content
Back to skills

Critical Section Portable Atomic Knowledge Patch

ASecurity

critical-section / portable-atomic

  • 25 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added December 1, 2026
developmentrustgotestingapibackend

Works with

  • api

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned December 1, 2026

npx -y skills add Nevaberry/nevaberry-plugins --skill critical-section-portable-atomic-knowledge-patch --agent claude-code

Installs into .claude/skills of the current project.

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.

Security grade badge for Critical Section Portable Atomic Knowledge Patch
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/nevaberry-critical-section-portable-atomic-knowledge-patch-nevaberry-plugins/badge)](https://www.skillsdirectory.com/skills/nevaberry-critical-section-portable-atomic-knowledge-patch-nevaberry-plugins)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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

Use this skill when migrating from `atomic-polyfill`, selecting atomic
backends, configuring runtime CPU detection, using atomic APIs on older
compilers, or running Loom tests with `critical-section`.

## Reference index

| Reference | Topics |
| --- | --- |
| [Backend selection](references/backend-selection.md) | Migration, incompatible backend options, unsafe backends, interrupt modes, early CAS diagnostics |
| [CPU detection and native atomics](references/cpu-detection-and-native-atomics.md) | Native 128-bit requirements, runtime dispatch controls, RISC-V, AArch64 FreeBSD, PowerPC64 AIX |
| [Atomic API compatibility](references/atomic-api-compatibility.md) | Unstable floating-point atomics, newer APIs and CAS orderings on older compilers |
| [Loom testing](references/loom-testing.md) | The built-in `std` implementation and `cfg(loom)` |

## Migration and backend incompatibilities

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.

## Unsafe backend requirements

### 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 fallback, including multicore

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.

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.

### Interrupt modes

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.

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

## Runtime CPU detection

### Dispatch controls

| cfg | Effect |
| --- | --- |
| `portable_atomic_no_outline_atomics` | Disables runtime CPU-feature dispatch |
| `portable_atomic_outline_atomics` | Enables runtime CPU-feature dispatch 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"]
```

### Platform-specific detection

- RISC-V Zacas compare-and-swap support is stable in `portable-atomic`.
  On Linux and Android, the crate detects Zacas at run time by default.
- On AArch64 FreeBSD, the crate can detect `FEAT_LRCPC3` and `FEAT_LSE128`
  at run time and use the corresponding atomic instructions when available.
- On PowerPC64 AIX, runtime detection of hardware quadword (128-bit)
  atomics is experimental and disabled by default.

## Native 128-bit atomic requirements

| Architectures | Required Rust version |
| --- | --- |
| 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 and uses the fallback if neither path provides it.

## 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. The crate also
accepts CAS failure orderings stronger than the success ordering on that
compiler.

## Loom testing 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)`.
To run your project's Loom tests with this configuration:

```sh
RUSTFLAGS="--cfg loom" cargo test
```

Files in this skill

  • SKILL.md5.8 KB
  • coverage.json598 B
  • references/atomic-api-compatibility.md541 B
  • references/backend-selection.md2.7 KB
  • references/cpu-detection-and-native-atomics.md1.7 KB
  • references/loom-testing.md364 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.