Skip to main content
Enigma provides a full set of atomic operations for safe concurrent access to shared device memory. All atomic ops map directly to Metal’s atomic_* functions.

Memory ordering

All atomic operations accept a memory_order argument: The default is "relaxed" unless otherwise specified.

Atomic load and store

Atomic exchange

Returns the old value and stores value atomically:

Atomic fetch-and-modify (RMW)

Each operation atomically reads the current value, applies the operation, stores the result, and returns the old value.

Example: global counter

Example: parallel histogram

Compare-and-swap (CAS)

atomic_compare_exchange_weak attempts to replace expected with desired. Returns True if the swap succeeded:

Example: lock-free maximum

Ordering guidelines

Using "relaxed" where ordering matters is a common source of subtle bugs. When in doubt, use "acq_rel".