|
| #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.
|
| |
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;
printf("Initial: %d\n", val);
printf(
"After increment: %d\n",
ATOMIC_LOAD(&counter));
int expected = 11;
} 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