Module sync/atomic

sync/atomic
Stability: unstable — the TYPES and method names are Rust's and are intended to keep their meanings, but `MemoryOrder` lost its `Consume` variant in this pass and the panic-on-illegal-order behaviour is new, so one more release of use is wanted before freezing. The `Atomic*` set itself is complete (bool, the eight fixed-width integers, `usize`, `isize`) and no additions are planned. — stable modules only change additively; this one may still change.

High-level Atomic* wrappers for thread-safe shared mutable values.

Receiver convention

Every method on every Atomic* type takes self : Self, the same convention the rest of std/sync uses for atomic(ref(...)) types (Mutex, RwLock, Cond, Channel, Once, WaitGroup). An atomic IS a shared handle — mutation goes through the C11 atomic operation, not through a Yo place-write — so an inout receiver would only demand a mutable binding at every call site without buying any extra guarantee. compare_exchange keeps inout(expected), which really is written back: on failure it is updated to the value the object actually held.

Read-modify-write family

Every integer atomic carries the full family: fetch_add, fetch_sub, fetch_and, fetch_or, fetch_xor, fetch_min, fetch_max. Each returns the value held BEFORE the operation and wraps on overflow, matching C11 atomic_fetch_*. AtomicBool has none of them: it is not an integer atomic.

AtomicI32 lowers fetch_add/sub/and/or/xor straight to the C11 atomic_fetch_*_explicit generic macros — the only atomic type std/libc/stdatomic.yo binds them for, because a c_include binding is keyed by C symbol name and those macros have exactly one Yo binding apiece. Every other type, and fetch_min/fetch_max on all of them (C11 has no atomic min/max at all), runs a strong compare-exchange loop over that type's __yo_atomic_compare_exchange_* primitive. The loop is lock-free and yields the same value and the same wrapping semantics; it only costs a retry under contention.

Memory ordering

MemoryOrder is the C11 model MINUS consume, which no production compiler implements and which Rust omits for the same reason — see the type's own doc.

An order the operation cannot legally carry PANICS rather than reaching C: C11 forbids release/acq_rel on a load and acquire/acq_rel on a store, but the order arrives as a RUNTIME value here, so the C compiler cannot diagnose it and the program would silently get some other ordering. Rust panics too. A compare-exchange loop additionally needs a read-side order for its load and for the CAS failure path; _read_order derives it by dropping the store half (Release -> Relaxed, AcqRel -> Acquire).

Stability

unstable — the TYPES and method names are Rust's and are intended to keep their meanings, but MemoryOrder lost its Consume variant in this pass and the panic-on-illegal-order behaviour is new, so one more release of use is wanted before freezing. The Atomic* set itself is complete (bool, the eight fixed-width integers, usize, isize) and no additions are planned.

Types

AtomicBool atomic object
AtomicBool

Fields

NameTypeDescription
*atomic_bool
impl(AtomicBool, ...)
new : (AtomicBool) fn(value : bool) -> AtomicBool

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valuebool

Returns: AtomicBool

load : (AtomicBool) fn(self : AtomicBool, order : MemoryOrder) -> bool

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicBool
orderMemoryOrder

Returns: bool

store : (AtomicBool) fn(self : AtomicBool, value : bool, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicBool
valuebool
orderMemoryOrder

Returns: unit

swap : (AtomicBool) fn(self : AtomicBool, value : bool, order : MemoryOrder) -> bool

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicBool
valuebool
orderMemoryOrder

Returns: bool

compare_exchange : (AtomicBool) fn(self : AtomicBool, expected : bool, new_value : bool, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicBool
expectedbool
new_valuebool
successMemoryOrder
failureMemoryOrder

Returns: bool

AtomicI8 atomic object
AtomicI8

Fields

NameTypeDescription
*atomic_schar
impl(AtomicI8, ...)
new : (AtomicI8) fn(value : i8) -> AtomicI8

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valuei8

Returns: AtomicI8

load : (AtomicI8) fn(self : AtomicI8, order : MemoryOrder) -> i8

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicI8
orderMemoryOrder

Returns: i8

store : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: unit

swap : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

compare_exchange : (AtomicI8) fn(self : AtomicI8, expected : i8, new_value : i8, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicI8
expectedi8
new_valuei8
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

fetch_sub : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

fetch_and : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

fetch_or : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

fetch_xor : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

fetch_min : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

fetch_max : (AtomicI8) fn(self : AtomicI8, value : i8, order : MemoryOrder) -> i8

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicI8
valuei8
orderMemoryOrder

Returns: i8

AtomicI16 atomic object
AtomicI16

Fields

NameTypeDescription
*atomic_short
impl(AtomicI16, ...)
new : (AtomicI16) fn(value : i16) -> AtomicI16

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valuei16

Returns: AtomicI16

load : (AtomicI16) fn(self : AtomicI16, order : MemoryOrder) -> i16

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicI16
orderMemoryOrder

Returns: i16

store : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: unit

swap : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

compare_exchange : (AtomicI16) fn(self : AtomicI16, expected : i16, new_value : i16, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicI16
expectedi16
new_valuei16
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

fetch_sub : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

fetch_and : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

fetch_or : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

fetch_xor : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

fetch_min : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

fetch_max : (AtomicI16) fn(self : AtomicI16, value : i16, order : MemoryOrder) -> i16

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicI16
valuei16
orderMemoryOrder

Returns: i16

AtomicI32 atomic object
AtomicI32

Fields

NameTypeDescription
*atomic_int
impl(AtomicI32, ...)
new : (AtomicI32) fn(value : i32) -> AtomicI32

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valuei32

Returns: AtomicI32

load : (AtomicI32) fn(self : AtomicI32, order : MemoryOrder) -> i32

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicI32
orderMemoryOrder

Returns: i32

store : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: unit

swap : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

compare_exchange : (AtomicI32) fn(self : AtomicI32, expected : i32, new_value : i32, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicI32
expectedi32
new_valuei32
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

fetch_sub : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

fetch_and : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

fetch_or : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

fetch_xor : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

fetch_min : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

fetch_max : (AtomicI32) fn(self : AtomicI32, value : i32, order : MemoryOrder) -> i32

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicI32
valuei32
orderMemoryOrder

Returns: i32

AtomicI64 atomic object
AtomicI64

Fields

NameTypeDescription
*atomic_llong
impl(AtomicI64, ...)
new : (AtomicI64) fn(value : i64) -> AtomicI64

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valuei64

Returns: AtomicI64

load : (AtomicI64) fn(self : AtomicI64, order : MemoryOrder) -> i64

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicI64
orderMemoryOrder

Returns: i64

store : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: unit

swap : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

compare_exchange : (AtomicI64) fn(self : AtomicI64, expected : i64, new_value : i64, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicI64
expectedi64
new_valuei64
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

fetch_sub : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

fetch_and : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

fetch_or : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

fetch_xor : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

fetch_min : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

fetch_max : (AtomicI64) fn(self : AtomicI64, value : i64, order : MemoryOrder) -> i64

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicI64
valuei64
orderMemoryOrder

Returns: i64

AtomicU8 atomic object
AtomicU8

Fields

NameTypeDescription
*atomic_uchar
impl(AtomicU8, ...)
new : (AtomicU8) fn(value : u8) -> AtomicU8

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valueu8

Returns: AtomicU8

load : (AtomicU8) fn(self : AtomicU8, order : MemoryOrder) -> u8

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicU8
orderMemoryOrder

Returns: u8

store : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: unit

swap : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

compare_exchange : (AtomicU8) fn(self : AtomicU8, expected : u8, new_value : u8, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicU8
expectedu8
new_valueu8
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

fetch_sub : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

fetch_and : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

fetch_or : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

fetch_xor : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

fetch_min : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

fetch_max : (AtomicU8) fn(self : AtomicU8, value : u8, order : MemoryOrder) -> u8

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicU8
valueu8
orderMemoryOrder

Returns: u8

AtomicU16 atomic object
AtomicU16

Fields

NameTypeDescription
*atomic_ushort
impl(AtomicU16, ...)
new : (AtomicU16) fn(value : u16) -> AtomicU16

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valueu16

Returns: AtomicU16

load : (AtomicU16) fn(self : AtomicU16, order : MemoryOrder) -> u16

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicU16
orderMemoryOrder

Returns: u16

store : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: unit

swap : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

compare_exchange : (AtomicU16) fn(self : AtomicU16, expected : u16, new_value : u16, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicU16
expectedu16
new_valueu16
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

fetch_sub : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

fetch_and : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

fetch_or : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

fetch_xor : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

fetch_min : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

fetch_max : (AtomicU16) fn(self : AtomicU16, value : u16, order : MemoryOrder) -> u16

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicU16
valueu16
orderMemoryOrder

Returns: u16

AtomicU32 atomic object
AtomicU32

Fields

NameTypeDescription
*atomic_uint
impl(AtomicU32, ...)
new : (AtomicU32) fn(value : u32) -> AtomicU32

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valueu32

Returns: AtomicU32

load : (AtomicU32) fn(self : AtomicU32, order : MemoryOrder) -> u32

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicU32
orderMemoryOrder

Returns: u32

store : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: unit

swap : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

compare_exchange : (AtomicU32) fn(self : AtomicU32, expected : u32, new_value : u32, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicU32
expectedu32
new_valueu32
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

fetch_sub : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

fetch_and : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

fetch_or : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

fetch_xor : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

fetch_min : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

fetch_max : (AtomicU32) fn(self : AtomicU32, value : u32, order : MemoryOrder) -> u32

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicU32
valueu32
orderMemoryOrder

Returns: u32

AtomicU64 atomic object
AtomicU64

Fields

NameTypeDescription
*atomic_ullong
impl(AtomicU64, ...)
new : (AtomicU64) fn(value : u64) -> AtomicU64

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valueu64

Returns: AtomicU64

load : (AtomicU64) fn(self : AtomicU64, order : MemoryOrder) -> u64

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicU64
orderMemoryOrder

Returns: u64

store : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: unit

swap : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

compare_exchange : (AtomicU64) fn(self : AtomicU64, expected : u64, new_value : u64, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicU64
expectedu64
new_valueu64
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

fetch_sub : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

fetch_and : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

fetch_or : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

fetch_xor : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

fetch_min : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

fetch_max : (AtomicU64) fn(self : AtomicU64, value : u64, order : MemoryOrder) -> u64

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicU64
valueu64
orderMemoryOrder

Returns: u64

AtomicUsize atomic object
AtomicUsize

Fields

NameTypeDescription
*atomic_size_t
impl(AtomicUsize, ...)
new : (AtomicUsize) fn(value : usize) -> AtomicUsize

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valueusize

Returns: AtomicUsize

load : (AtomicUsize) fn(self : AtomicUsize, order : MemoryOrder) -> usize

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicUsize
orderMemoryOrder

Returns: usize

store : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: unit

swap : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

compare_exchange : (AtomicUsize) fn(self : AtomicUsize, expected : usize, new_value : usize, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicUsize
expectedusize
new_valueusize
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

fetch_sub : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

fetch_and : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

fetch_or : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

fetch_xor : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

fetch_min : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

fetch_max : (AtomicUsize) fn(self : AtomicUsize, value : usize, order : MemoryOrder) -> usize

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicUsize
valueusize
orderMemoryOrder

Returns: usize

AtomicIsize atomic object
AtomicIsize

Fields

NameTypeDescription
*atomic_ptrdiff_t
impl(AtomicIsize, ...)
new : (AtomicIsize) fn(value : isize) -> AtomicIsize

A new atomic holding value. The initializing store is release, so the value is visible to any thread that acquires the handle.

Parameters

NameTypeNotes
valueisize

Returns: AtomicIsize

load : (AtomicIsize) fn(self : AtomicIsize, order : MemoryOrder) -> isize

Read the current value. PANICS on Release/AcqRel, which C11 does not permit on a load.

Parameters

NameTypeNotes
selfAtomicIsize
orderMemoryOrder

Returns: isize

store : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> unit

Replace the current value. PANICS on Acquire/AcqRel, which C11 does not permit on a store.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: unit

swap : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Replace the value and return what it held before — C11 atomic_exchange, Rust's swap.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

compare_exchange : (AtomicIsize) fn(self : AtomicIsize, expected : isize, new_value : isize, success : MemoryOrder, failure : MemoryOrder) -> bool

Store new_value only if the current value equals expected, and report whether it did — C11 atomic_compare_exchange_strong.

STRONG: it never fails spuriously. On failure expected is written back with the value the object actually held, which is what makes the usual retry loop terminate without a second load. success orders the store, failure orders the load taken when nothing is stored — and failure PANICS on Release/AcqRel, since that path only reads.

Parameters

NameTypeNotes
selfAtomicIsize
expectedisize
new_valueisize
successMemoryOrder
failureMemoryOrder

Returns: bool

fetch_add : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Add value, returning the PREVIOUS value. Wraps on overflow, as C11 atomic_fetch_add does.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

fetch_sub : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Subtract value, returning the PREVIOUS value. Wraps on underflow.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

fetch_and : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Bitwise-AND with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

fetch_or : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Bitwise-OR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

fetch_xor : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Bitwise-XOR with value, returning the PREVIOUS value.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

fetch_min : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Store the smaller of the current value and value, returning the PREVIOUS value. C11 has no atomic min, so this is a compare-exchange loop — lock-free, and it only costs a retry under contention.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

fetch_max : (AtomicIsize) fn(self : AtomicIsize, value : isize, order : MemoryOrder) -> isize

Store the larger of the current value and value, returning the PREVIOUS value. C11 has no atomic max; see fetch_min.

Parameters

NameTypeNotes
selfAtomicIsize
valueisize
orderMemoryOrder

Returns: isize

MemoryOrder

The memory ordering an atomic operation is performed with — Rust's atomic::Ordering, and the same five variants.

There is deliberately no Consume, though C11 has a consume ordering. It promises dependency ordering, and no production compiler implements it: clang and gcc both strengthen it to acquire, and C++ has formally discouraged it (P0371R1) for that reason. A variant that costs acquire while promising something weaker is a correctness trap — code written against the promise is unsound on a compiler that ever keeps it, and nothing today does. Rust omits it, and so does this.

Variants

VariantFieldsDescription
Relaxed
Acquire
Release
AcqRel
SeqCst
impl(MemoryOrder, ...)
to_c_order : (MemoryOrder) fn(self : MemoryOrder) -> memory_order

The C11 memory_order this maps to.

Parameters

NameTypeNotes
selfMemoryOrder

Returns: memory_order

Functions

fence function
fn(order : MemoryOrder) -> unit

Establish a memory fence with order on the calling thread.

Lowers to C11 atomic_thread_fence. An atomic operation's own ordering constrains accesses around THAT object; a fence constrains every prior and subsequent memory access of the calling thread, so it is what pairs a Relaxed store on one thread with a Relaxed load on another.

Parameters

NameTypeNotes
orderMemoryOrder

Returns: unit