]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Refactor ThreadsManager::set_size() functionality
[stockfish] / src / thread.cpp
index 358ced5dec3bf26eb823af4bd82ad8041801ca89..1913aee43ca62601a68000392cad4e844c779fb0 100644 (file)
@@ -36,13 +36,7 @@ namespace { extern "C" {
  // and last thread are special. First one is the main search thread while the
  // last one mimics a timer, they run in main_loop() and timer_loop().
 
-#if defined(_WIN32) || defined(_WIN64)
-  DWORD WINAPI start_routine(LPVOID thread) {
-#else
-  void* start_routine(void* thread) {
-#endif
-
-    Thread* th = (Thread*)thread;
+  long start_routine(Thread* th) {
 
     if (th->threadID == 0)
         th->main_loop();
@@ -179,35 +173,35 @@ void ThreadsManager::read_uci_options() {
   maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
   minimumSplitDepth       = Options["Min Split Depth"] * ONE_PLY;
   useSleepingThreads      = Options["Use Sleeping Threads"];
-}
-
-
-// set_size() changes the number of active threads and raises do_sleep flag for
-// all the unused threads that will go immediately to sleep.
-
-void ThreadsManager::set_size(int cnt) {
-
-  assert(cnt > 0 && cnt < MAX_THREADS);
-
-  activeThreads = cnt;
+  activeThreads           = Options["Threads"];
 
+  // Dynamically allocate pawn and material hash tables according to the
+  // number of active threads. This avoids preallocating memory for all
+  // possible threads if only few are used.
   for (int i = 0; i < MAX_THREADS; i++)
       if (i < activeThreads)
       {
-          // Dynamically allocate pawn and material hash tables according to the
-          // number of active threads. This avoids preallocating memory for all
-          // possible threads if only few are used.
           threads[i].pawnTable.init();
           threads[i].materialTable.init();
           threads[i].maxPly = 0;
+      }
+}
 
-          threads[i].do_sleep = false;
 
-          if (!useSleepingThreads)
-              threads[i].wake_up();
-      }
-      else
-          threads[i].do_sleep = true;
+void ThreadsManager::wake_up() {
+
+  for (int i = 0; i < activeThreads; i++)
+  {
+      threads[i].do_sleep = false;
+      threads[i].wake_up();
+  }
+}
+
+
+void ThreadsManager::sleep() {
+
+  for (int i = 0; i < activeThreads; i++)
+      threads[i].do_sleep = true;
 }
 
 
@@ -221,15 +215,6 @@ void ThreadsManager::init() {
   cond_init(sleepCond);
   lock_init(splitLock);
 
-  for (int i = 0; i <= MAX_THREADS; i++)
-  {
-      lock_init(threads[i].sleepLock);
-      cond_init(threads[i].sleepCond);
-
-      for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
-          lock_init(threads[i].splitPoints[j].lock);
-  }
-
   // Allocate main thread tables to call evaluate() also when not searching
   threads[0].pawnTable.init();
   threads[0].materialTable.init();
@@ -241,6 +226,12 @@ void ThreadsManager::init() {
       threads[i].do_sleep = (i != 0); // Avoid a race with start_thinking()
       threads[i].threadID = i;
 
+      lock_init(threads[i].sleepLock);
+      cond_init(threads[i].sleepCond);
+
+      for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
+          lock_init(threads[i].splitPoints[j].lock);
+
       if (!thread_create(threads[i].handle, start_routine, threads[i]))
       {
           std::cerr << "Failed to create thread number " << i << std::endl;
@@ -302,7 +293,7 @@ bool ThreadsManager::available_slave_exists(int master) const {
 template <bool Fake>
 Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
                             Value bestValue, Move* bestMove, Depth depth,
-                            Move threatMove, int moveCount, MovePicker *mp, int nodeType) {
+                            Move threatMove, int moveCount, MovePickermp, int nodeType) {
   assert(pos.pos_is_ok());
   assert(bestValue > -VALUE_INFINITE);
   assert(bestValue <= alpha);