]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Rename available_to()
[stockfish] / src / thread.cpp
index c232b849babacc66ef6e262322678b18883a37e5..ff52576b062785c49f8eae6bfa360b0380dbae71 100644 (file)
@@ -81,7 +81,8 @@ void ThreadBase::wait_for(volatile const bool& condition) {
 Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in MSVC
 
   searching = false;
-  maxPly = splitPointsSize = 0;
+  maxPly = 0;
+  splitPointsSize = 0;
   activeSplitPoint = nullptr;
   activePosition = nullptr;
   idx = Threads.size(); // Starts from 0
@@ -101,25 +102,24 @@ bool Thread::cutoff_occurred() const {
 }
 
 
-// Thread::available_to() checks whether the thread is available to help the
-// thread 'master' at a split point. An obvious requirement is that thread must
-// be idle. With more than two threads, this is not sufficient: If the thread is
-// the master of some split point, it is only available as a slave to the slaves
-// which are busy searching the split point at the top of slave's split point
-// stack (the "helpful master concept" in YBWC terminology).
+// Thread::can_join() checks whether the thread is available to join the split
+// point 'sp'. An obvious requirement is that thread must be idle. With more than
+// two threads, this is not sufficient: If the thread is the master of some split
+// point, it is only available as a slave for the split points below his active
+// one (the "helpful master" concept in YBWC terminology).
 
-bool Thread::available_to(const Thread* master) const {
+bool Thread::can_join(const SplitPoint* sp) const {
 
   if (searching)
       return false;
 
   // Make a local copy to be sure it doesn't become zero under our feet while
   // testing next condition and so leading to an out of bounds access.
-  const int size = splitPointsSize;
+  const size_t size = splitPointsSize;
 
   // No split points means that the thread is available as a slave for any
   // other thread otherwise apply the "helpful master" concept if possible.
-  return !size || splitPoints[size - 1].slavesMask.test(master->idx);
+  return !size || splitPoints[size - 1].slavesMask.test(sp->master->idx);
 }
 
 
@@ -144,7 +144,7 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
   // Pick and init the next available split point
   SplitPoint& sp = splitPoints[splitPointsSize];
 
-  sp.masterThread = this;
+  sp.master = this;
   sp.parentSplitPoint = activeSplitPoint;
   sp.slavesMask = 0, sp.slavesMask.set(idx);
   sp.depth = depth;
@@ -164,8 +164,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
   // Try to allocate available threads and ask them to start searching setting
   // 'searching' flag. This must be done under lock protection to avoid concurrent
   // allocation of the same slave by another master.
-  Threads.mutex.lock();
-  sp.mutex.lock();
+  Threads.spinlock.acquire();
+  sp.spinlock.acquire();
 
   sp.allSlavesSearching = true; // Must be set under lock protection
   ++splitPointsSize;
@@ -174,10 +174,11 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
 
   Thread* slave;
 
-  while ((slave = Threads.available_slave(this)) != nullptr)
+  while (    sp.slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT
+         && (slave = Threads.available_slave(activeSplitPoint)) != nullptr)
   {
       sp.slavesMask.set(slave->idx);
-      slave->activeSplitPoint = &sp;
+      slave->activeSplitPoint = activeSplitPoint;
       slave->searching = true; // Slave leaves idle_loop()
       slave->notify_one(); // Could be sleeping
   }
@@ -186,8 +187,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
   // 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();
-  Threads.mutex.unlock();
+  sp.spinlock.release();
+  Threads.spinlock.release();
 
   Thread::idle_loop(); // Force a call to base class idle_loop()
 
@@ -200,8 +201,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
   // We have returned from the idle loop, which means that all threads are
   // finished. Note that setting 'searching' and decreasing splitPointsSize must
   // be done under lock protection to avoid a race with Thread::available_to().
-  Threads.mutex.lock();
-  sp.mutex.lock();
+  Threads.spinlock.acquire();
+  sp.spinlock.acquire();
 
   searching = true;
   --splitPointsSize;
@@ -211,8 +212,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
   *bestMove = sp.bestMove;
   *bestValue = sp.bestValue;
 
-  sp.mutex.unlock();
-  Threads.mutex.unlock();
+  sp.spinlock.release();
+  Threads.spinlock.release();
 }
 
 
@@ -323,12 +324,12 @@ void ThreadPool::read_uci_options() {
 
 
 // ThreadPool::available_slave() tries to find an idle thread which is available
-// as a slave for the thread 'master'.
+// to join SplitPoint 'sp'.
 
-Thread* ThreadPool::available_slave(const Thread* master) const {
+Thread* ThreadPool::available_slave(const SplitPoint* sp) const {
 
   for (Thread* th : *this)
-      if (th->available_to(master))
+      if (th->can_join(sp))
           return th;
 
   return nullptr;
@@ -351,7 +352,7 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
                                 StateStackPtr& states) {
   wait_for_think_finished();
 
-  SearchTime = Time::now(); // As early as possible
+  SearchTime = now(); // As early as possible
 
   Signals.stopOnPonderhit = Signals.firstRootMove = false;
   Signals.stop = Signals.failedLowAtRoot = false;
@@ -365,10 +366,10 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
       assert(!states.get());
   }
 
-  for (const ExtMove& ms : MoveList<LEGAL>(pos))
+  for (const auto& m : MoveList<LEGAL>(pos))
       if (   limits.searchmoves.empty()
-          || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), ms.move))
-          RootMoves.push_back(RootMove(ms.move));
+          || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
+          RootMoves.push_back(RootMove(m));
 
   main()->thinking = true;
   main()->notify_one(); // Starts main thread