X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fthread.h;h=8f930149f333e764d3525fc9d93fd8f4dba3798b;hb=d71f7070406bc018e3ca868461d77a90eaebfcbc;hp=b4aad5cb7f42321c8954ee641452c5908f9d3cb5;hpb=8725494966f91af42c77d2f81d2c8a7fe1864316;p=stockfish diff --git a/src/thread.h b/src/thread.h index b4aad5cb..8f930149 100644 --- a/src/thread.h +++ b/src/thread.h @@ -40,20 +40,17 @@ const size_t MAX_THREADS = 128; const size_t MAX_SPLITPOINTS_PER_THREAD = 8; const size_t MAX_SLAVES_PER_SPLITPOINT = 4; - -/// Spinlock class wraps low level atomic operations to provide a spin lock - class Spinlock { - - std::atomic_int lock; + std::atomic_int _lock; public: - Spinlock() { lock = 1; } // Init here to workaround a bug with MSVC 2013 - void acquire() { - while (lock.fetch_sub(1, std::memory_order_acquire) != 1) - while (lock.load(std::memory_order_relaxed) <= 0) {} + Spinlock() { _lock = 1; } // Init here to workaround a bug with MSVC 2013 + void lock() { + while (_lock.fetch_sub(1, std::memory_order_acquire) != 1) + for (int cnt = 0; _lock.load(std::memory_order_relaxed) <= 0; ++cnt) + if (cnt >= 10000) std::this_thread::yield(); // Be nice to hyperthreading } - void release() { lock.store(1, std::memory_order_release); } + void unlock() { _lock.store(1, std::memory_order_release); } }; @@ -76,7 +73,7 @@ struct SplitPoint { SplitPoint* parentSplitPoint; // Shared variable data - Spinlock spinlock; + Spinlock mutex; std::bitset slavesMask; volatile bool allSlavesSearching; volatile uint64_t nodes; @@ -130,6 +127,7 @@ struct Thread : public ThreadBase { SplitPoint* volatile activeSplitPoint; volatile size_t splitPointsSize; volatile bool searching; + Spinlock allocMutex; }; @@ -167,7 +165,6 @@ struct ThreadPool : public std::vector { void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&); Depth minimumSplitDepth; - Spinlock spinlock; ConditionVariable sleepCondition; TimerThread* timer; };