]> git.sesse.net Git - stockfish/blob - src/thread.cpp
Update Copyright year
[stockfish] / src / thread.cpp
1 /*
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-2016 Marco Costalba, Joona Kiiski, Tord Romstad
5
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.
10
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.
15
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/>.
18 */
19
20 #include <algorithm> // For std::count
21 #include <cassert>
22
23 #include "movegen.h"
24 #include "search.h"
25 #include "thread.h"
26 #include "uci.h"
27
28 using namespace Search;
29
30 ThreadPool Threads; // Global object
31
32 /// Thread constructor launch the thread and then wait until it goes to sleep
33 /// in idle_loop().
34
35 Thread::Thread() {
36
37   resetCalls = exit = false;
38   maxPly = callsCnt = 0;
39   history.clear();
40   counterMoves.clear();
41   idx = Threads.size(); // Start from 0
42
43   std::unique_lock<Mutex> lk(mutex);
44   searching = true;
45   nativeThread = std::thread(&Thread::idle_loop, this);
46   sleepCondition.wait(lk, [&]{ return !searching; });
47 }
48
49
50 /// Thread destructor wait for thread termination before returning
51
52 Thread::~Thread() {
53
54   mutex.lock();
55   exit = true;
56   sleepCondition.notify_one();
57   mutex.unlock();
58   nativeThread.join();
59 }
60
61
62 /// Thread::wait_for_search_finished() wait on sleep condition until not searching
63
64 void Thread::wait_for_search_finished() {
65
66   std::unique_lock<Mutex> lk(mutex);
67   sleepCondition.wait(lk, [&]{ return !searching; });
68 }
69
70
71 /// Thread::wait() wait on sleep condition until condition is true
72
73 void Thread::wait(std::atomic_bool& condition) {
74
75   std::unique_lock<Mutex> lk(mutex);
76   sleepCondition.wait(lk, [&]{ return bool(condition); });
77 }
78
79
80 /// Thread::start_searching() wake up the thread that will start the search
81
82 void Thread::start_searching(bool resume) {
83
84   std::unique_lock<Mutex> lk(mutex);
85
86   if (!resume)
87       searching = true;
88
89   sleepCondition.notify_one();
90 }
91
92
93 /// Thread::idle_loop() is where the thread is parked when it has no work to do
94
95 void Thread::idle_loop() {
96
97   while (!exit)
98   {
99       std::unique_lock<Mutex> lk(mutex);
100
101       searching = false;
102
103       while (!searching && !exit)
104       {
105           sleepCondition.notify_one(); // Wake up any waiting thread
106           sleepCondition.wait(lk);
107       }
108
109       lk.unlock();
110
111       if (!exit)
112           search();
113   }
114 }
115
116
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.
121
122 void ThreadPool::init() {
123
124   push_back(new MainThread);
125   read_uci_options();
126 }
127
128
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().
132
133 void ThreadPool::exit() {
134
135   while (size())
136       delete back(), pop_back();
137 }
138
139
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.
143
144 void ThreadPool::read_uci_options() {
145
146   size_t requested = Options["Threads"];
147
148   assert(requested > 0);
149
150   while (size() < requested)
151       push_back(new Thread);
152
153   while (size() > requested)
154       delete back(), pop_back();
155 }
156
157
158 /// ThreadPool::nodes_searched() return the number of nodes searched
159
160 int64_t ThreadPool::nodes_searched() {
161
162   int64_t nodes = 0;
163   for (Thread* th : *this)
164       nodes += th->rootPos.nodes_searched();
165   return nodes;
166 }
167
168
169 /// ThreadPool::start_thinking() wake up the main thread sleeping in idle_loop()
170 /// and start a new search, then return immediately.
171
172 void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
173                                 StateStackPtr& states) {
174
175   main()->wait_for_search_finished();
176
177   Signals.stopOnPonderhit = Signals.stop = false;
178
179   main()->rootMoves.clear();
180   main()->rootPos = pos;
181   Limits = limits;
182   if (states.get()) // If we don't set a new position, preserve current state
183   {
184       SetupStates = std::move(states); // Ownership transfer here
185       assert(!states.get());
186   }
187
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));
192
193   main()->start_searching();
194 }