From: Marco Costalba Date: Sun, 22 Feb 2015 13:40:46 +0000 (+0100) Subject: Introduce Spinlock class X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=775f8239d3bec75c8deaf951ab24d3a030b671ee;hp=098f645d26675bcf2180b290be77fe64a63de3ae 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. --- 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.