]> git.sesse.net Git - stockfish/blob - src/thread.h
Fix compilation after recent merge.
[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.
38 class Thread {
39
40     std::mutex              mutex;
41     std::condition_variable cv;
42     size_t                  idx;
43     bool                    exit = false, searching = true;  // Set before starting std::thread
44     NativeThread            stdThread;
45
46    public:
47     explicit Thread(size_t);
48     virtual ~Thread();
49     virtual void search();
50     void         clear();
51     void         idle_loop();
52     void         start_searching();
53     void         wait_for_search_finished();
54     size_t       id() const { return idx; }
55
56     size_t                pvIdx, pvLast;
57     std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
58     int                   selDepth, nmpMinPly;
59     Value                 bestValue, optimism[COLOR_NB];
60
61     Position              rootPos;
62     StateInfo             rootState;
63     Search::RootMoves     rootMoves;
64     Depth                 rootDepth, completedDepth;
65     Value                 rootDelta;
66     Value                 rootSimpleEval;
67     CounterMoveHistory    counterMoves;
68     ButterflyHistory      mainHistory;
69     CapturePieceToHistory captureHistory;
70     ContinuationHistory   continuationHistory[2][2];
71     PawnHistory           pawnHistory;
72 };
73
74
75 // MainThread is a derived class specific for main thread
76 struct MainThread: public Thread {
77
78     using Thread::Thread;
79
80     void search() override;
81     void check_time();
82
83     double           previousTimeReduction;
84     Value            bestPreviousScore;
85     Value            bestPreviousAverageScore;
86     Value            iterValue[4];
87     int              callsCnt;
88     bool             stopOnPonderhit;
89     std::atomic_bool ponder;
90 };
91
92
93 // ThreadPool struct handles all the threads-related stuff like init, starting,
94 // parking and, most importantly, launching a thread. All the access to threads
95 // is done through this class.
96 struct ThreadPool {
97
98     void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
99     void clear();
100     void set(size_t);
101
102     MainThread* main() const { return static_cast<MainThread*>(threads.front()); }
103     uint64_t    nodes_searched() const { return accumulate(&Thread::nodes); }
104     uint64_t    tb_hits() const { return accumulate(&Thread::tbHits); }
105     Thread*     get_best_thread() const;
106     void        start_searching();
107     void        wait_for_search_finished() const;
108
109     std::atomic_bool stop, increaseDepth;
110
111     auto cbegin() const noexcept { return threads.cbegin(); }
112     auto begin() noexcept { return threads.begin(); }
113     auto end() noexcept { return threads.end(); }
114     auto cend() const noexcept { return threads.cend(); }
115     auto size() const noexcept { return threads.size(); }
116     auto empty() const noexcept { return threads.empty(); }
117
118    private:
119     StateListPtr         setupStates;
120     std::vector<Thread*> threads;
121
122     uint64_t accumulate(std::atomic<uint64_t> Thread::*member) const {
123
124         uint64_t sum = 0;
125         for (Thread* th : threads)
126             sum += (th->*member).load(std::memory_order_relaxed);
127         return sum;
128     }
129 };
130
131 extern ThreadPool Threads;
132
133 }  // namespace Stockfish
134
135 #endif  // #ifndef THREAD_H_INCLUDED