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
28 #include "syzygy/tbprobe.h"
30 ThreadPool Threads; // Global object
32 /// Thread constructor launches the thread and then waits until it goes to sleep
37 resetCalls = exit = false;
38 maxPly = callsCnt = 0;
41 idx = Threads.size(); // Start from 0
43 std::unique_lock<Mutex> lk(mutex);
45 nativeThread = std::thread(&Thread::idle_loop, this);
46 sleepCondition.wait(lk, [&]{ return !searching; });
50 /// Thread destructor waits for thread termination before returning
56 sleepCondition.notify_one();
62 /// Thread::wait_for_search_finished() waits on sleep condition
63 /// until not searching
65 void Thread::wait_for_search_finished() {
67 std::unique_lock<Mutex> lk(mutex);
68 sleepCondition.wait(lk, [&]{ return !searching; });
72 /// Thread::wait() waits on sleep condition until condition is true
74 void Thread::wait(std::atomic_bool& condition) {
76 std::unique_lock<Mutex> lk(mutex);
77 sleepCondition.wait(lk, [&]{ return bool(condition); });
81 /// Thread::start_searching() wakes up the thread that will start the search
83 void Thread::start_searching(bool resume) {
85 std::unique_lock<Mutex> lk(mutex);
90 sleepCondition.notify_one();
94 /// Thread::idle_loop() is where the thread is parked when it has no work to do
96 void Thread::idle_loop() {
100 std::unique_lock<Mutex> lk(mutex);
104 while (!searching && !exit)
106 sleepCondition.notify_one(); // Wake up any waiting thread
107 sleepCondition.wait(lk);
118 /// ThreadPool::init() creates and launches requested threads that will go
119 /// immediately to sleep. We cannot use a constructor because Threads is a
120 /// static object and we need a fully initialized engine at this point due to
121 /// allocation of Endgames in the Thread constructor.
123 void ThreadPool::init() {
125 push_back(new MainThread);
130 /// ThreadPool::exit() terminates threads before the program exits. Cannot be
131 /// done in destructor because threads must be terminated before deleting any
132 /// static objects while still in main().
134 void ThreadPool::exit() {
137 delete back(), pop_back();
141 /// ThreadPool::read_uci_options() updates internal threads parameters from the
142 /// corresponding UCI options and creates/destroys threads to match requested
143 /// number. Thread objects are dynamically allocated.
145 void ThreadPool::read_uci_options() {
147 size_t requested = Options["Threads"];
149 assert(requested > 0);
151 while (size() < requested)
152 push_back(new Thread);
154 while (size() > requested)
155 delete back(), pop_back();
159 /// ThreadPool::nodes_searched() returns the number of nodes searched
161 int64_t ThreadPool::nodes_searched() {
164 for (Thread* th : *this)
165 nodes += th->rootPos.nodes_searched();
170 /// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop()
171 /// and starts a new search, then returns immediately.
173 void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
174 const Search::LimitsType& limits) {
176 main()->wait_for_search_finished();
178 Search::Signals.stopOnPonderhit = Search::Signals.stop = false;
179 Search::Limits = limits;
180 Search::RootMoves rootMoves;
182 for (const auto& m : MoveList<LEGAL>(pos))
183 if ( limits.searchmoves.empty()
184 || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
185 rootMoves.push_back(Search::RootMove(m));
187 if (!rootMoves.empty())
188 Tablebases::filter_root_moves(pos, rootMoves);
190 // After ownership transfer 'states' becomes empty, so if we stop the search
191 // and call 'go' again without setting a new position states.get() == NULL.
192 assert(states.get() || setupStates.get());
195 setupStates = std::move(states); // Ownership transfer, states is now empty
197 StateInfo tmp = setupStates->back();
199 for (Thread* th : Threads)
202 th->rootDepth = DEPTH_ZERO;
203 th->rootMoves = rootMoves;
204 th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
207 setupStates->back() = tmp; // Restore st->previous, cleared by Position::set()
209 main()->start_searching();