7#include <botan/internal/thread_pool.h>
9#include <botan/exceptn.h>
10#include <botan/internal/os_utils.h>
17std::optional<size_t> global_thread_pool_size() {
26 return std::optional<size_t>(std::stoul(var,
nullptr));
27 }
catch(std::exception&) {}
35#if defined(BOTAN_TARGET_OS_IS_MINGW)
38 return std::optional<size_t>(0);
46 static Thread_Pool g_thread_pool(global_thread_pool_size());
53 const std::string tname =
"Botan thread";
55 if(!opt_pool_size.has_value()) {
59 size_t pool_size = opt_pool_size.value();
78 m_workers.resize(pool_size);
80 for(
size_t i = 0; i != pool_size; ++i) {
81 m_workers[i] = std::thread(&Thread_Pool::worker_thread,
this);
82 OS::set_thread_name(m_workers[i], tname);
88 std::unique_lock<std::mutex>
lock(m_mutex);
90 if(m_shutdown ==
true) {
96 m_more_tasks.notify_all();
99 for(
auto&& thread : m_workers) {
106 std::unique_lock<std::mutex>
lock(m_mutex);
109 throw Invalid_State(
"Cannot add work after thread pool has shut down");
112 if(m_workers.empty()) {
116 m_tasks.push_back(fn);
117 m_more_tasks.notify_one();
120void Thread_Pool::worker_thread() {
122 std::function<void()> task;
125 std::unique_lock<std::mutex>
lock(m_mutex);
126 m_more_tasks.wait(
lock, [
this] {
return m_shutdown || !m_tasks.empty(); });
128 if(m_tasks.empty()) {
136 task = m_tasks.front();
void queue_thunk(const std::function< void()> &)
Thread_Pool(std::optional< size_t > pool_size)
static Thread_Pool & global_instance()
size_t BOTAN_TEST_API get_cpu_available()
bool read_env_variable(std::string &value_out, std::string_view var_name)
secure_vector< T > lock(const std::vector< T > &in)