]> git.sesse.net Git - stockfish/blob - src/thread.h
Fix a MSVC warning at W4
[stockfish] / src / thread.h
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
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 #ifndef THREAD_H_INCLUDED
21 #define THREAD_H_INCLUDED
22
23 #include <bitset>
24 #include <vector>
25
26 #include "material.h"
27 #include "movepick.h"
28 #include "pawns.h"
29 #include "position.h"
30 #include "search.h"
31
32 struct Thread;
33
34 const int MAX_THREADS = 128;
35 const int MAX_SPLITPOINTS_PER_THREAD = 8;
36
37 /// Mutex and ConditionVariable struct are wrappers of the low level locking
38 /// machinery and are modeled after the corresponding C++11 classes.
39
40 struct Mutex {
41   Mutex() { lock_init(l); }
42  ~Mutex() { lock_destroy(l); }
43
44   void lock() { lock_grab(l); }
45   void unlock() { lock_release(l); }
46
47 private:
48   friend struct ConditionVariable;
49
50   Lock l;
51 };
52
53 struct ConditionVariable {
54   ConditionVariable() { cond_init(c); }
55  ~ConditionVariable() { cond_destroy(c); }
56
57   void wait(Mutex& m) { cond_wait(c, m.l); }
58   void wait_for(Mutex& m, int ms) { timed_wait(c, m.l, ms); }
59   void notify_one() { cond_signal(c); }
60
61 private:
62   WaitCondition c;
63 };
64
65
66 /// SplitPoint struct stores information shared by the threads searching in
67 /// parallel below the same split point. It is populated at splitting time.
68
69 struct SplitPoint {
70
71   // Const data after split point has been setup
72   const Position* pos;
73   Search::Stack* ss;
74   Thread* masterThread;
75   Depth depth;
76   Value beta;
77   int nodeType;
78   bool cutNode;
79
80   // Const pointers to shared data
81   MovePicker* movePicker;
82   SplitPoint* parentSplitPoint;
83
84   // Shared variable data
85   Mutex mutex;
86   std::bitset<MAX_THREADS> slavesMask;
87   volatile bool allSlavesSearching;
88   volatile uint64_t nodes;
89   volatile Value alpha;
90   volatile Value bestValue;
91   volatile Move bestMove;
92   volatile int moveCount;
93   volatile bool cutoff;
94 };
95
96
97 /// ThreadBase struct is the base of the hierarchy from where we derive all the
98 /// specialized thread classes.
99
100 struct ThreadBase {
101
102   ThreadBase() : handle(NativeHandle()), exit(false) {}
103   virtual ~ThreadBase() {}
104   virtual void idle_loop() = 0;
105   void notify_one();
106   void wait_for(volatile const bool& b);
107
108   Mutex mutex;
109   ConditionVariable sleepCondition;
110   NativeHandle handle;
111   volatile bool exit;
112 };
113
114
115 /// Thread struct keeps together all the thread related stuff like locks, state
116 /// and especially split points. We also use per-thread pawn and material hash
117 /// tables so that once we get a pointer to an entry its life time is unlimited
118 /// and we don't have to care about someone changing the entry under our feet.
119
120 struct Thread : public ThreadBase {
121
122   Thread();
123   virtual void idle_loop();
124   bool cutoff_occurred() const;
125   bool available_to(const Thread* master) const;
126
127   void split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
128              Depth depth, int moveCount, MovePicker* movePicker, int nodeType, bool cutNode);
129
130   SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
131   Pawns::Table pawnsTable;
132   Material::Table materialTable;
133   Endgames endgames;
134   Position* activePosition;
135   size_t idx;
136   int maxPly;
137   SplitPoint* volatile activeSplitPoint;
138   volatile int splitPointsSize;
139   volatile bool searching;
140 };
141
142
143 /// MainThread and TimerThread are derived classes used to characterize the two
144 /// special threads: the main one and the recurring timer.
145
146 struct MainThread : public Thread {
147   MainThread() : thinking(true) {} // Avoid a race with start_thinking()
148   virtual void idle_loop();
149   volatile bool thinking;
150 };
151
152 struct TimerThread : public ThreadBase {
153
154   static const int Resolution = 5; // Millisec between two check_time() calls
155
156   TimerThread() : run(false) {}
157   virtual void idle_loop();
158
159   bool run;
160 };
161
162
163 /// ThreadPool struct handles all the threads related stuff like init, starting,
164 /// parking and, most importantly, launching a slave thread at a split point.
165 /// All the access to shared thread data is done through this class.
166
167 struct ThreadPool : public std::vector<Thread*> {
168
169   void init(); // No c'tor and d'tor, threads rely on globals that should be
170   void exit(); // initialized and are valid during the whole thread lifetime.
171
172   MainThread* main() { return static_cast<MainThread*>(at(0)); }
173   void read_uci_options();
174   Thread* available_slave(const Thread* master) const;
175   void wait_for_think_finished();
176   void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&);
177
178   Depth minimumSplitDepth;
179   Mutex mutex;
180   ConditionVariable sleepCondition;
181   TimerThread* timer;
182 };
183
184 extern ThreadPool Threads;
185
186 #endif // #ifndef THREAD_H_INCLUDED