diff --git a/include/ucore/atomic.h b/include/ucore/atomic.h new file mode 100644 index 0000000..c5bbe1d --- /dev/null +++ b/include/ucore/atomic.h @@ -0,0 +1,44 @@ +#ifndef UC_ATOMIC_H_ +#define UC_ATOMIC_H_ + +#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 to *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_ADD((ptr), -1) + +/** + * Atomically increment *ptr + * @return *ptr before the increment + */ +#define UC_ATOMIC_INC(ptr) UC_ATOMIC_SUB((ptr), 1) + + +/** + * UC_ATOMIC_CAS(p, 12, + * 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