7#include <botan/internal/thread_pool.h>
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/os_utils.h>
12#include <botan/internal/parsing.h>
13#include <botan/internal/target_info.h>
15#include <condition_variable>
31 explicit Internal_Thread_Pool(std::optional<size_t> pool_size) :
32 m_pool_size(resolve_pool_size(pool_size)), m_shutdown(false) {}
35 ~Internal_Thread_Pool()
override {
37 const std::scoped_lock
lock(m_mutex);
40 m_more_tasks.notify_all();
42 for(
auto& worker : m_workers) {
47 Internal_Thread_Pool(
const Internal_Thread_Pool&) =
delete;
48 Internal_Thread_Pool& operator=(
const Internal_Thread_Pool&) =
delete;
49 Internal_Thread_Pool(Internal_Thread_Pool&&) =
delete;
50 Internal_Thread_Pool& operator=(Internal_Thread_Pool&&) =
delete;
52 void queue_work(std::function<
void()> work)
override;
54 size_t max_concurrency()
const override {
return m_pool_size; }
57 static size_t resolve_pool_size(std::optional<size_t> pool_size);
61 std::function<void()> next_task();
63 const size_t m_pool_size;
66 std::condition_variable m_more_tasks;
67 std::deque<std::function<void()>> m_tasks;
68 std::vector<std::thread> m_workers;
73size_t Internal_Thread_Pool::resolve_pool_size(std::optional<size_t> pool_size) {
74 if(!pool_size.has_value()) {
76 }
else if(pool_size.value() == 0) {
78 return std::clamp<size_t>(OS::get_cpu_available(), 2, 16);
80 return pool_size.value();
84void Internal_Thread_Pool::queue_work(std::function<
void()> work) {
88 if(m_pool_size == 0) {
92 const std::scoped_lock
lock(m_mutex);
94 if(m_workers.empty()) {
98 m_tasks.push_back(std::move(work));
99 m_more_tasks.notify_one();
103void Internal_Thread_Pool::start_workers() {
105 const std::string tname =
"Botan thread";
107 m_workers.reserve(m_pool_size);
111 for(
size_t i = 0; i != m_pool_size; ++i) {
112 m_workers.emplace_back(&Internal_Thread_Pool::worker_thread,
this);
113 OS::set_thread_name(m_workers.back(), tname);
117void Internal_Thread_Pool::worker_thread() {
118 while(
const auto task = next_task()) {
123std::function<void()> Internal_Thread_Pool::next_task() {
124 std::unique_lock<std::mutex>
lock(m_mutex);
125 m_more_tasks.wait(lock, [
this] {
return m_shutdown || !m_tasks.empty(); });
128 if(m_tasks.empty()) {
132 auto task = std::move(m_tasks.front());
137class Running_Pool_Work;
140thread_local const Running_Pool_Work* g_innermost_pool_work =
nullptr;
148class Running_Pool_Work final {
150 explicit Running_Pool_Work(
const Thread_Pool* pool) : m_pool(pool), m_outer(g_innermost_pool_work) {
151 g_innermost_pool_work =
this;
154 ~Running_Pool_Work() { g_innermost_pool_work = m_outer; }
156 Running_Pool_Work(
const Running_Pool_Work&) =
delete;
157 Running_Pool_Work& operator=(
const Running_Pool_Work&) =
delete;
158 Running_Pool_Work(Running_Pool_Work&&) =
delete;
159 Running_Pool_Work& operator=(Running_Pool_Work&&) =
delete;
162 static bool active_for(
const Thread_Pool* pool) {
163 for(
const auto* work = g_innermost_pool_work; work !=
nullptr; work = work->m_outer) {
164 if(work->m_pool == pool) {
172 const Thread_Pool* m_pool;
173 const Running_Pool_Work* m_outer;
176std::optional<size_t> global_thread_pool_size() {
196#if defined(BOTAN_TARGET_OS_IS_MINGW)
200#elif defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
206 return std::optional<size_t>(0);
214 static Thread_Pool g_thread_pool(global_thread_pool_size());
215 return g_thread_pool;
224 m_executor(std::make_shared<Internal_Thread_Pool>(opt_pool_size)),
225 m_worker_count(m_executor->max_concurrency()),
226 m_executor_was_set(false) {}
229 const std::scoped_lock
lock(m_mutex);
230 return m_worker_count;
234 BOTAN_ARG_CHECK(executor !=
nullptr,
"Thread_Pool::set_executor executor must not be null");
237 if(Running_Pool_Work::active_for(
this)) {
238 throw Invalid_State(
"Cannot set the executor from within work run by the thread pool");
242 const size_t concurrency = executor->max_concurrency();
246 const auto internal_pool = [&]() {
247 const std::scoped_lock
lock(m_mutex);
249 if(m_executor_was_set) {
250 throw Invalid_State(
"Thread pool executor has already been set");
254 throw Invalid_State(
"Cannot set the executor after the thread pool has shut down");
257 m_executor_was_set =
true;
258 m_worker_count = concurrency;
259 return std::exchange(m_executor, std::move(executor));
266 const auto executor = [
this]() {
267 const std::scoped_lock
lock(m_mutex);
269 return std::exchange(m_executor,
nullptr);
285 if(Running_Pool_Work::active_for(
this)) {
290 const auto executor = [
this]() {
291 const std::scoped_lock
lock(m_mutex);
294 throw Invalid_State(
"Cannot add work after thread pool has shut down");
302 const auto wrapped = [
this, work]() {
303 const Running_Pool_Work scope(
this);
308 executor->queue_work(wrapped);
#define BOTAN_ARG_CHECK(expr, msg)
size_t worker_count() const
Thread_Pool(std::optional< size_t > pool_size)
void queue_thunk(const std::function< void()> &work)
void set_executor(std::shared_ptr< Work_Executor > executor)
static Thread_Pool & global_instance()
static void set_global(std::shared_ptr< Work_Executor > executor)
bool read_env_variable(std::string &value_out, std::string_view var_name)
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
secure_vector< T > lock(const std::vector< T > &in)