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

randombit / botan / 16247897882

13 Jul 2025 09:20AM UTC coverage: 90.575% (+0.001%) from 90.574%
16247897882

push

github

web-flow
Merge pull request #4979 from randombit/jack/clang-tidy-readability-use-std-min-max

Enable and fix clang-tidy warning readability-use-std-min-max

99095 of 109406 relevant lines covered (90.58%)

12348199.05 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() {
795✔
20
   std::string var;
795✔
21
   if(OS::read_env_variable(var, "BOTAN_THREAD_POOL_SIZE")) {
795✔
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);
794✔
50
#endif
51
}
795✔
52

53
}  // namespace
54

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

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

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

70
   size_t pool_size = opt_pool_size.value();
796✔
71

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

81
   m_workers.resize(pool_size);
796✔
82

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

89
void Thread_Pool::shutdown() {
799✔
90
   {
799✔
91
      std::unique_lock<std::mutex> lock(m_mutex);
799✔
92

93
      if(m_shutdown == true) {
799✔
94
         return;
2✔
95
      }
96

97
      m_shutdown = true;
797✔
98

99
      m_more_tasks.notify_all();
797✔
100
   }
2✔
101

102
   for(auto&& thread : m_workers) {
3,993✔
103
      thread.join();
3,196✔
104
   }
105
   m_workers.clear();
797✔
106
}
107

108
void Thread_Pool::queue_thunk(const std::function<void()>& fn) {
213,250✔
109
   std::unique_lock<std::mutex> lock(m_mutex);
213,250✔
110

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

115
   if(m_workers.empty()) {
213,250✔
116
      return fn();
256✔
117
   }
118

119
   m_tasks.push_back(fn);
213,122✔
120
   m_more_tasks.notify_one();
213,122✔
121
}
213,250✔
122

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

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

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

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

143
      task();
426,244✔
144
   }
216,318✔
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