X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fthread.cpp;h=2dcbf91093dc7379d0281ccae235838d9c99f031;hb=adb71b8096436a54bf2326d5c69991b16ba5bafa;hp=f2a2c821151aa0219d525be1779e83d86c9dd2d1;hpb=32d3a07c6710e84d78999cb69c6a866b0bfff482;p=stockfish diff --git a/src/thread.cpp b/src/thread.cpp index f2a2c821..2dcbf910 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -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() } @@ -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& 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::set& 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; @@ -440,33 +445,6 @@ void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limit 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 - - 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(); }