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