Botan 3.5.0
Crypto and TLS for C&
thread_pool.cpp
Go to the documentation of this file.
1/*
2* (C) 2019,2021 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/thread_pool.h>
8
9#include <botan/exceptn.h>
10#include <botan/internal/os_utils.h>
11#include <thread>
12
13namespace Botan {
14
15namespace {
16
17std::optional<size_t> global_thread_pool_size() {
18 std::string var;
19 if(OS::read_env_variable(var, "BOTAN_THREAD_POOL_SIZE")) {
20 if(var == "none") {
21 return std::nullopt;
22 }
23
24 // Try to convert to an integer if possible:
25 try {
26 return std::optional<size_t>(std::stoul(var, nullptr));
27 } catch(std::exception&) {}
28
29 // If it was neither a number nor a special value, then ignore the env
30 }
31
32 // On MinGW by default (ie unless requested via env) we disable the global
33 // thread pool due to bugs causing deadlock on application exit.
34 // See https://github.com/randombit/botan/issues/2582 for background.
35#if defined(BOTAN_TARGET_OS_IS_MINGW)
36 return std::nullopt;
37#else
38 return std::optional<size_t>(0);
39#endif
40}
41
42} // namespace
43
44//static
46 static Thread_Pool g_thread_pool(global_thread_pool_size());
47 return g_thread_pool;
48}
49
50Thread_Pool::Thread_Pool(std::optional<size_t> opt_pool_size) {
51 m_shutdown = false;
52 // On Linux, it is 16 length max, including terminator
53 const std::string tname = "Botan thread";
54
55 if(!opt_pool_size.has_value()) {
56 return;
57 }
58
59 size_t pool_size = opt_pool_size.value();
60
61 if(pool_size == 0) {
62 pool_size = OS::get_cpu_available();
63
64 // Unclear if this can happen, but be defensive
65 if(pool_size == 0) {
66 pool_size = 2;
67 }
68
69 /*
70 * For large machines don't create too many threads, unless
71 * explicitly asked to by the caller.
72 */
73 if(pool_size > 16) {
74 pool_size = 16;
75 }
76 }
77
78 m_workers.resize(pool_size);
79
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);
83 }
84}
85
87 {
88 std::unique_lock<std::mutex> lock(m_mutex);
89
90 if(m_shutdown == true) {
91 return;
92 }
93
94 m_shutdown = true;
95
96 m_more_tasks.notify_all();
97 }
98
99 for(auto&& thread : m_workers) {
100 thread.join();
101 }
102 m_workers.clear();
103}
104
105void Thread_Pool::queue_thunk(const std::function<void()>& fn) {
106 std::unique_lock<std::mutex> lock(m_mutex);
107
108 if(m_shutdown) {
109 throw Invalid_State("Cannot add work after thread pool has shut down");
110 }
111
112 if(m_workers.empty()) {
113 return fn();
114 }
115
116 m_tasks.push_back(fn);
117 m_more_tasks.notify_one();
118}
119
120void Thread_Pool::worker_thread() {
121 for(;;) {
122 std::function<void()> task;
123
124 {
125 std::unique_lock<std::mutex> lock(m_mutex);
126 m_more_tasks.wait(lock, [this] { return m_shutdown || !m_tasks.empty(); });
127
128 if(m_tasks.empty()) {
129 if(m_shutdown) {
130 return;
131 } else {
132 continue;
133 }
134 }
135
136 task = m_tasks.front();
137 m_tasks.pop_front();
138 }
139
140 task();
141 }
142}
143
144} // namespace Botan
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()
Definition os_utils.cpp:236
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:431
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:70