From 775f8239d3bec75c8deaf951ab24d3a030b671ee Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 22 Feb 2015 14:40:46 +0100 Subject: [PATCH] Introduce Spinlock class Initialization is more complex than what I'd like due to MSVC compatibility that for some reason does not like: std::atomic_flag lock = ATOMIC_FLAG_INIT; No functional change. --- src/thread.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/thread.h b/src/thread.h index 606f9626..166ea80f 100644 --- a/src/thread.h +++ b/src/thread.h @@ -20,6 +20,7 @@ #ifndef THREAD_H_INCLUDED #define THREAD_H_INCLUDED +#include #include #include #include @@ -69,6 +70,19 @@ struct SplitPoint { }; +/// Spinlock class wraps low level atomic operations to provide spin lock functionality + +class Spinlock { + + std::atomic_flag lock; + +public: + Spinlock() { std::atomic_flag_clear(&lock); } + void acquire() { while (lock.test_and_set(std::memory_order_acquire)) {} } + void release() { lock.clear(std::memory_order_release); } +}; + + /// ThreadBase struct is the base of the hierarchy from where we derive all the /// specialized thread classes. -- 2.39.2