mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 09:39:36 +00:00
Introduce and use wait_for_search_finished()
Helper function that allows us to simplify the code. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
32d3a07c67
commit
3aa471f2a9
4 changed files with 35 additions and 51 deletions
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ Thread::Thread(Fn fn) {
|
||||||
start_fn = fn;
|
start_fn = fn;
|
||||||
threadID = Threads.size();
|
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);
|
lock_init(sleepLock);
|
||||||
cond_init(sleepCond);
|
cond_init(sleepCond);
|
||||||
|
@ -137,6 +137,7 @@ void Thread::main_loop() {
|
||||||
void Thread::wake_up() {
|
void Thread::wake_up() {
|
||||||
|
|
||||||
lock_grab(sleepLock);
|
lock_grab(sleepLock);
|
||||||
|
do_sleep = false;
|
||||||
cond_signal(sleepCond);
|
cond_signal(sleepCond);
|
||||||
lock_release(sleepLock);
|
lock_release(sleepLock);
|
||||||
}
|
}
|
||||||
|
@ -154,10 +155,7 @@ void Thread::wait_for_stop_or_ponderhit() {
|
||||||
Signals.stopOnPonderhit = true;
|
Signals.stopOnPonderhit = true;
|
||||||
|
|
||||||
lock_grab(sleepLock);
|
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;
|
threads[i]->maxPly = 0;
|
||||||
|
|
||||||
if (!useSleepingThreads)
|
if (!useSleepingThreads)
|
||||||
|
@ -273,7 +270,7 @@ void ThreadsManager::wake_up() const {
|
||||||
void ThreadsManager::sleep() const {
|
void ThreadsManager::sleep() const {
|
||||||
|
|
||||||
for (int i = 1; i < size(); i++) // Main thread will go to sleep by itself
|
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
|
// thread parked in main_loop() and starting a new search. If async is true
|
||||||
// then function returns immediately, otherwise caller is blocked waiting for
|
// then function returns immediately, otherwise caller is blocked waiting for
|
||||||
// the search to finish.
|
// the search to finish.
|
||||||
|
|
||||||
void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limits,
|
void ThreadsManager::start_searching(const Position& pos, const LimitsType& limits,
|
||||||
const std::set<Move>& searchMoves, bool async) {
|
const std::set<Move>& searchMoves, bool async) {
|
||||||
Thread& main = *threads.front();
|
wait_for_search_finished();
|
||||||
|
|
||||||
lock_grab(main.sleepLock);
|
|
||||||
|
|
||||||
while (!main.do_sleep)
|
|
||||||
cond_wait(sleepCond, main.sleepLock); // Wait main thread has finished
|
|
||||||
|
|
||||||
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
||||||
Signals.stop = Signals.failedLowAtRoot = 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()))
|
if (searchMoves.empty() || searchMoves.count(ml.move()))
|
||||||
RootMoves.push_back(RootMove(ml.move()));
|
RootMoves.push_back(RootMove(ml.move()));
|
||||||
|
|
||||||
main.do_sleep = false;
|
threads[0]->wake_up(); // Start main thread
|
||||||
cond_signal(main.sleepCond); // Wake up main thread and start searching
|
|
||||||
|
|
||||||
if (!async)
|
if (!async)
|
||||||
while (!main.do_sleep)
|
wait_for_search_finished();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,6 @@ struct SplitPoint {
|
||||||
MovePicker* mp;
|
MovePicker* mp;
|
||||||
SplitPoint* parent;
|
SplitPoint* parent;
|
||||||
|
|
||||||
|
|
||||||
// Shared data
|
// Shared data
|
||||||
Lock lock;
|
Lock lock;
|
||||||
volatile uint64_t slavesMask;
|
volatile uint64_t slavesMask;
|
||||||
|
@ -125,8 +124,8 @@ public:
|
||||||
void read_uci_options();
|
void read_uci_options();
|
||||||
bool available_slave_exists(int master) const;
|
bool available_slave_exists(int master) const;
|
||||||
void set_timer(int msec);
|
void set_timer(int msec);
|
||||||
void stop_thinking();
|
void wait_for_search_finished();
|
||||||
void start_thinking(const Position& pos, const Search::LimitsType& limits,
|
void start_searching(const Position& pos, const Search::LimitsType& limits,
|
||||||
const std::set<Move>& = std::set<Move>(), bool async = false);
|
const std::set<Move>& = std::set<Move>(), bool async = false);
|
||||||
|
|
||||||
template <bool Fake>
|
template <bool Fake>
|
||||||
|
|
12
src/uci.cpp
12
src/uci.cpp
|
@ -67,7 +67,10 @@ void uci_loop() {
|
||||||
is >> skipws >> token;
|
is >> skipws >> token;
|
||||||
|
|
||||||
if (token == "quit" || token == "stop")
|
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;
|
Search::Limits.ponder = false;
|
||||||
|
|
||||||
if (Search::Signals.stopOnPonderhit)
|
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.time = time[pos.side_to_move()];
|
||||||
limits.increment = inc[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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue