rlib
Convenience library for useful things
Loading...
Searching...
No Matches
Atomic operations

Sequentially-consistent load / store / exchange / CAS / fetch-modify operations on three atomic-friendly types (raint, rauint, raptr), plus a raboolean alias for the int variant. More...

Files

file  ratomic.h
 Atomic load / store / exchange / CAS / fetch-modify operations on integer, unsigned-integer and pointer types.
 

Typedefs

typedef int raint
 
typedef ruint rauint
 
typedef rpointer raptr
 
typedef raint raboolean
 Atomic-friendly boolean; thin alias over raint so the boolean accessors funnel through r_atomic_int_* with rboolean casts.
 

Atomic int (raint) operations

int r_atomic_int_load (raint *a)
 Atomically read a's value.
 
void r_atomic_int_store (raint *a, int val)
 Atomically store val into a.
 
int r_atomic_int_exchange (raint *a, int val)
 Atomically write val and return the previous value.
 
rboolean r_atomic_int_cmp_xchg_weak (raint *a, int *old, int val)
 Weak compare-and-swap: if *a equals *old, store val and return TRUE; otherwise write the current *a back into *old and return FALSE.
 
rboolean r_atomic_int_cmp_xchg_strong (raint *a, int *old, int val)
 Strong compare-and-swap: as r_atomic_int_cmp_xchg_weak but no spurious failure.
 
int r_atomic_int_fetch_add (raint *a, int val)
 Atomically compute *a += val and return the previous *a.
 
int r_atomic_int_fetch_sub (raint *a, int val)
 Atomically compute *a -= val and return the previous *a.
 
int r_atomic_int_fetch_and (raint *a, int val)
 Atomically compute *a &= val and return the previous *a.
 
int r_atomic_int_fetch_or (raint *a, int val)
 Atomically compute *a |= val and return the previous *a.
 
int r_atomic_int_fetch_xor (raint *a, int val)
 Atomically compute *a ^= val and return the previous *a.
 

Atomic unsigned int (rauint) operations

ruint r_atomic_uint_load (rauint *a)
 Atomically read a's value.
 
void r_atomic_uint_store (rauint *a, ruint val)
 Atomically store val into a.
 
ruint r_atomic_uint_exchange (rauint *a, ruint val)
 Atomically write val and return the previous value.
 
rboolean r_atomic_uint_cmp_xchg_weak (rauint *a, ruint *old, ruint val)
 Weak CAS; see r_atomic_int_cmp_xchg_weak for semantics.
 
rboolean r_atomic_uint_cmp_xchg_strong (rauint *a, ruint *old, ruint val)
 Strong CAS; no spurious failures.
 
ruint r_atomic_uint_fetch_add (rauint *a, ruint val)
 Atomically compute *a += val and return the previous *a.
 
ruint r_atomic_uint_fetch_sub (rauint *a, ruint val)
 Atomically compute *a -= val and return the previous *a.
 
ruint r_atomic_uint_fetch_and (rauint *a, ruint val)
 Atomically compute *a &= val and return the previous *a.
 
ruint r_atomic_uint_fetch_or (rauint *a, ruint val)
 Atomically compute *a |= val and return the previous *a.
 
ruint r_atomic_uint_fetch_xor (rauint *a, ruint val)
 Atomically compute *a ^= val and return the previous *a.
 

Atomic pointer (raptr) operations

rpointer r_atomic_ptr_load (raptr *a)
 Atomically read a's pointer value.
 
void r_atomic_ptr_store (raptr *a, rpointer val)
 Atomically store val into a.
 
rpointer r_atomic_ptr_exchange (raptr *a, rpointer val)
 Atomically write val and return the previous value.
 
rboolean r_atomic_ptr_cmp_xchg_weak (raptr *a, rpointer *old, rpointer val)
 Weak CAS on pointer storage; see r_atomic_int_cmp_xchg_weak for semantics.
 
rboolean r_atomic_ptr_cmp_xchg_strong (raptr *a, rpointer *old, rpointer val)
 Strong CAS on pointer storage; no spurious failures.
 
rpointer r_atomic_ptr_fetch_add (raptr *a, rpointer val)
 Pointer-arithmetic fetch-add: atomically *a += val and return the previous pointer.
 
rpointer r_atomic_ptr_fetch_sub (raptr *a, rpointer val)
 Pointer-arithmetic fetch-sub; semantics mirror r_atomic_ptr_fetch_add.
 
rpointer r_atomic_ptr_fetch_and (raptr *a, rpointer val)
 Bitwise &= over pointer storage (treat as integer width).
 
rpointer r_atomic_ptr_fetch_or (raptr *a, rpointer val)
 Bitwise |= over pointer storage.
 
rpointer r_atomic_ptr_fetch_xor (raptr *a, rpointer val)
 Bitwise ^= over pointer storage.
 

Atomic boolean (raboolean) sugar

Convenience macros over the r_atomic_int_* primitives that cast rboolean values through. _set / _unset are sugar for the common TRUE / FALSE assignments.

#define r_atomic_bool_load(a)   ((rboolean) r_atomic_int_load (a))
 Atomically read an raboolean.
 
#define r_atomic_bool_store(a, v)   r_atomic_int_store (a, (int) (v))
 Atomically store v into an raboolean.
 
#define r_atomic_bool_exchange(a, v)   ((rboolean) r_atomic_int_exchange (a, (int) (v)))
 Atomically write v and return the previous value.
 
#define r_atomic_bool_set(a)   r_atomic_bool_store (a, TRUE)
 Convenience: atomically store TRUE.
 
#define r_atomic_bool_unset(a)   r_atomic_bool_store (a, FALSE)
 Convenience: atomically store FALSE.
 

Detailed Description

Sequentially-consistent load / store / exchange / CAS / fetch-modify operations on three atomic-friendly types (raint, rauint, raptr), plus a raboolean alias for the int variant.

The declared R_API entry points act as the specification; each compiler family (Clang __c11_atomic_*, GCC __atomic_*, legacy __sync_*, MSVC _Interlocked*) gets substituted in by macros further down the header.

All operations are memory_order_seq_cst — the strongest ordering — to keep call sites simple. Lock-free retry loops built on r_atomic_int_cmp_xchg_weak therefore see updated values on every spurious failure without extra barriers.

Function Documentation

◆ r_atomic_int_cmp_xchg_weak()

rboolean r_atomic_int_cmp_xchg_weak ( raint *  a,
int *  old,
int  val 
)

Weak compare-and-swap: if *a equals *old, store val and return TRUE; otherwise write the current *a back into *old and return FALSE.

weak is allowed to fail spuriously; retry loops are expected.

◆ r_atomic_ptr_fetch_add()

rpointer r_atomic_ptr_fetch_add ( raptr *  a,
rpointer  val 
)

Pointer-arithmetic fetch-add: atomically *a += val and return the previous pointer.

val is byte-distance, not element count. Callers doing element-stride moves must multiply by the element size first.