2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5 Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <algorithm> // For std::count
29 using namespace Search;
31 ThreadPool Threads; // Global object
33 /// Thread constructor launches the thread and then waits until it goes to sleep
38 resetCalls = exit = false;
39 maxPly = callsCnt = 0;
42 idx = Threads.size(); // Start from 0
44 std::unique_lock<Mutex> lk(mutex);
46 nativeThread = std::thread(&Thread::idle_loop, this);
47 sleepCondition.wait(lk, [&]{ return !searching; });
51 /// Thread destructor waits for thread termination before returning
57 sleepCondition.notify_one();
63 /// Thread::wait_for_search_finished() waits on sleep condition
64 /// until not searching
66 void Thread::wait_for_search_finished() {
68 std::unique_lock<Mutex> lk(mutex);
69 sleepCondition.wait(lk, [&]{ return !searching; });
73 /// Thread::wait() waits on sleep condition until condition is true
75 void Thread::wait(std::atomic_bool& condition) {
77 std::unique_lock<Mutex> lk(mutex);
78 sleepCondition.wait(lk, [&]{ return bool(condition); });
82 /// Thread::start_searching() wakes up the thread that will start the search
84 void Thread::start_searching(bool resume) {
86 std::unique_lock<Mutex> lk(mutex);
91 sleepCondition.notify_one();
95 /// Thread::idle_loop() is where the thread is parked when it has no work to do
97 void Thread::idle_loop() {
101 std::unique_lock<Mutex> lk(mutex);
105 while (!searching && !exit)
107 sleepCondition.notify_one(); // Wake up any waiting thread
108 sleepCondition.wait(lk);
119 /// ThreadPool::init() creates and launches requested threads that will go
120 /// immediately to sleep. We cannot use a constructor because Threads is a
121 /// static object and we need a fully initialized engine at this point due to
122 /// allocation of Endgames in the Thread constructor.
124 void ThreadPool::init() {
126 push_back(new MainThread);
131 /// ThreadPool::exit() terminates threads before the program exits. Cannot be
132 /// done in destructor because threads must be terminated before deleting any
133 /// static objects while still in main().
135 void ThreadPool::exit() {
138 delete back(), pop_back();
142 /// ThreadPool::read_uci_options() updates internal threads parameters from the
143 /// corresponding UCI options and creates/destroys threads to match requested
144 /// number. Thread objects are dynamically allocated.
146 void ThreadPool::read_uci_options() {
148 size_t requested = Options["Threads"];
150 assert(requested > 0);
152 while (size() < requested)
153 push_back(new Thread);
155 while (size() > requested)
156 delete back(), pop_back();
160 /// ThreadPool::nodes_searched() returns the number of nodes searched
162 int64_t ThreadPool::nodes_searched() {
165 for (Thread* th : *this)
166 nodes += th->rootPos.nodes_searched();
171 /// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop()
172 /// and starts a new search, then returns immediately.
174 void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
175 StateStackPtr& states) {
177 main()->wait_for_search_finished();
179 Signals.stopOnPonderhit = Signals.stop = false;
181 main()->rootMoves.clear();
182 main()->rootPos = pos;
184 if (states.get()) // If we don't set a new position, preserve current state
186 SetupStates = std::move(states); // Ownership transfer here
187 assert(!states.get());
190 for (const auto& m : MoveList<LEGAL>(pos))
191 if ( limits.searchmoves.empty()
192 || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
193 main()->rootMoves.push_back(RootMove(m));
195 main()->start_searching();