]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Retire Thread::TERMINATED
[stockfish] / src / search.cpp
index 4864b1cb151f70e2e1fc1849e198c9abc24802a6..9992049efdacc4792138348d63febaed162bfc84 100644 (file)
@@ -409,7 +409,8 @@ bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) {
   read_evaluation_uci_options(pos.side_to_move());
   Threads.read_uci_options();
 
-  // If needed allocate pawn and material hash tables and adjust TT size
+  // Allocate pawn and material hash tables if number of active threads
+  // increased and set a new TT size if changed.
   Threads.init_hash_tables();
   TT.set_size(Options["Hash"].value<int>());
 
@@ -2129,71 +2130,75 @@ split_point_start: // At split points actual search starts from here
 } // namespace
 
 
-// ThreadsManager::idle_loop() is where the threads are parked when they have no work
-// to do. The parameter 'sp', if non-NULL, is a pointer to an active SplitPoint
-// object for which the current thread is the master.
+// Little helper used by idle_loop() to check that all the slaves of a
+// master thread have finished searching.
 
-void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {
+static bool all_slaves_finished(SplitPoint* sp) {
 
-  assert(threadID >= 0 && threadID < MAX_THREADS);
+  assert(sp);
+
+  for (int i = 0; i < Threads.size(); i++)
+      if (sp->is_slave[i])
+          return false;
+
+  return true;
+}
 
-  int i;
-  bool allFinished;
+
+// Thread::idle_loop() is where the thread is parked when it has no work to do.
+// The parameter 'sp', if non-NULL, is a pointer to an active SplitPoint object
+// for which the thread is the master.
+
+void Thread::idle_loop(SplitPoint* sp) {
 
   while (true)
   {
-      // Slave threads can exit as soon as AllThreadsShouldExit raises,
-      // master should exit as last one.
-      if (allThreadsShouldExit)
-      {
-          assert(!sp);
-          threads[threadID].state = Thread::TERMINATED;
-          return;
-      }
-
-      // If we are not thinking, wait for a condition to be signaled
+      // If we are not searching, wait for a condition to be signaled
       // instead of wasting CPU time polling for work.
-      while (   threadID >= activeThreads
-             || threads[threadID].state == Thread::INITIALIZING
-             || (useSleepingThreads && threads[threadID].state == Thread::AVAILABLE))
+      while (   do_sleep
+             || do_terminate
+             || (Threads.use_sleeping_threads() && state == Thread::AVAILABLE))
       {
-          assert(!sp || useSleepingThreads);
-          assert(threadID != 0 || useSleepingThreads);
-
-          if (threads[threadID].state == Thread::INITIALIZING)
-              threads[threadID].state = Thread::AVAILABLE;
+          assert((!sp && threadID) || Threads.use_sleeping_threads());
 
           // Grab the lock to avoid races with Thread::wake_up()
-          lock_grab(&threads[threadID].sleepLock);
+          lock_grab(&sleepLock);
 
-          // If we are master and all slaves have finished do not go to sleep
-          for (i = 0; sp && i < activeThreads && !sp->is_slave[i]; i++) {}
-          allFinished = (i == activeThreads);
+          // Slave thread should exit as soon as do_terminate flag raises
+          if (do_terminate)
+          {
+              assert(!sp);
+              lock_release(&sleepLock);
+              return;
+          }
 
-          if (allFinished || allThreadsShouldExit)
+          // If we are master and all slaves have finished don't go to sleep
+          if (sp && all_slaves_finished(sp))
           {
-              lock_release(&threads[threadID].sleepLock);
+              lock_release(&sleepLock);
               break;
           }
 
-          // Do sleep here after retesting sleep conditions
-          if (threadID >= activeThreads || threads[threadID].state == Thread::AVAILABLE)
-              cond_wait(&threads[threadID].sleepCond, &threads[threadID].sleepLock);
+          // Do sleep after retesting sleep conditions under lock protection, in
+          // particular we need to avoid a deadlock in case a master thread has,
+          // in the meanwhile, allocated us and sent the wake_up() call before we
+          // had the chance to grab the lock.
+          if (do_sleep || state == Thread::AVAILABLE)
+              cond_wait(&sleepCond, &sleepLock);
 
-          lock_release(&threads[threadID].sleepLock);
+          lock_release(&sleepLock);
       }
 
       // If this thread has been assigned work, launch a search
-      if (threads[threadID].state == Thread::WORKISWAITING)
+      if (state == Thread::WORKISWAITING)
       {
-          assert(!allThreadsShouldExit);
+          assert(!do_terminate);
 
-          threads[threadID].state = Thread::SEARCHING;
+          state = Thread::SEARCHING;
 
           // Copy split point position and search stack and call search()
-          // with SplitPoint template parameter set to true.
           SearchStack ss[PLY_MAX_PLUS_2];
-          SplitPoint* tsp = threads[threadID].splitPoint;
+          SplitPoint* tsp = splitPoint;
           Position pos(*tsp->pos, threadID);
 
           memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack));
@@ -2208,33 +2213,26 @@ void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {
           else
               assert(false);
 
-          assert(threads[threadID].state == Thread::SEARCHING);
+          assert(state == Thread::SEARCHING);
 
-          threads[threadID].state = Thread::AVAILABLE;
+          state = Thread::AVAILABLE;
 
           // Wake up master thread so to allow it to return from the idle loop in
           // case we are the last slave of the split point.
-          if (   useSleepingThreads
+          if (   Threads.use_sleeping_threads()
               && threadID != tsp->master
-              && threads[tsp->master].state == Thread::AVAILABLE)
-              threads[tsp->master].wake_up();
+              && Threads[tsp->master].state == Thread::AVAILABLE)
+              Threads[tsp->master].wake_up();
       }
 
       // If this thread is the master of a split point and all slaves have
       // finished their work at this split point, return from the idle loop.
-      for (i = 0; sp && i < activeThreads && !sp->is_slave[i]; i++) {}
-      allFinished = (i == activeThreads);
-
-      if (allFinished)
+      if (sp && all_slaves_finished(sp))
       {
-          // Because sp->slaves[] is reset under lock protection,
+          // Because sp->is_slave[] is reset under lock protection,
           // be sure sp->lock has been released before to return.
           lock_grab(&(sp->lock));
           lock_release(&(sp->lock));
-
-          // In helpful master concept a master can help only a sub-tree, and
-          // because here is all finished is not possible master is booked.
-          assert(threads[threadID].state == Thread::AVAILABLE);
           return;
       }
   }