]> git.sesse.net Git - stockfish/blob - src/thread.h
add clang-format
[stockfish] / src / thread.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef THREAD_H_INCLUDED
20 #define THREAD_H_INCLUDED
21
22 #include <atomic>
23 #include <condition_variable>
24 #include <cstddef>
25 #include <cstdint>
26 #include <mutex>
27 #include <vector>
28
29 #include "movepick.h"
30 #include "position.h"
31 #include "search.h"
32 #include "thread_win32_osx.h"
33 #include "types.h"
34
35 namespace Stockfish {
36
37 // Thread class keeps together all the thread-related stuff. We use
38 // per-thread pawn and material hash tables so that once we get a
39 // pointer to an entry its lifetime is unlimited and we don't have
40 // to care about someone changing the entry under our feet.
41
42 class Thread {
43
44     std::mutex              mutex;
45     std::condition_variable cv;
46     size_t                  idx;
47     bool                    exit = false, searching = true;  // Set before starting std::thread
48     NativeThread            stdThread;
49
50    public:
51     explicit Thread(size_t);
52     virtual ~Thread();
53     virtual void search();
54     void         clear();
55     void         idle_loop();
56     void         start_searching();
57     void         wait_for_search_finished();
58     size_t       id() const { return idx; }
59
60     size_t                pvIdx, pvLast;
61     std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
62     int                   selDepth, nmpMinPly;
63     Value                 bestValue, optimism[COLOR_NB];
64
65     Position              rootPos;
66     StateInfo             rootState;
67     Search::RootMoves     rootMoves;
68     Depth                 rootDepth, completedDepth;
69     Value                 rootDelta;
70     Value                 rootSimpleEval;
71     CounterMoveHistory    counterMoves;
72     ButterflyHistory      mainHistory;
73     CapturePieceToHistory captureHistory;
74     ContinuationHistory   continuationHistory[2][2];
75 };
76
77
78 // MainThread is a derived class specific for main thread
79
80 struct MainThread: public Thread {
81
82     using Thread::Thread;
83
84     void search() override;
85     void check_time();
86
87     double           previousTimeReduction;
88     Value            bestPreviousScore;
89     Value            bestPreviousAverageScore;
90     Value            iterValue[4];
91     int              callsCnt;
92     bool             stopOnPonderhit;
93     std::atomic_bool ponder;
94 };
95
96
97 // ThreadPool struct handles all the threads-related stuff like init, starting,
98 // parking and, most importantly, launching a thread. All the access to threads
99 // is done through this class.
100
101 struct ThreadPool {
102
103     void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
104     void clear();
105     void set(size_t);
106
107     MainThread* main() const { return static_cast<MainThread*>(threads.front()); }
108     uint64_t    nodes_searched() const { return accumulate(&Thread::nodes); }
109     uint64_t    tb_hits() const { return accumulate(&Thread::tbHits); }
110     Thread*     get_best_thread() const;
111     void        start_searching();
112     void        wait_for_search_finished() const;
113
114     std::atomic_bool stop, increaseDepth;
115
116     auto cbegin() const noexcept { return threads.cbegin(); }
117     auto begin() noexcept { return threads.begin(); }
118     auto end() noexcept { return threads.end(); }
119     auto cend() const noexcept { return threads.cend(); }
120     auto size() const noexcept { return threads.size(); }
121     auto empty() const noexcept { return threads.empty(); }
122
123    private:
124     StateListPtr         setupStates;
125     std::vector<Thread*> threads;
126
127     uint64_t accumulate(std::atomic<uint64_t> Thread::*member) const {
128
129         uint64_t sum = 0;
130         for (Thread* th : threads)
131             sum += (th->*member).load(std::memory_order_relaxed);
132         return sum;
133     }
134 };
135
136 extern ThreadPool Threads;
137
138 }  // namespace Stockfish
139
140 #endif  // #ifndef THREAD_H_INCLUDED