BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Thread.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <atomic>
23#include <chrono>
27#include <libutil/Memory.hpp>
28#include <libutil/Threading.hpp>
29
30namespace chess::uci {
31struct GoCommandOptions;
32} // namespace chess::uci
33
34namespace ben_bot::search {
35
36using std::chrono::milliseconds;
37
42struct Thread final {
44 Thread() = default;
45
49 explicit Thread(Callbacks&& callbacksToUse);
50
52
53 Thread(const Thread&) = delete;
54 Thread& operator=(const Thread&) = delete;
55 Thread(Thread&&) = delete;
56 Thread& operator=(Thread&&) = delete;
57
63
68 void start()
69 {
70 // shouldn't have been searching, but better safe than sorry
71 context.abort();
72 context.wait();
73
74 // reset the abort flag here, rather than on the background thread, so that
75 // a stop command received before the background thread begins searching isn't lost
76 context.reset_abort();
77
78 startSearch.store(true, std::memory_order::release);
79 }
80
81private:
82 void thread_func();
83
84 // align to cache lines to prevent false sharing of these flags
85 alignas(util::memory::CacheLineSize) std::atomic_bool threadShouldExit { false };
86 alignas(util::memory::CacheLineSize) std::atomic_bool startSearch { false };
87
88 // The search recurses once per ply, up to MAX_PLY plies deep, & each ply uses about 3 KiB
89 // of stack in a Release build (more in Debug & sanitizer builds), so it can need more than
90 // the default stack size for a new thread, which is only 512 KiB on macOS. The memory is
91 // only reserved, so a generous size costs nothing until the search actually goes that deep.
92 static constexpr auto STACK_SIZE { 64uz * 1024uz * 1024uz };
93
94 // this must be declared after everything that thread_func() uses, since members are
95 // initialized in declaration order and the thread starts running as soon as it's created
96 util::LargeStackThread searcherThread { STACK_SIZE, [this] { thread_func(); } };
97};
98
99} // namespace ben_bot::search
constexpr auto CacheLineSize
Definition Memory.hpp:73
Thread & operator=(const Thread &)=delete
Thread & operator=(Thread &&)=delete
Thread(Thread &&)=delete
Thread(const Thread &)=delete
Thread(Callbacks &&callbacksToUse)