• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 23992310984

05 Apr 2026 02:14AM UTC coverage: 89.447% (-0.007%) from 89.454%
23992310984

Pull #5521

github

web-flow
Merge c18078031 into 417709dd7
Pull Request #5521: Rollup of small fixes

105767 of 118245 relevant lines covered (89.45%)

11566159.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.94
/src/lib/utils/thread_utils/thread_pool.cpp
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 <botan/internal/target_info.h>
12
#include <algorithm>
13
#include <thread>
14

15
namespace Botan {
16

17
namespace {
18

19
std::optional<size_t> global_thread_pool_size() {
787✔
20
   std::string var;
787✔
21
   if(OS::read_env_variable(var, "BOTAN_THREAD_POOL_SIZE")) {
787✔
22
      if(var == "none") {
1✔
23
         return std::nullopt;
1✔
24
      }
25

26
      // Try to convert to an integer if possible:
27
      try {
×
28
         return std::optional<size_t>(std::stoul(var, nullptr));
×
29
      } catch(std::exception&) {}
×
30

31
      // If it was neither a number nor a special value, then ignore the env
32
   }
33

34
   /*
35
   * On a few platforms, disable the thread pool by default; it is only
36
   * used if a size is set explicitly in the environment.
37
   */
38

39
#if defined(BOTAN_TARGET_OS_IS_MINGW)
40
   // MinGW seems to have bugs causing deadlock on application exit.
41
   // See https://github.com/randombit/botan/issues/2582 for background.
42
   return std::nullopt;
43
#elif defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
44
   // Emscripten's threads are reportedly problematic
45
   // See https://github.com/randombit/botan/issues/4195
46
   return std::nullopt;
47
#else
48
   // Some(0) means choose based on CPU count
49
   return std::optional<size_t>(0);
786✔
50
#endif
51
}
787✔
52

53
}  // namespace
54

55
//static
56
Thread_Pool& Thread_Pool::global_instance() {
6,770✔
57
   static Thread_Pool g_thread_pool(global_thread_pool_size());
6,770✔
58
   return g_thread_pool;
6,770✔
59
}
60

61
Thread_Pool::Thread_Pool(std::optional<size_t> opt_pool_size) : m_shutdown(false) {
789✔
62
   // On Linux, it is 16 length max, including terminator
63
   const std::string tname = "Botan thread";
789✔
64

65
   if(!opt_pool_size.has_value()) {
789✔
66
      return;
1✔
67
   }
68

69
   size_t pool_size = opt_pool_size.value();
788✔
70

71
   if(pool_size == 0) {
788✔
72
      /*
73
      * For large machines don't create too many threads, unless
74
      * explicitly asked to by the caller.
75
      */
76
      const size_t cores = OS::get_cpu_available();
787✔
77
      pool_size = std::clamp<size_t>(cores, 2, 16);
1,574✔
78
   }
79

80
   m_workers.resize(pool_size);
788✔
81

82
   for(size_t i = 0; i != pool_size; ++i) {
3,952✔
83
      m_workers[i] = std::thread(&Thread_Pool::worker_thread, this);
3,164✔
84
      OS::set_thread_name(m_workers[i], tname);
3,164✔
85
   }
86
}
789✔
87

88
void Thread_Pool::shutdown() {
791✔
89
   {
791✔
90
      const std::unique_lock<std::mutex> lock(m_mutex);
791✔
91

92
      if(m_shutdown) {
791✔
93
         return;
2✔
94
      }
95

96
      m_shutdown = true;
789✔
97

98
      m_more_tasks.notify_all();
789✔
99
   }
2✔
100

101
   for(auto&& thread : m_workers) {
3,953✔
102
      thread.join();
3,164✔
103
   }
104
   m_workers.clear();
789✔
105
}
106

107
void Thread_Pool::queue_thunk(const std::function<void()>& work) {
213,520✔
108
   std::unique_lock<std::mutex> lock(m_mutex);
213,520✔
109

110
   if(m_shutdown) {
213,520✔
111
      throw Invalid_State("Cannot add work after thread pool has shut down");
×
112
   }
113

114
   if(m_workers.empty()) {
213,520✔
115
      lock.unlock();
128✔
116
      return work();
256✔
117
   }
118

119
   m_tasks.push_back(work);
213,392✔
120
   m_more_tasks.notify_one();
213,392✔
121
}
213,520✔
122

123
void Thread_Pool::worker_thread() {
3,164✔
124
   for(;;) {
216,556✔
125
      std::function<void()> task;
216,556✔
126

127
      {
216,556✔
128
         std::unique_lock<std::mutex> lock(m_mutex);
216,556✔
129
         m_more_tasks.wait(lock, [this] { return m_shutdown || !m_tasks.empty(); });
250,578✔
130

131
         if(m_tasks.empty()) {
216,556✔
132
            if(m_shutdown) {
3,164✔
133
               return;
3,164✔
134
            } else {
135
               continue;
×
136
            }
137
         }
138

139
         task = m_tasks.front();
213,392✔
140
         m_tasks.pop_front();
213,392✔
141
      }
3,164✔
142

143
      task();
426,784✔
144
   }
216,556✔
145
}
146

147
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc