diff mbox

[kvm-unit-tests,v2,1/3] lib: provide generic spinlock

Message ID 20170515110348.30449-2-david@redhat.com
State Accepted
Headers show

Commit Message

David Hildenbrand May 15, 2017, 11:03 a.m. UTC
Let's provide a basic lock implementation that should work on most
architectures.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 lib/asm-generic/spinlock.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/lib/asm-generic/spinlock.h b/lib/asm-generic/spinlock.h
index 3141744..31fa510 100644
--- a/lib/asm-generic/spinlock.h
+++ b/lib/asm-generic/spinlock.h
@@ -1,4 +1,18 @@ 
 #ifndef _ASM_GENERIC_SPINLOCK_H_
 #define _ASM_GENERIC_SPINLOCK_H_
-#error need architecture specific asm/spinlock.h
+
+struct spinlock {
+    unsigned int v;
+};
+
+static inline void spin_lock(struct spinlock *lock)
+{
+	while (__sync_lock_test_and_set(&lock->v, 1));
+}
+
+static inline void spin_unlock(struct spinlock *lock)
+{
+	__sync_lock_release(&lock->v);
+}
+
 #endif