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 #ifndef THREAD_H_INCLUDED
21 #define THREAD_H_INCLUDED
34 const int MAX_THREADS = 128;
35 const int MAX_SPLITPOINTS_PER_THREAD = 8;
37 /// Mutex and ConditionVariable struct are wrappers of the low level locking
38 /// machinery and are modeled after the corresponding C++11 classes.
41 Mutex() { lock_init(l); }
42 ~Mutex() { lock_destroy(l); }
44 void lock() { lock_grab(l); }
45 void unlock() { lock_release(l); }
48 friend struct ConditionVariable;
53 struct ConditionVariable {
54 ConditionVariable() { cond_init(c); }
55 ~ConditionVariable() { cond_destroy(c); }
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); }
66 /// SplitPoint struct stores information shared by the threads searching in
67 /// parallel below the same split point. It is populated at splitting time.
71 // Const data after split point has been setup
80 // Const pointers to shared data
81 MovePicker* movePicker;
82 SplitPoint* parentSplitPoint;
84 // Shared variable data
86 std::bitset<MAX_THREADS> slavesMask;
87 volatile bool allSlavesSearching;
88 volatile uint64_t nodes;
90 volatile Value bestValue;
91 volatile Move bestMove;
92 volatile int moveCount;
97 /// ThreadBase struct is the base of the hierarchy from where we derive all the
98 /// specialized thread classes.
102 ThreadBase() : handle(NativeHandle()), exit(false) {}
103 virtual ~ThreadBase() {}
104 virtual void idle_loop() = 0;
106 void wait_for(volatile const bool& b);
109 ConditionVariable sleepCondition;
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.
120 struct Thread : public ThreadBase {
123 virtual void idle_loop();
124 bool cutoff_occurred() const;
125 bool available_to(const Thread* master) const;
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);
130 SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
131 Pawns::Table pawnsTable;
132 Material::Table materialTable;
134 Position* activePosition;
137 SplitPoint* volatile activeSplitPoint;
138 volatile int splitPointsSize;
139 volatile bool searching;
143 /// MainThread and TimerThread are derived classes used to characterize the two
144 /// special threads: the main one and the recurring timer.
146 struct MainThread : public Thread {
147 MainThread() : thinking(true) {} // Avoid a race with start_thinking()
148 virtual void idle_loop();
149 volatile bool thinking;
152 struct TimerThread : public ThreadBase {
154 static const int Resolution = 5; // Millisec between two check_time() calls
156 TimerThread() : run(false) {}
157 virtual void idle_loop();
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.
167 struct ThreadPool : public std::vector<Thread*> {
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.
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&);
178 Depth minimumSplitDepth;
180 ConditionVariable sleepCondition;
184 extern ThreadPool Threads;
186 #endif // #ifndef THREAD_H_INCLUDED