diff mbox

[kvm-unit-tests,3/8] lib/asm-generic: add atomic.h

Message ID 1483088160-6714-4-git-send-email-peterx@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Peter Xu Dec. 30, 2016, 8:55 a.m. UTC
Provide some gcc-builtin atomic ops.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 lib/asm-generic/atomic.h | 21 +++++++++++++++++++++
 lib/x86/atomic.h         |  2 ++
 2 files changed, 23 insertions(+)
 create mode 100644 lib/asm-generic/atomic.h
diff mbox

Patch

diff --git a/lib/asm-generic/atomic.h b/lib/asm-generic/atomic.h
new file mode 100644
index 0000000..26b645a
--- /dev/null
+++ b/lib/asm-generic/atomic.h
@@ -0,0 +1,21 @@ 
+#ifndef __ASM_GENERIC_ATOMIC_H__
+#define __ASM_GENERIC_ATOMIC_H__
+
+/* From QEMU include/qemu/atomic.h */
+#define atomic_fetch_inc(ptr)  __sync_fetch_and_add(ptr, 1)
+#define atomic_fetch_dec(ptr)  __sync_fetch_and_add(ptr, -1)
+#define atomic_fetch_add(ptr, n) __sync_fetch_and_add(ptr, n)
+#define atomic_fetch_sub(ptr, n) __sync_fetch_and_sub(ptr, n)
+#define atomic_fetch_and(ptr, n) __sync_fetch_and_and(ptr, n)
+#define atomic_fetch_or(ptr, n) __sync_fetch_and_or(ptr, n)
+#define atomic_fetch_xor(ptr, n) __sync_fetch_and_xor(ptr, n)
+
+#define atomic_inc_fetch(ptr)  __sync_add_and_fetch(ptr, 1)
+#define atomic_dec_fetch(ptr)  __sync_add_and_fetch(ptr, -1)
+#define atomic_add_fetch(ptr, n) __sync_add_and_fetch(ptr, n)
+#define atomic_sub_fetch(ptr, n) __sync_sub_and_fetch(ptr, n)
+#define atomic_and_fetch(ptr, n) __sync_and_and_fetch(ptr, n)
+#define atomic_or_fetch(ptr, n) __sync_or_and_fetch(ptr, n)
+#define atomic_xor_fetch(ptr, n) __sync_xor_and_fetch(ptr, n)
+
+#endif
diff --git a/lib/x86/atomic.h b/lib/x86/atomic.h
index de2f033..c9ce489 100644
--- a/lib/x86/atomic.h
+++ b/lib/x86/atomic.h
@@ -1,6 +1,8 @@ 
 #ifndef __ATOMIC_H
 #define __ATOMIC_H
 
+#include "asm-generic/atomic.h"
+
 typedef struct {
 	volatile int counter;
 } atomic_t;