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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

83.33
/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 <thread>
12

13
namespace Botan {
14

15
namespace {
16

17
std::optional<size_t> global_thread_pool_size() {
747✔
18
   std::string var;
747✔
19
   if(OS::read_env_variable(var, "BOTAN_THREAD_POOL_SIZE")) {
747✔
20
      try {
×
21
         return std::optional<size_t>(std::stoul(var, nullptr));
×
22
      } catch(std::exception&) { /* ignore it */
×
23
      }
×
24

25
      if(var == "none") {
×
26
         return std::nullopt;
×
27
      }
28
   }
29

30
   // If it was neither a number nor a special value, then ignore it.
31
   return std::optional<size_t>(0);
747✔
32
}
747✔
33

34
}  // namespace
35

36
//static
37
Thread_Pool& Thread_Pool::global_instance() {
6,312✔
38
   static Thread_Pool g_thread_pool(global_thread_pool_size());
6,312✔
39
   return g_thread_pool;
6,312✔
40
}
41

42
Thread_Pool::Thread_Pool(std::optional<size_t> opt_pool_size) {
749✔
43
   m_shutdown = false;
749✔
44

45
   if(!opt_pool_size.has_value()) {
749✔
46
      return;
47
   }
48

49
   size_t pool_size = opt_pool_size.value();
749✔
50

51
   if(pool_size == 0) {
749✔
52
      pool_size = OS::get_cpu_available();
748✔
53

54
      // Unclear if this can happen, but be defensive
55
      if(pool_size == 0) {
748✔
56
         pool_size = 2;
57
      }
58

59
      /*
60
      * For large machines don't create too many threads, unless
61
      * explicitly asked to by the caller.
62
      */
63
      if(pool_size > 16) {
748✔
64
         pool_size = 16;
65
      }
66
   }
67

68
   for(size_t i = 0; i != pool_size; ++i) {
2,261✔
69
      m_workers.push_back(std::thread(&Thread_Pool::worker_thread, this));
3,024✔
70
   }
71
}
×
72

73
void Thread_Pool::shutdown() {
751✔
74
   {
751✔
75
      std::unique_lock<std::mutex> lock(m_mutex);
751✔
76

77
      if(m_shutdown == true) {
751✔
78
         return;
2✔
79
      }
80

81
      m_shutdown = true;
749✔
82

83
      m_more_tasks.notify_all();
749✔
84
   }
2✔
85

86
   for(auto&& thread : m_workers) {
2,261✔
87
      thread.join();
1,512✔
88
   }
89
   m_workers.clear();
749✔
90
}
91

92
void Thread_Pool::queue_thunk(const std::function<void()>& fn) {
204,883✔
93
   std::unique_lock<std::mutex> lock(m_mutex);
204,883✔
94

95
   if(m_shutdown) {
204,883✔
96
      throw Invalid_State("Cannot add work after thread pool has shut down");
×
97
   }
98

99
   if(m_workers.empty()) {
204,883✔
100
      return fn();
×
101
   }
102

103
   m_tasks.push_back(fn);
204,883✔
104
   m_more_tasks.notify_one();
204,883✔
105
}
204,883✔
106

107
void Thread_Pool::worker_thread() {
1,512✔
108
   for(;;) {
206,395✔
109
      std::function<void()> task;
206,395✔
110

111
      {
206,395✔
112
         std::unique_lock<std::mutex> lock(m_mutex);
206,395✔
113
         m_more_tasks.wait(lock, [this] { return m_shutdown || !m_tasks.empty(); });
224,679✔
114

115
         if(m_tasks.empty()) {
206,395✔
116
            if(m_shutdown) {
1,512✔
117
               return;
1,512✔
118
            } else {
119
               continue;
×
120
            }
121
         }
122

123
         task = m_tasks.front();
204,883✔
124
         m_tasks.pop_front();
204,883✔
125
      }
1,512✔
126

127
      task();
409,766✔
128
   }
206,395✔
129
}
130

131
}  // 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

© 2025 Coveralls, Inc