48 lines
1.1 KiB
C
48 lines
1.1 KiB
C
#ifndef UC_ATOMIC_H_
|
|
#define UC_ATOMIC_H_
|
|
|
|
/** Atomic functions (macros). The ptr argument must be a pointer to
|
|
* an integer type.
|
|
* For gcc, see http://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/_005f_005fsync-Builtins.html
|
|
*/
|
|
|
|
#ifdef __GNUC__
|
|
|
|
/**
|
|
* Atomically add val to *ptr
|
|
* @return *ptr before the addition
|
|
*/
|
|
#define UC_ATOMIC_ADD(ptr, val) __sync_fetch_and_add((ptr), (val))
|
|
|
|
/**
|
|
* Atomically subtract val from *ptr
|
|
* @return *ptr before the subtraction
|
|
*/
|
|
#define UC_ATOMIC_SUB(ptr, val) __sync_fetch_and_add((ptr), -(val))
|
|
|
|
/**
|
|
* Atomically decrement *ptr
|
|
* @return *ptr before the decrement
|
|
*/
|
|
#define UC_ATOMIC_DEC(ptr) UC_ATOMIC_SUB((ptr), 1)
|
|
|
|
/**
|
|
* Atomically increment *ptr
|
|
* @return *ptr before the increment
|
|
*/
|
|
#define UC_ATOMIC_INC(ptr) UC_ATOMIC_ADD((ptr), 1)
|
|
|
|
|
|
/**
|
|
* Atomic Compare-And-Swap. If *ptr is oldval, write newwal to *ptr.
|
|
* @return *ptr that was before the operation.
|
|
*/
|
|
#define UC_ATOMIC_CAS(ptr, oldval, newval) __sync_val_compare_and_swap((ptr), (oldval), (newval))
|
|
|
|
#else
|
|
#error "No atomic operations implemented for this compiler"
|
|
#endif
|
|
|
|
|
|
#endif
|