]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Retire AllThreadsShouldSleep flag
[stockfish] / src / search.cpp
index d8a6e1c8dcd6d5ed10a39de62ab675f66351baf8..861eaacabef8c90f190b5ee96c9ba9a8db8fc50b 100644 (file)
@@ -82,7 +82,7 @@ namespace {
     bool available_thread_exists(int master) const;
     bool thread_is_available(int slave, int master) const;
     bool thread_should_stop(int threadID) const;
-    void wake_sleeping_threads();
+    void wake_sleeping_thread(int threadID);
     void put_threads_to_sleep();
     void idle_loop(int threadID, SplitPoint* sp);
 
@@ -94,17 +94,10 @@ namespace {
     friend void poll();
 
     int ActiveThreads;
-    volatile bool AllThreadsShouldExit, AllThreadsShouldSleep;
+    volatile bool AllThreadsShouldExit;
     Thread threads[MAX_THREADS];
-
     Lock MPLock, WaitLock;
-
-#if !defined(_MSC_VER)
-    pthread_cond_t WaitCond;
-#else
-    HANDLE SitIdleEvent[MAX_THREADS];
-#endif
-
+    WaitCondition WaitCond[MAX_THREADS];
   };
 
 
@@ -472,8 +465,9 @@ bool think(const Position& pos, bool infinite, bool ponder, int time[], int incr
       init_eval(ThreadsMgr.active_threads());
   }
 
-  // Wake up sleeping threads
-  ThreadsMgr.wake_sleeping_threads();
+  // Wake up needed threads
+  for (int i = 1; i < newActiveThreads; i++)
+      ThreadsMgr.wake_sleeping_thread(i);
 
   // Set thinking time
   int myTime = time[pos.side_to_move()];
@@ -2230,7 +2224,7 @@ split_point_start: // At split points actual search starts from here
 
         // If we are not thinking, wait for a condition to be signaled
         // instead of wasting CPU time polling for work.
-        while (AllThreadsShouldSleep || threadID >= ActiveThreads)
+        while (threadID >= ActiveThreads)
         {
             assert(!sp);
             assert(threadID != 0);
@@ -2238,11 +2232,11 @@ split_point_start: // At split points actual search starts from here
 
 #if !defined(_MSC_VER)
             lock_grab(&WaitLock);
-            if (AllThreadsShouldSleep || threadID >= ActiveThreads)
-                pthread_cond_wait(&WaitCond, &WaitLock);
+            if (threadID >= ActiveThreads)
+                pthread_cond_wait(&WaitCond[threadID], &WaitLock);
             lock_release(&WaitLock);
 #else
-            WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
+            WaitForSingleObject(WaitCond[threadID], INFINITE);
 #endif
         }
 
@@ -2253,7 +2247,7 @@ split_point_start: // At split points actual search starts from here
         // If this thread has been assigned work, launch a search
         if (threads[threadID].state == THREAD_WORKISWAITING)
         {
-            assert(!AllThreadsShouldExit && !AllThreadsShouldSleep);
+            assert(!AllThreadsShouldExit);
 
             threads[threadID].state = THREAD_SEARCHING;
 
@@ -2305,19 +2299,15 @@ split_point_start: // At split points actual search starts from here
     volatile int i;
     bool ok;
 
-#if !defined(_MSC_VER)
-    pthread_t pthread[1];
-#endif
-
     // Initialize global locks
     lock_init(&MPLock);
     lock_init(&WaitLock);
 
+    for (i = 0; i < MAX_THREADS; i++)
 #if !defined(_MSC_VER)
-    pthread_cond_init(&WaitCond, NULL);
+        pthread_cond_init(&WaitCond[i], NULL);
 #else
-    for (i = 0; i < MAX_THREADS; i++)
-        SitIdleEvent[i] = CreateEvent(0, FALSE, FALSE, 0);
+        WaitCond[i] = CreateEvent(0, FALSE, FALSE, 0);
 #endif
 
     // Initialize splitPoints[] locks
@@ -2329,10 +2319,9 @@ split_point_start: // At split points actual search starts from here
     AllThreadsShouldExit = false;
 
     // Threads will be put to sleep as soon as created
-    AllThreadsShouldSleep = true;
+    ActiveThreads = 1;
 
     // All threads except the main thread should be initialized to THREAD_AVAILABLE
-    ActiveThreads = 1;
     threads[0].state = THREAD_SEARCHING;
     for (i = 1; i < MAX_THREADS; i++)
         threads[i].state = THREAD_AVAILABLE;
@@ -2342,6 +2331,7 @@ split_point_start: // At split points actual search starts from here
     {
 
 #if !defined(_MSC_VER)
+        pthread_t pthread[1];
         ok = (pthread_create(pthread, NULL, init_thread, (void*)(&i)) == 0);
 #else
         ok = (CreateThread(NULL, 0, init_thread, (LPVOID)(&i), 0, NULL) != NULL);
@@ -2364,14 +2354,15 @@ split_point_start: // At split points actual search starts from here
 
   void ThreadsManager::exit_threads() {
 
-    ActiveThreads = MAX_THREADS;  // Wake up all the threads
-    AllThreadsShouldExit = true;  // Let the woken up threads to exit idle_loop()
-    AllThreadsShouldSleep = true; // Avoid an assert in wake_sleeping_threads()
-    wake_sleeping_threads();
+    AllThreadsShouldExit = true; // Let the woken up threads to exit idle_loop()
+    ActiveThreads = MAX_THREADS; // Avoid any woken up thread comes back to sleep
 
-    // Wait for thread termination
+    // Wake up all the threads and waits for termination
     for (int i = 1; i < MAX_THREADS; i++)
+    {
+        wake_sleeping_thread(i);
         while (threads[i].state != THREAD_TERMINATED) {}
+    }
 
     // Now we can safely destroy the locks
     for (int i = 0; i < MAX_THREADS; i++)
@@ -2380,6 +2371,10 @@ split_point_start: // At split points actual search starts from here
 
     lock_destroy(&WaitLock);
     lock_destroy(&MPLock);
+
+    // Now we can safely destroy the wait conditions
+    for (int i = 0; i < MAX_THREADS; i++)
+        cond_destroy(&WaitCond[i]);
   }
 
 
@@ -2562,28 +2557,21 @@ split_point_start: // At split points actual search starts from here
   }
 
 
-  // wake_sleeping_threads() wakes up all sleeping threads when it is time
+  // wake_sleeping_thread() wakes up all sleeping threads when it is time
   // to start a new search from the root.
 
-  void ThreadsManager::wake_sleeping_threads() {
-
-    assert(AllThreadsShouldSleep);
-    assert(ActiveThreads > 0);
+  void ThreadsManager::wake_sleeping_thread(int threadID) {
 
-    AllThreadsShouldSleep = false;
-
-    if (ActiveThreads == 1)
-        return;
+    assert(threadID > 0);
+    assert(threads[threadID].state == THREAD_SLEEPING);
 
 #if !defined(_MSC_VER)
-    pthread_mutex_lock(&WaitLock);
-    pthread_cond_broadcast(&WaitCond);
-    pthread_mutex_unlock(&WaitLock);
+        pthread_mutex_lock(&WaitLock);
+        pthread_cond_signal(&WaitCond[threadID]);
+        pthread_mutex_unlock(&WaitLock);
 #else
-    for (int i = 1; i < MAX_THREADS; i++)
-        SetEvent(SitIdleEvent[i]);
+        SetEvent(WaitCond[threadID]);
 #endif
-
   }
 
 
@@ -2593,10 +2581,8 @@ split_point_start: // At split points actual search starts from here
 
   void ThreadsManager::put_threads_to_sleep() {
 
-    assert(!AllThreadsShouldSleep);
-
     // This makes the threads to go to sleep
-    AllThreadsShouldSleep = true;
+    ActiveThreads = 1;
   }
 
   /// The RootMoveList class