From 7d4f1f16c26d7905e50ba97385040de0e37851ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Mon, 17 Jun 2013 20:28:15 +0200 Subject: [PATCH] Add header for simple atomic operations --- include/ucore/atomic.h | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 include/ucore/atomic.h 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