C-libs 0.1.0
Some C utils libraries
 
Loading...
Searching...
No Matches
atomics.h File Reference

Atomic operation macros and typedefs using GCC/Clang __atomic_* builtins. More...

Macros

#define _HAVE___ATOMIC_BUILTINS   1
 
#define ATOMIC_LOAD(Ptr)
 Load the value atomically from the given pointer.
 
#define ATOMIC_STORE(Ptr, Value)
 Store a value atomically to the given pointer.
 
#define ATOMIC_EXCHANGE(Ptr, Value)
 Atomically exchange the value at the pointer with a new value.
 
#define ATOMIC_COMPARE_EXCHANGE(Ptr, PtrExpected, DesiredValue)
 Atomically compare and exchange the value.
 
#define ATOMIC_FETCH_ADD(Ptr, Value)
 Atomically add a value and return the old value.
 
#define ATOMIC_INC(Ptr)
 Atomically increment the value and return the new value.
 
#define ATOMIC_DEC(Ptr)
 Atomically decrement the value and return the new value.
 
#define DEFINE_ATOMIC(Type)
 Declares a volatile (atomic) type.
 
#define DEFINE_ATOMIC_PTR(Type)
 Declares a volatile (atomic) pointer to a type.
 
#define ATOMIC(Type)
 Shorthand for declaring a volatile atomic type.
 

Detailed Description

Atomic operation macros and typedefs using GCC/Clang __atomic_* builtins.

This header provides macros for performing atomic operations using the __atomic_* builtins provided by GCC 4.7+ or Clang 3.3+. The macros are designed to work with scalar or pointer types that are 1, 2, 4, or 8 bytes in size.

Compilation will fail if the compiler does not support the required built-ins.

Note
All operations use __ATOMIC_SEQ_CST memory ordering for maximum safety.
Todo

Add other atomic operation (even if less common in my use cases)

Add support if __atomic_* builtins cannot be used

Usage example:

#include <stdio.h>
int main() {
atomic_int counter = 0;
ATOMIC_STORE(&counter, 10);
int val = ATOMIC_LOAD(&counter);
printf("Initial: %d\n", val);
ATOMIC_INC(&counter);
printf("After increment: %d\n", ATOMIC_LOAD(&counter));
int expected = 11;
if (ATOMIC_COMPARE_EXCHANGE(&counter, &expected, 20)) {
printf("CAS succeeded: %d\n", ATOMIC_LOAD(&counter));
} else {
printf("CAS failed: expected was %d\n", expected);
}
return 0;
}
Atomic operation macros and typedefs using GCC/Clang __atomic_* builtins.
#define ATOMIC_STORE(Ptr, Value)
Store a value atomically to the given pointer.
Definition atomics.h:84
#define ATOMIC_INC(Ptr)
Atomically increment the value and return the new value.
Definition atomics.h:121
#define DEFINE_ATOMIC(Type)
Declares a volatile (atomic) type.
Definition atomics.h:135
#define ATOMIC_LOAD(Ptr)
Load the value atomically from the given pointer.
Definition atomics.h:77
#define ATOMIC_COMPARE_EXCHANGE(Ptr, PtrExpected, DesiredValue)
Atomically compare and exchange the value.
Definition atomics.h:103

Macro Definition Documentation

◆ ATOMIC

#define ATOMIC ( Type)
Value:
volatile Type

Shorthand for declaring a volatile atomic type.

Parameters
TypeThe base type.

◆ ATOMIC_COMPARE_EXCHANGE

#define ATOMIC_COMPARE_EXCHANGE ( Ptr,
PtrExpected,
DesiredValue )
Value:
__atomic_compare_exchange_n(Ptr, PtrExpected, DesiredValue, 0, \
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)

Atomically compare and exchange the value.

Parameters
PtrPointer to the atomic variable.
PtrExpectedPointer to the expected value.
DesiredValueValue to store if the comparison succeeds.
Returns
true if the exchange took place, false otherwise.

◆ ATOMIC_DEC

#define ATOMIC_DEC ( Ptr)
Value:
__atomic_sub_fetch(Ptr, 1, __ATOMIC_SEQ_CST)

Atomically decrement the value and return the new value.

Parameters
PtrPointer to the atomic variable.
Returns
The value after decrementing.

◆ ATOMIC_EXCHANGE

#define ATOMIC_EXCHANGE ( Ptr,
Value )
Value:
__atomic_exchange_n(Ptr, Value, __ATOMIC_SEQ_CST)

Atomically exchange the value at the pointer with a new value.

Parameters
PtrPointer to the atomic variable.
ValueNew value to set.
Returns
The old value before the exchange.

◆ ATOMIC_FETCH_ADD

#define ATOMIC_FETCH_ADD ( Ptr,
Value )
Value:
__atomic_fetch_add(Ptr, Value, __ATOMIC_SEQ_CST)

Atomically add a value and return the old value.

Parameters
PtrPointer to the atomic variable.
ValueValue to add.
Returns
The value before the addition.

◆ ATOMIC_INC

#define ATOMIC_INC ( Ptr)
Value:
__atomic_add_fetch(Ptr, 1, __ATOMIC_SEQ_CST)

Atomically increment the value and return the new value.

Parameters
PtrPointer to the atomic variable.
Returns
The value after incrementing.
Examples
example_atomics.c.

◆ ATOMIC_LOAD

#define ATOMIC_LOAD ( Ptr)
Value:
__atomic_load_n(Ptr, __ATOMIC_SEQ_CST)

Load the value atomically from the given pointer.

Parameters
PtrPointer to the atomic variable.
Returns
Value loaded from the pointer.
Examples
example_atomics.c.

◆ ATOMIC_STORE

#define ATOMIC_STORE ( Ptr,
Value )
Value:
__atomic_store_n(Ptr, Value, __ATOMIC_SEQ_CST)

Store a value atomically to the given pointer.

Parameters
PtrPointer to the atomic variable.
ValueValue to store.

◆ DEFINE_ATOMIC

#define DEFINE_ATOMIC ( Type)
Value:
typedef volatile Type atomic_##Type

Declares a volatile (atomic) type.

Parameters
TypeThe base type.
Examples
example_atomics.c.

◆ DEFINE_ATOMIC_PTR

#define DEFINE_ATOMIC_PTR ( Type)
Value:
typedef volatile Type *GLUE(atomic_, GLUE(Type, _ptr))
#define GLUE(a, b)
Definition util_macros.h:240

Declares a volatile (atomic) pointer to a type.

Parameters
TypeThe base type.