]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Big Position renaming
[stockfish] / src / thread.cpp
index f2a2c821151aa0219d525be1779e83d86c9dd2d1..4ae268eb77b9ae885554bf4e60c78ef1e2ebf032 100644 (file)
@@ -50,7 +50,7 @@ Thread::Thread(Fn fn) {
   start_fn = fn;
   threadID = Threads.size();
 
-  do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_thinking()
+  do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_searching()
 
   lock_init(sleepLock);
   cond_init(sleepCond);
@@ -154,10 +154,7 @@ void Thread::wait_for_stop_or_ponderhit() {
   Signals.stopOnPonderhit = true;
 
   lock_grab(sleepLock);
-
-  while (!Signals.stop)
-      cond_wait(sleepCond, sleepLock);
-
+  while (!Signals.stop) cond_wait(sleepCond, sleepLock);
   lock_release(sleepLock);
 }
 
@@ -258,8 +255,8 @@ void ThreadsManager::wake_up() const {
 
   for (int i = 0; i < size(); i++)
   {
-      threads[i]->do_sleep = false;
       threads[i]->maxPly = 0;
+      threads[i]->do_sleep = false;
 
       if (!useSleepingThreads)
           threads[i]->wake_up();
@@ -273,7 +270,7 @@ void ThreadsManager::wake_up() const {
 void ThreadsManager::sleep() const {
 
   for (int i = 1; i < size(); i++) // Main thread will go to sleep by itself
-      threads[i]->do_sleep = true; // to avoid a race with start_thinking()
+      threads[i]->do_sleep = true; // to avoid a race with start_searching()
 }
 
 
@@ -312,7 +309,7 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
   assert(beta <= VALUE_INFINITE);
   assert(depth > DEPTH_ZERO);
 
-  int master = pos.thread();
+  int master = pos.this_thread();
   Thread& masterThread = *threads[master];
 
   if (masterThread.splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
@@ -415,19 +412,27 @@ void ThreadsManager::set_timer(int msec) {
 }
 
 
-// ThreadsManager::start_thinking() is used by UI thread to wake up the main
-// thread parked in main_loop() and starting a new search. If async is true
-// then function returns immediately, otherwise caller is blocked waiting for
-// the search to finish.
+// ThreadsManager::wait_for_search_finished() waits for main thread to go to
+// sleep, this means search is finished. Then returns.
+
+void ThreadsManager::wait_for_search_finished() {
 
-void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limits,
-                                    const std::set<Move>& searchMoves, bool async) {
-  Thread& main = *threads.front();
+  Thread* main = threads[0];
+  lock_grab(main->sleepLock);
+  cond_signal(main->sleepCond); // In case is waiting for stop or ponderhit
+  while (!main->do_sleep) cond_wait(sleepCond, main->sleepLock);
+  lock_release(main->sleepLock);
+}
+
+
+// ThreadsManager::start_searching() wakes up the main thread sleeping in
+// main_loop() so to start a new search, then returns immediately.
 
-  lock_grab(main.sleepLock);
+void ThreadsManager::start_searching(const Position& pos, const LimitsType& limits,
+                                     const std::vector<Move>& searchMoves) {
+  wait_for_search_finished();
 
-  while (!main.do_sleep)
-      cond_wait(sleepCond, main.sleepLock); // Wait main thread has finished
+  SearchTime.restart(); // As early as possible
 
   Signals.stopOnPonderhit = Signals.firstRootMove = false;
   Signals.stop = Signals.failedLowAtRoot = false;
@@ -437,36 +442,9 @@ void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limit
   RootMoves.clear();
 
   for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
-      if (searchMoves.empty() || searchMoves.count(ml.move()))
+      if (searchMoves.empty() || count(searchMoves.begin(), searchMoves.end(), ml.move()))
           RootMoves.push_back(RootMove(ml.move()));
 
-  main.do_sleep = false;
-  cond_signal(main.sleepCond); // Wake up main thread and start searching
-
-  if (!async)
-      while (!main.do_sleep)
-          cond_wait(sleepCond, main.sleepLock);
-
-  lock_release(main.sleepLock);
-}
-
-
-// ThreadsManager::stop_thinking() is used by UI thread to raise a stop request
-// and to wait for the main thread finishing the search. We cannot return before
-// main has finished to avoid a crash in case of a 'quit' command.
-
-void ThreadsManager::stop_thinking() {
-
-  Thread& main = *threads.front();
-
-  Search::Signals.stop = true;
-
-  lock_grab(main.sleepLock);
-
-  cond_signal(main.sleepCond); // In case is waiting for stop or ponderhit
-
-  while (!main.do_sleep)
-      cond_wait(sleepCond, main.sleepLock);
-
-  lock_release(main.sleepLock);
+  threads[0]->do_sleep = false;
+  threads[0]->wake_up();
 }