]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Retire now unused input_available()
[stockfish] / src / search.cpp
index d55a1e1743db323aa109a86a08676e2bd5da657a..320f480c2df854ac575ab4f4776f120d5c933a84 100644 (file)
@@ -197,7 +197,6 @@ namespace {
   bool connected_threat(const Position& pos, Move m, Move threat);
   Value refine_eval(const TTEntry* tte, Value defaultEval, int ply);
   void update_history(const Position& pos, Move move, Depth depth, Move movesSearched[], int moveCount);
-  void update_gains(const Position& pos, Move move, Value before, Value after);
   void do_skill_level(Move* best, Move* ponder);
 
   int current_search_time(int set = 0);
@@ -440,6 +439,10 @@ bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) {
           << endl;
   }
 
+  // Start async mode to catch UCI commands sent to us while searching,
+  // like "quit", "stop", etc.
+  Threads.start_listener();
+
   // We're ready to start thinking. Call the iterative deepening loop function
   Move ponderMove = MOVE_NONE;
   Move bestMove = id_loop(pos, searchMoves, &ponderMove);
@@ -463,6 +466,9 @@ bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) {
   // This makes all the threads to go to sleep
   Threads.set_size(1);
 
+  // From now on any UCI command will be read in-sync with Threads.getline()
+  Threads.stop_listener();
+
   // If we are pondering or in infinite search, we shouldn't print the
   // best move before we are told to do so.
   if (!StopRequest && (Limits.ponder || Limits.infinite))
@@ -504,7 +510,7 @@ namespace {
     *ponderMove = bestMove = easyMove = skillBest = skillPonder = MOVE_NONE;
     depth = aspirationDelta = 0;
     value = alpha = -VALUE_INFINITE, beta = VALUE_INFINITE;
-    ss->currentMove = MOVE_NULL; // Hack to skip update_gains()
+    ss->currentMove = MOVE_NULL; // Hack to skip update gains
 
     // Moves to search are verified and copied
     Rml.init(pos, searchMoves);
@@ -552,7 +558,7 @@ namespace {
             // research with bigger window until not failing high/low anymore.
             do {
                 // Search starts from ss+1 to allow referencing (ss-1). This is
-                // needed by update_gains() and ss copy when splitting at Root.
+                // needed by update gains and ss copy when splitting at Root.
                 value = search<Root>(pos, ss+1, alpha, beta, depth * ONE_PLY);
 
                 // Bring to front the best move. It is critical that sorting is
@@ -821,8 +827,17 @@ namespace {
         TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, ss->evalMargin);
     }
 
-    // Save gain for the parent non-capture move
-    update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
+    // Update gain for the parent non-capture move given the static position
+    // evaluation before and after the move.
+    if (   (move = (ss-1)->currentMove) != MOVE_NULL
+        && (ss-1)->eval != VALUE_NONE
+        && ss->eval != VALUE_NONE
+        && pos.captured_piece_type() == PIECE_TYPE_NONE
+        && !is_special(move))
+    {
+        Square to = move_to(move);
+        H.update_gain(pos.piece_on(to), to, -(ss-1)->eval - ss->eval);
+    }
 
     // Step 6. Razoring (is omitted in PV nodes)
     if (   !PvNode
@@ -1727,20 +1742,6 @@ split_point_start: // At split points actual search starts from here
   }
 
 
-  // update_gains() updates the gains table of a non-capture move given
-  // the static position evaluation before and after the move.
-
-  void update_gains(const Position& pos, Move m, Value before, Value after) {
-
-    if (   m != MOVE_NULL
-        && before != VALUE_NONE
-        && after != VALUE_NONE
-        && pos.captured_piece_type() == PIECE_TYPE_NONE
-        && !is_special(m))
-        H.update_gain(pos.piece_on(move_to(m)), move_to(m), -(before + after));
-  }
-
-
   // current_search_time() returns the number of milliseconds which have passed
   // since the beginning of the current search.
 
@@ -1918,38 +1919,6 @@ split_point_start: // At split points actual search starts from here
     static int lastInfoTime;
     int t = current_search_time();
 
-    //  Poll for input
-    if (input_available())
-    {
-        // We are line oriented, don't read single chars
-        string command;
-
-        if (!std::getline(std::cin, command) || command == "quit")
-        {
-            // Quit the program as soon as possible
-            Limits.ponder = false;
-            QuitRequest = StopRequest = true;
-            return;
-        }
-        else if (command == "stop")
-        {
-            // Stop calculating as soon as possible, but still send the "bestmove"
-            // and possibly the "ponder" token when finishing the search.
-            Limits.ponder = false;
-            StopRequest = true;
-        }
-        else if (command == "ponderhit")
-        {
-            // The opponent has played the expected move. GUI sends "ponderhit" if
-            // we were told to ponder on the same move the opponent has played. We
-            // should continue searching but switching from pondering to normal search.
-            Limits.ponder = false;
-
-            if (StopOnPonderhit)
-                StopRequest = true;
-        }
-    }
-
     // Print search information
     if (t < 1000)
         lastInfoTime = 0;
@@ -1994,14 +1963,14 @@ split_point_start: // At split points actual search starts from here
 
   void wait_for_stop_or_ponderhit() {
 
-    string command;
+    string cmd;
 
     // Wait for a command from stdin
-    while (   std::getline(std::cin, command)
-           && command != "ponderhit" && command != "stop" && command != "quit") {};
+    while (cmd != "ponderhit" && cmd != "stop" && cmd != "quit")
+        Threads.getline(cmd);
 
-    if (command != "ponderhit" && command != "stop")
-        QuitRequest = true; // Must be "quit" or getline() returned false
+    if (cmd == "quit")
+        QuitRequest = true;
   }
 
 
@@ -2254,3 +2223,34 @@ void Thread::idle_loop(SplitPoint* sp) {
       }
   }
 }
+
+
+// ThreadsManager::do_uci_async_cmd() processes the commands from GUI received
+// by listener thread while the other threads are searching.
+
+void ThreadsManager::do_uci_async_cmd(const std::string& cmd) {
+
+  if (cmd == "quit")
+  {
+      // Quit the program as soon as possible
+      Limits.ponder = false;
+      QuitRequest = StopRequest = true;
+  }
+  else if (cmd == "stop")
+  {
+      // Stop calculating as soon as possible, but still send the "bestmove"
+      // and possibly the "ponder" token when finishing the search.
+      Limits.ponder = false;
+      StopRequest = true;
+  }
+  else if (cmd == "ponderhit")
+  {
+      // The opponent has played the expected move. GUI sends "ponderhit" if
+      // we were told to ponder on the same move the opponent has played. We
+      // should continue searching but switching from pondering to normal search.
+      Limits.ponder = false;
+
+      if (StopOnPonderhit)
+          StopRequest = true;
+  }
+}