]> git.sesse.net Git - stockfish/blobdiff - src/thread.h
Introduce Spinlock class
[stockfish] / src / thread.h
index 54083d2e5c83af994e0c3f647dc61eacd51d44cf..166ea80f65a8af4ec6dca96ada543142873e71c4 100644 (file)
@@ -20,6 +20,7 @@
 #ifndef THREAD_H_INCLUDED
 #define THREAD_H_INCLUDED
 
+#include <atomic>
 #include <bitset>
 #include <condition_variable>
 #include <mutex>
@@ -46,7 +47,7 @@ struct SplitPoint {
   // Const data after split point has been setup
   const Position* pos;
   Search::Stack* ss;
-  Thread* masterThread;
+  Thread* master;
   Depth depth;
   Value beta;
   int nodeType;
@@ -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.