Skip to content

Commit

Permalink
Simplify finished search in ponder/infinite mode.
Browse files Browse the repository at this point in the history
In this rare case (e.g. go infinite on a stalemate),
just spin till ponderhit/stop comes.

The Thread::wait() is a renmant of the old YBWC
code, today with lazy SMP, threads don't need to
wait when outside of their idle loop.

No functional change.
  • Loading branch information
vondele authored and mcostalba committed Aug 11, 2017
1 parent 66c5eae commit 2783203
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/search.cpp
Expand Up @@ -277,13 +277,13 @@ void MainThread::search() {
// the UCI protocol states that we shouldn't print the best move before the
// GUI sends a "stop" or "ponderhit" command. We therefore simply wait here
// until the GUI sends one of those commands (which also raises Threads.stop).
if (!Threads.stop && (Threads.ponder || Limits.infinite))
{
Threads.stopOnPonderhit = true;
wait(Threads.stop);
}
Threads.stopOnPonderhit = true;

while (!Threads.stop && (Threads.ponder || Limits.infinite))
{} // Busy wait for a stop or a ponder reset

// Stop the threads if not already stopped
// Stop the threads if not already stopped (also raise the stop if
// "ponderhit" just reset Threads.ponder).
Threads.stop = true;

// Wait until all threads have finished
Expand Down
16 changes: 2 additions & 14 deletions src/thread.cpp
Expand Up @@ -68,24 +68,12 @@ void Thread::wait_for_search_finished() {
}


/// Thread::wait() waits on sleep condition until condition is true

void Thread::wait(std::atomic_bool& condition) {

std::unique_lock<Mutex> lk(mutex);
sleepCondition.wait(lk, [&]{ return bool(condition); });
}


/// Thread::start_searching() wakes up the thread that will start the search

void Thread::start_searching(bool resume) {
void Thread::start_searching() {

std::unique_lock<Mutex> lk(mutex);

if (!resume)
searching = true;

searching = true;
sleepCondition.notify_one();
}

Expand Down
3 changes: 1 addition & 2 deletions src/thread.h
Expand Up @@ -52,9 +52,8 @@ class Thread {
virtual ~Thread();
virtual void search();
void idle_loop();
void start_searching(bool resume = false);
void start_searching();
void wait_for_search_finished();
void wait(std::atomic_bool& condition);

Pawns::Table pawnsTable;
Material::Table materialTable;
Expand Down
4 changes: 1 addition & 3 deletions src/uci.cpp
Expand Up @@ -176,10 +176,8 @@ void UCI::loop(int argc, char* argv[]) {
if ( token == "quit"
|| token == "stop"
|| (token == "ponderhit" && Threads.stopOnPonderhit))
{
Threads.stop = true;
Threads.main()->start_searching(true); // Could be sleeping
}

else if (token == "ponderhit")
Threads.ponder = false; // Switch to normal search

Expand Down

0 comments on commit 2783203

Please sign in to comment.