picofuse

Data Structures | Typedefs | Functions

Implements atomic values which can be safely updated across threads. More...

Collaboration diagram for Atomic Operations:

Data Structures

struct  sys_atomic_t
 Underlying atomic uint32_t value. More...
 

Typedefs

typedef struct sys_atomic_t sys_atomic_t
 Underlying atomic uint32_t value. More...
 

Functions

static void sys_atomic_init (sys_atomic_t *a, uint32_t initial)
 Initialize an atomic with an initial value. More...
 
static uint32_t sys_atomic_get (const sys_atomic_t *a)
 Load the current value atomically. More...
 
static void sys_atomic_set (sys_atomic_t *a, uint32_t v)
 Store a new value atomically. More...
 
static uint32_t sys_atomic_inc (sys_atomic_t *a)
 Atomically increment and return the new value. More...
 
static uint32_t sys_atomic_dec (sys_atomic_t *a)
 Atomically decrement and return the new value. More...
 
static void sys_atomic_set_bits (sys_atomic_t *a, uint32_t mask)
 Atomically set bits (OR with mask). More...
 
static void sys_atomic_clear_bits (sys_atomic_t *a, uint32_t mask)
 Atomically clear bits (AND with ~mask). More...
 

Detailed Description

Implements atomic values which can be safely updated across threads.

This API allows you to maintain a uint32_t value across threads safely. The operations are to get, set, increment and decrement. For the increment and decrement operations return the new value.

Typedef Documentation

◆ sys_atomic_t

typedef struct sys_atomic_t sys_atomic_t

Underlying atomic uint32_t value.

Do not access the value directly; use the sys_atomic_* APIs. in order to maintain thread safety.

Function Documentation

◆ sys_atomic_clear_bits()

static void sys_atomic_clear_bits ( sys_atomic_t a,
uint32_t  mask 
)
inlinestatic

Atomically clear bits (AND with ~mask).

Parameters
aPointer to the atomic value (must be non-NULL).
maskBit mask of bits to clear.

Performs a relaxed fetch-and with the complement of mask; no ordering is implied.

Definition at line 136 of file atomic.h.

136  {
137  (void)__atomic_fetch_and(&a->value, ~mask, __ATOMIC_RELAXED);
138 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40

◆ sys_atomic_dec()

static uint32_t sys_atomic_dec ( sys_atomic_t a)
inlinestatic

Atomically decrement and return the new value.

Parameters
aPointer to the atomic value (must be non-NULL).
Returns
The decremented value after the operation.

This uses relaxed ordering and wraps modulo 2^32.

Definition at line 109 of file atomic.h.

109  {
110  return __atomic_sub_fetch(&a->value, 1, __ATOMIC_RELAXED);
111 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40

◆ sys_atomic_get()

static uint32_t sys_atomic_get ( const sys_atomic_t a)
inlinestatic

Load the current value atomically.

Parameters
aPointer to the atomic value (must be non-NULL).
Returns
The current 32-bit value.

This is a relaxed load; it does not establish synchronization with other memory operations. If you need acquire semantics, introduce an explicit fence in the caller.

Definition at line 69 of file atomic.h.

69  {
70  return __atomic_load_n(&a->value, __ATOMIC_RELAXED);
71 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40

◆ sys_atomic_inc()

static uint32_t sys_atomic_inc ( sys_atomic_t a)
inlinestatic

Atomically increment and return the new value.

Parameters
aPointer to the atomic value (must be non-NULL).
Returns
The incremented value after the operation.

This uses relaxed ordering and wraps modulo 2^32.

Definition at line 96 of file atomic.h.

96  {
97  return __atomic_add_fetch(&a->value, 1, __ATOMIC_RELAXED);
98 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40

◆ sys_atomic_init()

static void sys_atomic_init ( sys_atomic_t a,
uint32_t  initial 
)
inlinestatic

Initialize an atomic with an initial value.

Parameters
aPointer to the atomic value to initialize (must be non-NULL).
initialThe initial 32-bit value to store.

This performs a relaxed store; it does not impose ordering with other memory operations. Use only as an initialization step before the atomic is shared with other threads.

Definition at line 54 of file atomic.h.

54  {
55  __atomic_store_n(&a->value, initial, __ATOMIC_RELAXED);
56 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40

◆ sys_atomic_set()

static void sys_atomic_set ( sys_atomic_t a,
uint32_t  v 
)
inlinestatic

Store a new value atomically.

Parameters
aPointer to the atomic value (must be non-NULL).
vThe value to store.

This is a relaxed store; it does not order or publish other writes. If you need release semantics, introduce an explicit fence in the caller.

Definition at line 83 of file atomic.h.

83  {
84  __atomic_store_n(&a->value, v, __ATOMIC_RELAXED);
85 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40

◆ sys_atomic_set_bits()

static void sys_atomic_set_bits ( sys_atomic_t a,
uint32_t  mask 
)
inlinestatic

Atomically set bits (OR with mask).

Parameters
aPointer to the atomic value (must be non-NULL).
maskBit mask of bits to set.

Performs a relaxed fetch-or; no ordering is implied.

Definition at line 122 of file atomic.h.

122  {
123  (void)__atomic_fetch_or(&a->value, mask, __ATOMIC_RELAXED);
124 }
uint32_t value
Raw atomic storage; use sys_atomic_* APIs instead.
Definition: atomic.h:40