]> git.sesse.net Git - stockfish/commitdiff
Introduce and use wait_for_search_finished()
authorMarco Costalba <mcostalba@gmail.com>
Mon, 26 Mar 2012 15:19:37 +0000 (17:19 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Mon, 26 Mar 2012 17:22:41 +0000 (18:22 +0100)
Helper function that allows us to simplify
the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/benchmark.cpp
src/thread.cpp
src/thread.h
src/uci.cpp

index 4124899b33d4fd7a1b84a36a3eb1490b29509166..9f0ac0c145b220798eaab1449e8b848f3c411b94 100644 (file)
@@ -118,7 +118,7 @@ void benchmark(int argc, char* argv[]) {
       }
       else
       {
       }
       else
       {
-          Threads.start_thinking(pos, limits);
+          Threads.start_searching(pos, limits);
           nodes += Search::RootPosition.nodes_searched();
       }
   }
           nodes += Search::RootPosition.nodes_searched();
       }
   }
index f2a2c821151aa0219d525be1779e83d86c9dd2d1..65d01d9100a874e19db9addea3772bafa735dd69 100644 (file)
@@ -50,7 +50,7 @@ Thread::Thread(Fn fn) {
   start_fn = fn;
   threadID = Threads.size();
 
   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);
 
   lock_init(sleepLock);
   cond_init(sleepCond);
@@ -137,6 +137,7 @@ void Thread::main_loop() {
 void Thread::wake_up() {
 
   lock_grab(sleepLock);
 void Thread::wake_up() {
 
   lock_grab(sleepLock);
+  do_sleep = false;
   cond_signal(sleepCond);
   lock_release(sleepLock);
 }
   cond_signal(sleepCond);
   lock_release(sleepLock);
 }
@@ -154,10 +155,7 @@ void Thread::wait_for_stop_or_ponderhit() {
   Signals.stopOnPonderhit = true;
 
   lock_grab(sleepLock);
   Signals.stopOnPonderhit = true;
 
   lock_grab(sleepLock);
-
-  while (!Signals.stop)
-      cond_wait(sleepCond, sleepLock);
-
+  while (!Signals.stop) cond_wait(sleepCond, sleepLock);
   lock_release(sleepLock);
 }
 
   lock_release(sleepLock);
 }
 
@@ -258,7 +256,6 @@ void ThreadsManager::wake_up() const {
 
   for (int i = 0; i < size(); i++)
   {
 
   for (int i = 0; i < size(); i++)
   {
-      threads[i]->do_sleep = false;
       threads[i]->maxPly = 0;
 
       if (!useSleepingThreads)
       threads[i]->maxPly = 0;
 
       if (!useSleepingThreads)
@@ -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
 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()
 }
 
 
 }
 
 
@@ -415,19 +412,26 @@ void ThreadsManager::set_timer(int msec) {
 }
 
 
 }
 
 
-// ThreadsManager::start_thinking() is used by UI thread to wake up the main
+// 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() {
+
+  Thread* main = threads[0];
+  lock_grab(main->sleepLock);
+  while (!main->do_sleep) cond_wait(sleepCond, main->sleepLock);
+  lock_release(main->sleepLock);
+}
+
+
+// ThreadsManager::start_searching() 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.
 
 // 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.
 
-void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limits,
-                                    const std::set<Move>& searchMoves, bool async) {
-  Thread& main = *threads.front();
-
-  lock_grab(main.sleepLock);
-
-  while (!main.do_sleep)
-      cond_wait(sleepCond, main.sleepLock); // Wait main thread has finished
+void ThreadsManager::start_searching(const Position& pos, const LimitsType& limits,
+                                     const std::set<Move>& searchMoves, bool async) {
+  wait_for_search_finished();
 
   Signals.stopOnPonderhit = Signals.firstRootMove = false;
   Signals.stop = Signals.failedLowAtRoot = false;
 
   Signals.stopOnPonderhit = Signals.firstRootMove = false;
   Signals.stop = Signals.failedLowAtRoot = false;
@@ -440,33 +444,8 @@ void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limit
       if (searchMoves.empty() || searchMoves.count(ml.move()))
           RootMoves.push_back(RootMove(ml.move()));
 
       if (searchMoves.empty() || searchMoves.count(ml.move()))
           RootMoves.push_back(RootMove(ml.move()));
 
-  main.do_sleep = false;
-  cond_signal(main.sleepCond); // Wake up main thread and start searching
+  threads[0]->wake_up(); // Start main thread
 
   if (!async)
 
   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);
+      wait_for_search_finished();
 }
 }
index 04e023067999c63fefb432cfa67cfcda1b1787f5..e6a4cf95744ca0260f2ae11fd7c58aa7a1dae169 100644 (file)
@@ -47,7 +47,6 @@ struct SplitPoint {
   MovePicker* mp;
   SplitPoint* parent;
 
   MovePicker* mp;
   SplitPoint* parent;
 
-
   // Shared data
   Lock lock;
   volatile uint64_t slavesMask;
   // Shared data
   Lock lock;
   volatile uint64_t slavesMask;
@@ -125,9 +124,9 @@ public:
   void read_uci_options();
   bool available_slave_exists(int master) const;
   void set_timer(int msec);
   void read_uci_options();
   bool available_slave_exists(int master) const;
   void set_timer(int msec);
-  void stop_thinking();
-  void start_thinking(const Position& pos, const Search::LimitsType& limits,
-                      const std::set<Move>& = std::set<Move>(), bool async = false);
+  void wait_for_search_finished();
+  void start_searching(const Position& pos, const Search::LimitsType& limits,
+                       const std::set<Move>& = std::set<Move>(), bool async = false);
 
   template <bool Fake>
   Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Move* bestMove,
 
   template <bool Fake>
   Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Move* bestMove,
index 718c78752502b61bd991b25191c94b2d30487e41..987da9c1f879ec7304184092ffa14028c5dab396 100644 (file)
@@ -67,7 +67,10 @@ void uci_loop() {
       is >> skipws >> token;
 
       if (token == "quit" || token == "stop")
       is >> skipws >> token;
 
       if (token == "quit" || token == "stop")
-          Threads.stop_thinking();
+      {
+          Search::Signals.stop = true;
+          Threads.wait_for_search_finished(); // Cannot quit while threads are running
+      }
 
       else if (token == "ponderhit")
       {
 
       else if (token == "ponderhit")
       {
@@ -77,7 +80,10 @@ void uci_loop() {
           Search::Limits.ponder = false;
 
           if (Search::Signals.stopOnPonderhit)
           Search::Limits.ponder = false;
 
           if (Search::Signals.stopOnPonderhit)
-              Threads.stop_thinking();
+          {
+              Search::Signals.stop = true;
+              Threads.wait_for_search_finished();
+          }
       }
 
       else if (token == "go")
       }
 
       else if (token == "go")
@@ -223,7 +229,7 @@ namespace {
     limits.time = time[pos.side_to_move()];
     limits.increment = inc[pos.side_to_move()];
 
     limits.time = time[pos.side_to_move()];
     limits.increment = inc[pos.side_to_move()];
 
-    Threads.start_thinking(pos, limits, searchMoves, true);
+    Threads.start_searching(pos, limits, searchMoves, true);
   }
 
 
   }