funct(3rtapi) | RTAPI | funct(3rtapi) |
rtapi_atomic - subset of C11 <stdatomic.h>
#include <rtapi_atomic.h>
enum memory_order { ... };
#define atomic_store(obj, desired)...
#define atomic_store_explicit(obj, desired, order)...
#define atomic_load(obj)...
#define atomic_load_explicit(obj, order)...
This header provides at least the subset of C11's <stdatomic.h> given above. When there is an ordering requirement for multiple values read or written in RTAPI shared memory areas by other threads of execution, including the values of HAL pins and parameters, these functions (or function-like macros) are the only way to ensure the ordering requirement is obeyed. Otherwise, according to architecture-specific rules, loads and stores may be reordered from their normal source code order.
For example, to leave a message in a shared memory area from one thread and retrieve it from another, the writer must use an atomic store for the "message is complete" variable, and the reader must use an atomic load when checking that variable:
// producer *message = 42; atomic_store_explicit(message_ready, 1, memory_order_release); // consumer while(atomic_load_explicit(message_ready, memory_order_acquire) == 0) sched_yield(); printf("message was %d\n", *message); // must print 42
May be called from any code.
atomic_load and atomic_load_explicit return the value pointed to by the obj argument.
atomic_store and atomic_store_explicit have no return value.
<stdatomic.h> (C11), <rtapi_bitops.h> (for other atomic memory operations supported by rtapi)
2006-10-12 | LinuxCNC Documentation |