]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Polymorphic Thread hierarchy
[stockfish] / src / search.cpp
index 002f0500ac8e21ce3c52179d59f414239eb5d69f..0be691246feab592874fc8c52e031fafc13ea4b0 100644 (file)
@@ -227,22 +227,25 @@ void Search::think() {
           << std::endl;
   }
 
-  Threads.wake_up();
+  // Reset the threads, still sleeping: will be wake up at split time
+  for (size_t i = 0; i < Threads.size(); i++)
+      Threads[i].maxPly = 0;
+
+  Threads.sleepWhileIdle = Options["Use Sleeping Threads"];
 
   // Set best timer interval to avoid lagging under time pressure. Timer is
   // used to check for remaining available thinking time.
-  if (Limits.use_time_management())
-      Threads.set_timer(std::min(100, std::max(TimeMgr.available_time() / 16,
-                                               TimerResolution)));
-  else if (Limits.nodes)
-      Threads.set_timer(2 * TimerResolution);
-  else
-      Threads.set_timer(100);
+  Threads.timer_thread()->msec =
+  Limits.use_time_management() ? std::min(100, std::max(TimeMgr.available_time() / 16, TimerResolution)) :
+                  Limits.nodes ? 2 * TimerResolution
+                               : 100;
+
+  Threads.timer_thread()->notify_one(); // Wake up the recurring timer
 
   id_loop(RootPos); // Let's start searching !
 
-  Threads.set_timer(0); // Stop timer
-  Threads.sleep();
+  Threads.timer_thread()->msec = 0; // Stop the timer
+  Threads.sleepWhileIdle = true; // Send idle threads to sleep
 
   if (Options["Use Search Log"])
   {
@@ -262,12 +265,14 @@ void Search::think() {
 finalize:
 
   // When we reach max depth we arrive here even without Signals.stop is raised,
-  // but if we are pondering or in infinite search, we shouldn't print the best
-  // move before we are told to do so.
+  // but if we are pondering or in infinite search, according to UCI protocol,
+  // we shouldn't print the best move before the GUI sends a "stop" or "ponderhit"
+  // command. We simply wait here until GUI sends one of those commands (that
+  // raise Signals.stop).
   if (!Signals.stop && (Limits.ponder || Limits.infinite))
   {
       Signals.stopOnPonderhit = true;
-      RootPos.this_thread()->wait_for_stop();
+      RootPos.this_thread()->wait_for(Signals.stop);
   }
 
   // Best move could be MOVE_NONE when searching on a stalemate position
@@ -1619,9 +1624,7 @@ void Thread::idle_loop() {
   {
       // If we are not searching, wait for a condition to be signaled
       // instead of wasting CPU time polling for work.
-      while (   do_sleep
-             || do_exit
-             || (!is_searching && Threads.use_sleeping_threads()))
+      while (do_exit || (!is_searching && Threads.sleepWhileIdle))
       {
           if (do_exit)
           {
@@ -1643,7 +1646,7 @@ void Thread::idle_loop() {
           // 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 || !is_searching)
+          if (!is_searching && Threads.sleepWhileIdle)
               sleepCondition.wait(mutex);
 
           mutex.unlock();
@@ -1652,7 +1655,7 @@ void Thread::idle_loop() {
       // If this thread has been assigned work, launch a search
       if (is_searching)
       {
-          assert(!do_sleep && !do_exit);
+          assert(!do_exit);
 
           Threads.mutex.lock();
 
@@ -1691,7 +1694,7 @@ void Thread::idle_loop() {
 
           // 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 (    Threads.use_sleeping_threads()
+          if (    Threads.sleepWhileIdle
               &&  this != sp->master
               && !sp->slavesMask)
           {