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&) {}
37#if defined(BOTAN_TARGET_OS_IS_MINGW)
41#elif defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
47 return std::optional<size_t>(0);
55 static Thread_Pool g_thread_pool(global_thread_pool_size());
62 const std::string tname =
"Botan thread";
64 if(!opt_pool_size.has_value()) {
68 size_t pool_size = opt_pool_size.value();
87 m_workers.resize(pool_size);
89 for(
size_t i = 0; i != pool_size; ++i) {
90 m_workers[i] = std::thread(&Thread_Pool::worker_thread,
this);
91 OS::set_thread_name(m_workers[i], tname);
97 std::unique_lock<std::mutex>
lock(m_mutex);
99 if(m_shutdown ==
true) {
105 m_more_tasks.notify_all();
108 for(
auto&& thread : m_workers) {
115 std::unique_lock<std::mutex>
lock(m_mutex);
118 throw Invalid_State(
"Cannot add work after thread pool has shut down");
121 if(m_workers.empty()) {
125 m_tasks.push_back(fn);
126 m_more_tasks.notify_one();
129void Thread_Pool::worker_thread() {
131 std::function<void()> task;
134 std::unique_lock<std::mutex>
lock(m_mutex);
135 m_more_tasks.wait(
lock, [
this] {
return m_shutdown || !m_tasks.empty(); });
137 if(m_tasks.empty()) {
145 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)