WireCellToolkit
Wire Cell Simulation, Signal Process and Reconstruction Toolki for Liquid Argon Detectors
thread_pool.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "spdlog/details/os.h"
7 
8 #include <chrono>
9 #include <memory>
10 #include <thread>
11 #include <vector>
12 
13 namespace spdlog {
14 namespace details {
15 
16 using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
17 
18 enum class async_msg_type
19 {
20  log,
21  flush,
22  terminate
23 };
24 
25 // Async msg to move to/from the queue
26 // Movable only. should never be copied
27 struct async_msg
28 {
31  log_clock::time_point time;
32  size_t thread_id;
33  fmt::basic_memory_buffer<char, 176> raw;
34 
35  size_t msg_id;
38 
39  async_msg() = default;
40  ~async_msg() = default;
41 
42  // should only be moved in or out of the queue..
43  async_msg(const async_msg &) = delete;
44 
45 // support for vs2013 move
46 #if defined(_MSC_VER) && _MSC_VER <= 1800
47  async_msg(async_msg &&other) SPDLOG_NOEXCEPT : msg_type(other.msg_type),
48  level(other.level),
49  time(other.time),
50  thread_id(other.thread_id),
51  raw(move(other.raw)),
52  msg_id(other.msg_id),
53  source(other.source),
54  worker_ptr(std::move(other.worker_ptr))
55  {
56  }
57 
58  async_msg &operator=(async_msg &&other) SPDLOG_NOEXCEPT
59  {
60  msg_type = other.msg_type;
61  level = other.level;
62  time = other.time;
63  thread_id = other.thread_id;
64  raw = std::move(other.raw);
65  msg_id = other.msg_id;
66  source = other.source;
67  worker_ptr = std::move(other.worker_ptr);
68  return *this;
69  }
70 #else // (_MSC_VER) && _MSC_VER <= 1800
71  async_msg(async_msg &&) = default;
72  async_msg &operator=(async_msg &&) = default;
73 #endif
74 
75  // construct from log_msg with given type
77  : msg_type(the_type)
78  , level(m.level)
79  , time(m.time)
80  , thread_id(m.thread_id)
81  , msg_id(m.msg_id)
82  , source(m.source)
83  , worker_ptr(std::move(worker))
84  {
86  }
87 
89  : msg_type(the_type)
90  , level(level::off)
91  , time()
92  , thread_id(0)
93  , msg_id(0)
94  , source()
95  , worker_ptr(std::move(worker))
96  {
97  }
98 
99  explicit async_msg(async_msg_type the_type)
100  : async_msg(nullptr, the_type)
101  {
102  }
103 
104  // copy into log_msg
106  {
107  log_msg msg(&worker_ptr->name(), level, string_view_t(raw.data(), raw.size()));
108  msg.time = time;
109  msg.thread_id = thread_id;
110  msg.msg_id = msg_id;
111  msg.source = source;
112  msg.color_range_start = 0;
113  msg.color_range_end = 0;
114  return msg;
115  }
116 };
117 
119 {
120 public:
123 
124  thread_pool(size_t q_max_items, size_t threads_n)
125  : q_(q_max_items)
126  {
127  // std::cout << "thread_pool() q_size_bytes: " << q_size_bytes <<
128  // "\tthreads_n: " << threads_n << std::endl;
129  if (threads_n == 0 || threads_n > 1000)
130  {
131  throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
132  "range is 1-1000)");
133  }
134  for (size_t i = 0; i < threads_n; i++)
135  {
136  threads_.emplace_back(&thread_pool::worker_loop_, this);
137  }
138  }
139 
140  // message all threads to terminate gracefully join them
142  {
143  try
144  {
145  for (size_t i = 0; i < threads_.size(); i++)
146  {
148  }
149 
150  for (auto &t : threads_)
151  {
152  t.join();
153  }
154  }
155  catch (...)
156  {
157  }
158  }
159 
160  thread_pool(const thread_pool &) = delete;
161  thread_pool &operator=(thread_pool &&) = delete;
162 
163  void post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy)
164  {
165  async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
166  post_async_msg_(std::move(async_m), overflow_policy);
167  }
168 
169  void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
170  {
171  post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
172  }
173 
175  {
176  return q_.overrun_counter();
177  }
178 
179 private:
180  q_type q_;
181 
182  std::vector<std::thread> threads_;
183 
184  void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
185  {
186  if (overflow_policy == async_overflow_policy::block)
187  {
188  q_.enqueue(std::move(new_msg));
189  }
190  else
191  {
192  q_.enqueue_nowait(std::move(new_msg));
193  }
194  }
195 
196  void worker_loop_()
197  {
198  while (process_next_msg_()) {};
199  }
200 
201  // process next message in the queue
202  // return true if this thread should still be active (while no terminate msg
203  // was received)
204  bool process_next_msg_()
205  {
206  async_msg incoming_async_msg;
207  bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
208  if (!dequeued)
209  {
210  return true;
211  }
212 
213  switch (incoming_async_msg.msg_type)
214  {
215  case async_msg_type::log:
216  {
217  auto msg = incoming_async_msg.to_log_msg();
218  incoming_async_msg.worker_ptr->backend_log_(msg);
219  return true;
220  }
222  {
223  incoming_async_msg.worker_ptr->backend_flush_();
224  return true;
225  }
226 
228  {
229  return false;
230  }
231  }
232  assert(false && "Unexpected async_msg_type");
233  return true;
234  }
235 };
236 
237 } // namespace details
238 } // namespace spdlog
thread_pool(size_t q_max_items, size_t threads_n)
Definition: thread_pool.h:124
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
Definition: thread_pool.h:169
async_overflow_policy
Definition: async_logger.h:32
async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &m)
Definition: thread_pool.h:76
If we are still before C++14, supply the fodder for doing the "indices trick".
Definition: format.h:297
log_clock::time_point time
Definition: thread_pool.h:31
async_logger_ptr worker_ptr
Definition: thread_pool.h:37
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
Definition: async.h:27
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:83
level::level_enum level
Definition: thread_pool.h:30
async_msg(async_msg_type the_type)
Definition: thread_pool.h:99
void append_string_view(spdlog::string_view_t view, fmt::basic_memory_buffer< char, Buffer_Size > &dest)
Definition: fmt_helper.h:30
fmt::basic_memory_buffer< char, 176 > raw
Definition: thread_pool.h:33
fmt::string_view string_view_t
Definition: common.h:88
size_t thread_id() SPDLOG_NOEXCEPT
Definition: os.h:339
std::shared_ptr< spdlog::async_logger > async_logger_ptr
Definition: thread_pool.h:16
const string_view_t payload
Definition: log_msg.h:52
#define SPDLOG_NOEXCEPT
Definition: common.h:35
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
Definition: thread_pool.h:88
async_msg_type msg_type
Definition: thread_pool.h:29
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy)
Definition: thread_pool.h:163
log_clock::time_point time
Definition: log_msg.h:43