]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Polymorphic Thread hierarchy
[stockfish] / src / search.cpp
index a97c73485523eaf13002ddf3c6895b3f7c52d663..0be691246feab592874fc8c52e031fafc13ea4b0 100644 (file)
@@ -227,34 +227,25 @@ void Search::think() {
           << std::endl;
   }
 
-  // Reset and wake up the threads
+  // 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[i].do_sleep = false;
 
-      if (!Threads.use_sleeping_threads())
-          Threads[i].notify_one();
-  }
+  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;
 
-  id_loop(RootPos); // Let's start searching !
+  Threads.timer_thread()->notify_one(); // Wake up the recurring timer
 
-  Threads.set_timer(0); // Stop timer
+  id_loop(RootPos); // Let's start searching !
 
-  // Main thread will go to sleep by itself to avoid a race with start_searching()
-  for (size_t i = 0; i < Threads.size(); i++)
-      if (&Threads[i] != Threads.main_thread())
-          Threads[i].do_sleep = true;
+  Threads.timer_thread()->msec = 0; // Stop the timer
+  Threads.sleepWhileIdle = true; // Send idle threads to sleep
 
   if (Options["Use Search Log"])
   {
@@ -1633,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)
           {
@@ -1657,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();
@@ -1666,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();
 
@@ -1705,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)
           {