template<>
void MovePicker::score<QUIETS>() {
- Square prevMoveSq = to_sq((ss-1)->currentMove);
- Piece prevMovePiece = pos.piece_on(prevMoveSq);
- const HistoryStats &cmh = counterMovesHistory[prevMovePiece][prevMoveSq];
+
+ Square prevSq = to_sq((ss-1)->currentMove);
+ const HistoryStats& cmh = counterMovesHistory[pos.piece_on(prevSq)][prevSq];
for (auto& m : *this)
m.value = history[pos.moved_piece(m)][to_sq(m)]
continue;
moveCount = ++splitPoint->moveCount;
- splitPoint->mutex.unlock();
+ splitPoint->spinlock.release();
}
else
++moveCount;
&& moveCount >= FutilityMoveCounts[improving][depth])
{
if (SpNode)
- splitPoint->mutex.lock();
+ splitPoint->spinlock.acquire();
continue;
}
if (SpNode)
{
- splitPoint->mutex.lock();
+ splitPoint->spinlock.acquire();
if (bestValue > splitPoint->bestValue)
splitPoint->bestValue = bestValue;
}
if (predictedDepth < 4 * ONE_PLY && pos.see_sign(move) < VALUE_ZERO)
{
if (SpNode)
- splitPoint->mutex.lock();
+ splitPoint->spinlock.acquire();
continue;
}
// Step 18. Check for new best move
if (SpNode)
{
- splitPoint->mutex.lock();
+ splitPoint->spinlock.acquire();
bestValue = splitPoint->bestValue;
alpha = splitPoint->alpha;
}
std::memcpy(ss-2, sp->ss-2, 5 * sizeof(Stack));
ss->splitPoint = sp;
- sp->mutex.lock();
+ sp->spinlock.acquire();
assert(activePosition == nullptr);
// After releasing the lock we can't access any SplitPoint related data
// in a safe way because it could have been released under our feet by
// the sp master.
- sp->mutex.unlock();
+ sp->spinlock.release();
// Try to late join to another split point if none of its slaves has
// already finished.
sp = bestSp;
// Recheck the conditions under lock protection
- sp->mutex.lock();
+ sp->spinlock.acquire();
if ( sp->allSlavesSearching
&& sp->slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT)
{
- allocMutex.lock();
+ spinlock.acquire();
if (can_join(sp))
{
searching = true;
}
- allocMutex.unlock();
+ spinlock.release();
}
- sp->mutex.unlock();
+ sp->spinlock.release();
}
}
}
{
SplitPoint& sp = th->splitPoints[i];
- sp.mutex.lock();
+ sp.spinlock.acquire();
nodes += sp.nodes;
if (sp.slavesMask.test(idx) && Threads[idx]->activePosition)
nodes += Threads[idx]->activePosition->nodes_searched();
- sp.mutex.unlock();
+ sp.spinlock.release();
}
if (nodes >= Limits.nodes)
// Pick and init the next available split point
SplitPoint& sp = splitPoints[splitPointsSize];
- sp.mutex.lock(); // No contention here until we don't increment splitPointsSize
+ sp.spinlock.acquire(); // No contention here until we don't increment splitPointsSize
sp.master = this;
sp.parentSplitPoint = activeSplitPoint;
while ( sp.slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT
&& (slave = Threads.available_slave(&sp)) != nullptr)
{
- slave->allocMutex.lock();
+ slave->spinlock.acquire();
if (slave->can_join(activeSplitPoint))
{
slave->searching = true;
}
- slave->allocMutex.unlock();
+ slave->spinlock.release();
}
// Everything is set up. The master thread enters the idle loop, from which
// it will instantly launch a search, because its 'searching' flag is set.
// The thread will return from the idle loop when all slaves have finished
// their work at this split point.
- sp.mutex.unlock();
+ sp.spinlock.release();
Thread::idle_loop(); // Force a call to base class idle_loop()
// We have returned from the idle loop, which means that all threads are
// finished. Note that decreasing splitPointsSize must be done under lock
// protection to avoid a race with Thread::can_join().
- sp.mutex.lock();
+ sp.spinlock.acquire();
--splitPointsSize;
activeSplitPoint = sp.parentSplitPoint;
*bestMove = sp.bestMove;
*bestValue = sp.bestValue;
- sp.mutex.unlock();
+ sp.spinlock.release();
}
const size_t MAX_SLAVES_PER_SPLITPOINT = 4;
class Spinlock {
- std::atomic_int _lock;
+
+ std::atomic_int lock;
public:
- 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)
+ Spinlock() { lock = 1; } // Init here to workaround a bug with MSVC 2013
+ void acquire() {
+ 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 unlock() { _lock.store(1, std::memory_order_release); }
+ void release() { lock.store(1, std::memory_order_release); }
};
SplitPoint* parentSplitPoint;
// Shared variable data
- Spinlock mutex;
+ Spinlock spinlock;
std::bitset<MAX_THREADS> slavesMask;
volatile bool allSlavesSearching;
volatile uint64_t nodes;
std::thread nativeThread;
Mutex mutex;
+ Spinlock spinlock;
ConditionVariable sleepCondition;
volatile bool exit = false;
};
SplitPoint* volatile activeSplitPoint;
volatile size_t splitPointsSize;
volatile bool searching;
- Spinlock allocMutex;
};