7#include <botan/internal/thread_pool.h>
9#include <botan/exceptn.h>
10#include <botan/internal/os_utils.h>
11#include <botan/internal/target_info.h>
19std::optional<size_t> global_thread_pool_size() {
28 return std::optional<size_t>(std::stoul(var,
nullptr));
29 }
catch(std::exception&) {}
39#if defined(BOTAN_TARGET_OS_IS_MINGW)
43#elif defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
49 return std::optional<size_t>(0);
57 static Thread_Pool g_thread_pool(global_thread_pool_size());
63 const std::string tname =
"Botan thread";
65 if(!opt_pool_size.has_value()) {
69 size_t pool_size = opt_pool_size.value();
76 const size_t cores = OS::get_cpu_available();
77 pool_size = std::clamp<size_t>(cores, 2, 16);
80 m_workers.resize(pool_size);
82 for(
size_t i = 0; i != pool_size; ++i) {
83 m_workers[i] = std::thread(&Thread_Pool::worker_thread, this);
84 OS::set_thread_name(m_workers[i], tname);
90 std::unique_lock<std::mutex>
lock(m_mutex);
98 m_more_tasks.notify_all();
101 for(
auto&& thread : m_workers) {
108 std::unique_lock<std::mutex>
lock(m_mutex);
111 throw Invalid_State(
"Cannot add work after thread pool has shut down");
114 if(m_workers.empty()) {
118 m_tasks.push_back(work);
119 m_more_tasks.notify_one();
122void Thread_Pool::worker_thread() {
124 std::function<void()> task;
127 std::unique_lock<std::mutex>
lock(m_mutex);
128 m_more_tasks.wait(
lock, [
this] {
return m_shutdown || !m_tasks.empty(); });
130 if(m_tasks.empty()) {
138 task = m_tasks.front();
Thread_Pool(std::optional< size_t > pool_size)
void queue_thunk(const std::function< void()> &work)
static Thread_Pool & global_instance()
bool read_env_variable(std::string &value_out, std::string_view var_name)
secure_vector< T > lock(const std::vector< T > &in)