Add header for simple atomic operations

This commit is contained in:
Nils O. Selåsdal
2013-06-17 20:28:15 +02:00
parent f79f41fb62
commit 7d4f1f16c2
+44
View File
@@ -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