mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 09:47:46 +00:00
df6cb446ea
Simplify out low level sync stuff (mutex and friends) and avoid to use them directly in many functions. Also some renaming and better comment while there. No functional change.
178 lines
5.0 KiB
C++
178 lines
5.0 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <algorithm> // For std::count
|
|
#include <cassert>
|
|
|
|
#include "movegen.h"
|
|
#include "search.h"
|
|
#include "thread.h"
|
|
#include "syzygy/tbprobe.h"
|
|
|
|
ThreadPool Threads; // Global object
|
|
|
|
|
|
/// Thread constructor launches the thread and waits until it goes to sleep
|
|
/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set.
|
|
|
|
Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {
|
|
|
|
wait_for_search_finished();
|
|
}
|
|
|
|
|
|
/// Thread destructor wakes up the thread in idle_loop() and waits
|
|
/// for its termination. Thread should be already waiting.
|
|
|
|
Thread::~Thread() {
|
|
|
|
assert(!searching);
|
|
|
|
exit = true;
|
|
start_searching();
|
|
stdThread.join();
|
|
}
|
|
|
|
|
|
/// Thread::start_searching() wakes up the thread that will start the search
|
|
|
|
void Thread::start_searching() {
|
|
|
|
std::lock_guard<Mutex> lk(mutex);
|
|
searching = true;
|
|
cv.notify_one(); // Wake up the thread in idle_loop()
|
|
}
|
|
|
|
|
|
/// Thread::wait_for_search_finished() blocks on the condition variable
|
|
/// until the thread has finished searching.
|
|
|
|
void Thread::wait_for_search_finished() {
|
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
|
cv.wait(lk, [&]{ return !searching; });
|
|
}
|
|
|
|
|
|
/// Thread::idle_loop() is where the thread is parked, blocked on the
|
|
/// condition variable, when it has no work to do.
|
|
|
|
void Thread::idle_loop() {
|
|
|
|
WinProcGroup::bindThisThread(idx);
|
|
|
|
while (true)
|
|
{
|
|
std::unique_lock<Mutex> lk(mutex);
|
|
searching = false;
|
|
cv.notify_one(); // Wake up anyone waiting for search finished
|
|
cv.wait(lk, [&]{ return searching; });
|
|
|
|
if (exit)
|
|
return;
|
|
|
|
lk.unlock();
|
|
|
|
search();
|
|
}
|
|
}
|
|
|
|
|
|
/// ThreadPool::init() creates and launches the threads that will go
|
|
/// immediately to sleep in idle_loop. We cannot use the c'tor because
|
|
/// Threads is a static object and we need a fully initialized engine at
|
|
/// this point due to allocation of Endgames in the Thread constructor.
|
|
|
|
void ThreadPool::init(size_t requested) {
|
|
|
|
push_back(new MainThread(0));
|
|
set(requested);
|
|
}
|
|
|
|
|
|
/// ThreadPool::exit() terminates threads before the program exits. Cannot be
|
|
/// done in the destructor because threads must be terminated before deleting
|
|
/// any static object, so before main() returns.
|
|
|
|
void ThreadPool::exit() {
|
|
|
|
main()->wait_for_search_finished();
|
|
set(0);
|
|
}
|
|
|
|
|
|
/// ThreadPool::set() creates/destroys threads to match the requested number
|
|
|
|
void ThreadPool::set(size_t requested) {
|
|
|
|
while (size() < requested)
|
|
push_back(new Thread(size()));
|
|
|
|
while (size() > requested)
|
|
delete back(), pop_back();
|
|
}
|
|
|
|
|
|
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
|
/// returns immediately. Main thread will wake up other threads and start the search.
|
|
|
|
void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
|
|
const Search::LimitsType& limits, bool ponderMode) {
|
|
|
|
main()->wait_for_search_finished();
|
|
|
|
stopOnPonderhit = stop = false;
|
|
ponder = ponderMode;
|
|
Search::Limits = limits;
|
|
Search::RootMoves rootMoves;
|
|
|
|
for (const auto& m : MoveList<LEGAL>(pos))
|
|
if ( limits.searchmoves.empty()
|
|
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
|
|
rootMoves.emplace_back(m);
|
|
|
|
if (!rootMoves.empty())
|
|
Tablebases::filter_root_moves(pos, rootMoves);
|
|
|
|
// After ownership transfer 'states' becomes empty, so if we stop the search
|
|
// and call 'go' again without setting a new position states.get() == NULL.
|
|
assert(states.get() || setupStates.get());
|
|
|
|
if (states.get())
|
|
setupStates = std::move(states); // Ownership transfer, states is now empty
|
|
|
|
// We use Position::set() to set root position across threads. So we
|
|
// need to save and later to restore st->previous, cleared by set().
|
|
// Note that setupStates is shared by threads but is accessed in read-only mode.
|
|
StateInfo* previous = setupStates->back().previous;
|
|
|
|
for (Thread* th : Threads)
|
|
{
|
|
th->nodes = th->tbHits = 0;
|
|
th->rootDepth = th->completedDepth = DEPTH_ZERO;
|
|
th->rootMoves = rootMoves;
|
|
th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
|
|
}
|
|
|
|
setupStates->back().previous = previous;
|
|
|
|
main()->start_searching();
|
|
}
|