]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Be sure threads are woken in wake_sleeping_threads()
[stockfish] / src / search.cpp
index ba9b947845b6f867d297632b9ddd7dce3cc9cc69..22bc0156705121a9a02adfcd8b59c1ff66e21997 100644 (file)
@@ -566,9 +566,6 @@ void init_threads() {
   pthread_t pthread[1];
 #endif
 
-  for (i = 0; i < THREAD_MAX; i++)
-      Threads[i].activeSplitPoints = 0;
-
   // Initialize global locks
   lock_init(&MPLock, NULL);
   lock_init(&IOLock, NULL);
@@ -591,12 +588,7 @@ void init_threads() {
 
   // All threads except the main thread should be initialized to idle state
   for (i = 1; i < THREAD_MAX; i++)
-  {
-      Threads[i].stop = false;
-      Threads[i].workIsWaiting = false;
       Threads[i].idle = true;
-      Threads[i].running = false;
-  }
 
   // Launch the helper threads
   for (i = 1; i < THREAD_MAX; i++)
@@ -2801,9 +2793,12 @@ namespace {
 #else
             WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
 #endif
-            Threads[threadID].sleeping = false;
         }
 
+        // Out of the while loop to avoid races in case thread is woken up but
+        // while condition still holds true so that is put to sleep again.
+        Threads[threadID].sleeping = false;
+
       // If this thread has been assigned work, launch a search
       if (Threads[threadID].workIsWaiting)
       {
@@ -3059,6 +3054,8 @@ namespace {
     {
         for (int i = 1; i < ActiveThreads; i++)
         {
+            assert(Threads[i].sleeping == true);
+
             Threads[i].idle = true;
             Threads[i].workIsWaiting = false;
         }
@@ -3071,6 +3068,10 @@ namespace {
       for (int i = 1; i < THREAD_MAX; i++)
           SetEvent(SitIdleEvent[i]);
 #endif
+
+      // Wait for the threads to be all woken up
+      for (int i = 1; i < ActiveThreads; i++)
+           while (Threads[i].sleeping);
     }
   }