#ifndef UC_ATOMIC_H_ #define UC_ATOMIC_H_ /** @file * 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 subtract val from *ptr * @return *ptr before the subtraction */ #define UC_ATOMIC_SUB(ptr, val) __sync_fetch_and_add((ptr), -(val)) /** * Atomically add val to *ptr * @return *ptr before the addition */ #define UC_ATOMIC_ADD(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) /** * Atomically get the value of *ptr * @return value of *ptr */ #define UC_ATOMIC_GET(ptr) UC_ATOMIC_ADD((ptr), 0) /** * 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)) /** Issue a full (hardware) memory barrier. * preventing the cpu to move load/stores across * the barrier. (This implies a compiler barrier as well) * */ #define UC_MEM_BARRIER() __sync_synchronize() /** Issue a full compiler/software only memory barrier * preventing compiler optimization to move load/stores across * the barrier. * */ #define UC_COMPILER_BARRIER() __asm__ __volatile__("": : :"memory") #else #error "No atomic operations implemented for this compiler" #endif #endif