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>
24
#include <
libbenbot/search/Callbacks.hpp
>
25
#include <
libbenbot/search/Context.hpp
>
26
#include <
libchess/game/Position.hpp
>
27
#include <
libutil/Memory.hpp
>
28
#include <
libutil/Threading.hpp
>
29
30
namespace
chess::uci
{
31
struct
GoCommandOptions
;
32
}
// namespace chess::uci
33
34
namespace
ben_bot::search
{
35
36
using
std::chrono::milliseconds;
37
42
struct
Thread
final {
44
Thread
() =
default
;
45
49
explicit
Thread
(
Callbacks
&& callbacksToUse);
50
51
~Thread
();
52
53
Thread
(
const
Thread
&) =
delete
;
54
Thread
&
operator=
(
const
Thread
&) =
delete
;
55
Thread
(
Thread
&&) =
delete
;
56
Thread
&
operator=
(
Thread
&&) =
delete
;
57
62
Context
context
;
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
81
private
:
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
Callbacks.hpp
Context.hpp
Memory.hpp
Position.hpp
Threading.hpp
util::memory::CacheLineSize
constexpr auto CacheLineSize
Definition
Memory.hpp:73
ben_bot::search
Definition
TranspositionTable.hpp:42
chess::uci
Definition
CommandParsing.hpp:43
ben_bot::search::Callbacks
Definition
Callbacks.hpp:46
ben_bot::search::Context
Definition
Context.hpp:47
ben_bot::search::Thread::operator=
Thread & operator=(const Thread &)=delete
ben_bot::search::Thread::operator=
Thread & operator=(Thread &&)=delete
ben_bot::search::Thread::~Thread
~Thread()
ben_bot::search::Thread::Thread
Thread(Thread &&)=delete
ben_bot::search::Thread::Thread
Thread(const Thread &)=delete
ben_bot::search::Thread::context
Context context
Definition
Thread.hpp:62
ben_bot::search::Thread::start
void start()
Definition
Thread.hpp:68
ben_bot::search::Thread::Thread
Thread(Callbacks &&callbacksToUse)
ben_bot::search::Thread::Thread
Thread()=default
chess::uci::GoCommandOptions
Definition
CommandParsing.hpp:92
libbenbot
include
libbenbot
search
Thread.hpp
Generated on
for BenBot by
1.16.1