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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 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
   // If it was neither a number nor a special value, then ignore it.
30
   return std::optional<size_t>(0);
747✔
31
}
747✔
32

33
}  // namespace
34

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

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

44
   if(!opt_pool_size.has_value())
749✔
45
      return;
46

47
   size_t pool_size = opt_pool_size.value();
749✔
48

49
   if(pool_size == 0) {
749✔
50
      pool_size = OS::get_cpu_available();
748✔
51

52
      // Unclear if this can happen, but be defensive
53
      if(pool_size == 0)
748✔
54
         pool_size = 2;
55

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

64
   for(size_t i = 0; i != pool_size; ++i) {
2,261✔
65
      m_workers.push_back(std::thread(&Thread_Pool::worker_thread, this));
3,024✔
66
   }
67
}
×
68

69
void Thread_Pool::shutdown() {
751✔
70
   {
751✔
71
      std::unique_lock<std::mutex> lock(m_mutex);
751✔
72

73
      if(m_shutdown == true)
751✔
74
         return;
2✔
75

76
      m_shutdown = true;
749✔
77

78
      m_more_tasks.notify_all();
749✔
79
   }
2✔
80

81
   for(auto&& thread : m_workers) {
2,261✔
82
      thread.join();
1,512✔
83
   }
84
   m_workers.clear();
749✔
85
}
86

87
void Thread_Pool::queue_thunk(const std::function<void()>& fn) {
204,880✔
88
   std::unique_lock<std::mutex> lock(m_mutex);
204,880✔
89

90
   if(m_shutdown)
204,880✔
91
      throw Invalid_State("Cannot add work after thread pool has shut down");
×
92

93
   if(m_workers.empty()) {
204,880✔
94
      return fn();
×
95
   }
96

97
   m_tasks.push_back(fn);
204,880✔
98
   m_more_tasks.notify_one();
204,880✔
99
}
204,880✔
100

101
void Thread_Pool::worker_thread() {
1,512✔
102
   for(;;) {
206,392✔
103
      std::function<void()> task;
206,392✔
104

105
      {
206,392✔
106
         std::unique_lock<std::mutex> lock(m_mutex);
206,392✔
107
         m_more_tasks.wait(lock, [this] { return m_shutdown || !m_tasks.empty(); });
223,064✔
108

109
         if(m_tasks.empty()) {
206,392✔
110
            if(m_shutdown)
1,512✔
111
               return;
1,512✔
112
            else
113
               continue;
×
114
         }
115

116
         task = m_tasks.front();
204,880✔
117
         m_tasks.pop_front();
204,880✔
118
      }
1,512✔
119

120
      task();
409,760✔
121
   }
206,392✔
122
}
123

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