aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/thread_utils/thread_pool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils/thread_utils/thread_pool.cpp')
-rw-r--r--src/lib/utils/thread_utils/thread_pool.cpp50
1 files changed, 42 insertions, 8 deletions
diff --git a/src/lib/utils/thread_utils/thread_pool.cpp b/src/lib/utils/thread_utils/thread_pool.cpp
index 405f79585..ce326fdc0 100644
--- a/src/lib/utils/thread_utils/thread_pool.cpp
+++ b/src/lib/utils/thread_utils/thread_pool.cpp
@@ -1,5 +1,5 @@
/*
-* (C) 2019 Jack Lloyd
+* (C) 2019,2021 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -11,19 +11,53 @@
namespace Botan {
+namespace {
+
+std::optional<size_t> global_thread_pool_size()
+ {
+ std::string var;
+ if(OS::read_env_variable(var, "BOTAN_THREAD_POOL_SIZE"))
+ {
+ try
+ {
+ return std::optional<size_t>(std::stoul(var, nullptr));
+ }
+ catch(std::exception&) { /* ignore it */ }
+
+ if(var == "none")
+ return std::nullopt;
+ }
+
+ // If it was neither a number nor a special value, then ignore it.
+ return std::optional<size_t>(0);
+ }
+
+}
+
//static
Thread_Pool& Thread_Pool::global_instance()
{
- static Thread_Pool g_thread_pool(OS::read_env_variable_sz("BOTAN_THREAD_POOL_SIZE"));
+ static Thread_Pool g_thread_pool(global_thread_pool_size());
return g_thread_pool;
}
-Thread_Pool::Thread_Pool(size_t pool_size)
+Thread_Pool::Thread_Pool(std::optional<size_t> opt_pool_size)
{
+ m_shutdown = false;
+
+ if(!opt_pool_size.has_value())
+ return;
+
+ size_t pool_size = opt_pool_size.value();
+
if(pool_size == 0)
{
pool_size = OS::get_cpu_available();
+ // Unclear if this can happen, but be defensive
+ if(pool_size == 0)
+ pool_size = 2;
+
/*
* For large machines don't create too many threads, unless
* explicitly asked to by the caller.
@@ -32,11 +66,6 @@ Thread_Pool::Thread_Pool(size_t pool_size)
pool_size = 16;
}
- if(pool_size <= 1)
- pool_size = 2;
-
- m_shutdown = false;
-
for(size_t i = 0; i != pool_size; ++i)
{
m_workers.push_back(std::thread(&Thread_Pool::worker_thread, this));
@@ -70,6 +99,11 @@ void Thread_Pool::queue_thunk(std::function<void ()> fn)
if(m_shutdown)
throw Invalid_State("Cannot add work after thread pool has shut down");
+ if(m_workers.empty())
+ {
+ return fn();
+ }
+
m_tasks.push_back(fn);
m_more_tasks.notify_one();
}