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
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <algorithm> // For std::count
28 using namespace Search;
30 ThreadPool Threads; // Global object
32 /// Thread constructor launch the thread and then wait 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 wait for thread termination before returning
56 sleepCondition.notify_one();
62 /// Thread::wait_for_search_finished() wait on sleep condition until not searching
64 void Thread::wait_for_search_finished() {
66 std::unique_lock<Mutex> lk(mutex);
67 sleepCondition.wait(lk, [&]{ return !searching; });
71 /// Thread::wait() wait on sleep condition until condition is true
73 void Thread::wait(std::atomic_bool& condition) {
75 std::unique_lock<Mutex> lk(mutex);
76 sleepCondition.wait(lk, [&]{ return bool(condition); });
80 /// Thread::start_searching() wake up the thread that will start the search
82 void Thread::start_searching(bool resume) {
84 std::unique_lock<Mutex> lk(mutex);
89 sleepCondition.notify_one();
93 /// Thread::idle_loop() is where the thread is parked when it has no work to do
95 void Thread::idle_loop() {
99 std::unique_lock<Mutex> lk(mutex);
103 while (!searching && !exit)
105 sleepCondition.notify_one(); // Wake up any waiting thread
106 sleepCondition.wait(lk);
117 /// ThreadPool::init() create and launch requested threads, that will go
118 /// immediately to sleep. We cannot use a constructor because Threads is a
119 /// static object and we need a fully initialized engine at this point due to
120 /// allocation of Endgames in the Thread constructor.
122 void ThreadPool::init() {
124 push_back(new MainThread);
129 /// ThreadPool::exit() terminate threads before the program exits. Cannot be
130 /// done in destructor because threads must be terminated before deleting any
131 /// static objects, so while still in main().
133 void ThreadPool::exit() {
136 delete back(), pop_back();
140 /// ThreadPool::read_uci_options() updates internal threads parameters from the
141 /// corresponding UCI options and creates/destroys threads to match requested
142 /// number. Thread objects are dynamically allocated.
144 void ThreadPool::read_uci_options() {
146 size_t requested = Options["Threads"];
148 assert(requested > 0);
150 while (size() < requested)
151 push_back(new Thread);
153 while (size() > requested)
154 delete back(), pop_back();
158 /// ThreadPool::nodes_searched() return the number of nodes searched
160 int64_t ThreadPool::nodes_searched() {
163 for (Thread* th : *this)
164 nodes += th->rootPos.nodes_searched();
169 /// ThreadPool::start_thinking() wake up the main thread sleeping in idle_loop()
170 /// and start a new search, then return immediately.
172 void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
173 StateStackPtr& states) {
175 main()->wait_for_search_finished();
177 Signals.stopOnPonderhit = Signals.stop = false;
179 main()->rootMoves.clear();
180 main()->rootPos = pos;
182 if (states.get()) // If we don't set a new position, preserve current state
184 SetupStates = std::move(states); // Ownership transfer here
185 assert(!states.get());
188 for (const auto& m : MoveList<LEGAL>(pos))
189 if ( limits.searchmoves.empty()
190 || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
191 main()->rootMoves.push_back(RootMove(m));
193 main()->start_searching();