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 makes some init and launches the thread that will go to
33 // sleep in idle_loop().
37 searching = true; // Avoid a race with start_thinking()
38 exit = resetCalls = false;
39 maxPly = callsCnt = 0;
42 idx = Threads.size(); // Starts from 0
43 std::thread::operator=(std::thread(&Thread::idle_loop, this));
47 // Thread destructor waits for thread termination before deleting
52 exit = true; // Search must be already finished
56 std::thread::join(); // Wait for thread termination
60 // Thread::join() waits for the thread to finish searching
63 std::unique_lock<Mutex> lk(mutex);
64 sleepCondition.wait(lk, [&]{ return !searching; });
68 // Thread::notify_one() wakes up the thread when there is some work to do
70 void Thread::notify_one() {
72 std::unique_lock<Mutex> lk(mutex);
73 sleepCondition.notify_one();
77 // Thread::wait() set the thread to sleep until 'condition' turns true
79 void Thread::wait(std::atomic_bool& condition) {
81 std::unique_lock<Mutex> lk(mutex);
82 sleepCondition.wait(lk, [&]{ return bool(condition); });
86 // Thread::idle_loop() is where the thread is parked when it has no work to do
88 void Thread::idle_loop() {
92 std::unique_lock<Mutex> lk(mutex);
96 while (!searching && !exit)
98 sleepCondition.notify_one(); // Wake up main thread if needed
99 sleepCondition.wait(lk);
104 if (!exit && searching)
110 // ThreadPool::init() is called at startup to create and launch requested threads,
111 // that will go immediately to sleep. We cannot use a constructor because Threads
112 // is a static object and we need a fully initialized engine at this point due to
113 // allocation of Endgames in the Thread constructor.
115 void ThreadPool::init() {
117 push_back(new MainThread);
122 // ThreadPool::exit() terminates the threads before the program exits. Cannot be
123 // done in destructor because threads must be terminated before freeing us.
125 void ThreadPool::exit() {
127 for (Thread* th : *this)
130 clear(); // Get rid of stale pointers
134 // ThreadPool::read_uci_options() updates internal threads parameters from the
135 // corresponding UCI options and creates/destroys threads to match the requested
136 // number. Thread objects are dynamically allocated to avoid creating all possible
137 // threads in advance (which include pawns and material tables), even if only a
138 // few are to be used.
140 void ThreadPool::read_uci_options() {
142 size_t requested = Options["Threads"];
144 assert(requested > 0);
146 while (size() < requested)
147 push_back(new Thread);
149 while (size() > requested)
157 // ThreadPool::nodes_searched() returns the number of nodes searched
159 int64_t ThreadPool::nodes_searched() {
162 for (Thread *th : *this)
163 nodes += th->rootPos.nodes_searched();
168 // ThreadPool::start_thinking() wakes up the main thread sleeping in
169 // MainThread::idle_loop() and starts a new search, then returns immediately.
171 void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
172 StateStackPtr& states) {
173 for (Thread* th : Threads)
176 Signals.stopOnPonderhit = Signals.firstRootMove = false;
177 Signals.stop = Signals.failedLowAtRoot = 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()->searching = true;
194 main()->notify_one(); // Wake up main thread: 'searching' must be already set