Agora  1.2.0
Agora project
thread_pool.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
8 #include <spdlog/details/os.h>
9 
10 #include <chrono>
11 #include <memory>
12 #include <thread>
13 #include <vector>
14 #include <functional>
15 
16 namespace spdlog {
17 class async_logger;
18 
19 namespace details {
20 
21 using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
22 
23 enum class async_msg_type
24 {
25  log,
26  flush,
27  terminate
28 };
29 
30 // Async msg to move to/from the queue
31 // Movable only. should never be copied
33 {
36 
37  async_msg() = default;
38  ~async_msg() = default;
39 
40  // should only be moved in or out of the queue..
41  async_msg(const async_msg &) = delete;
42 
43 // support for vs2013 move
44 #if defined(_MSC_VER) && _MSC_VER <= 1800
45  async_msg(async_msg &&other)
46  : log_msg_buffer(std::move(other))
47  , msg_type(other.msg_type)
48  , worker_ptr(std::move(other.worker_ptr))
49  {}
50 
51  async_msg &operator=(async_msg &&other)
52  {
53  *static_cast<log_msg_buffer *>(this) = std::move(other);
54  msg_type = other.msg_type;
55  worker_ptr = std::move(other.worker_ptr);
56  return *this;
57  }
58 #else // (_MSC_VER) && _MSC_VER <= 1800
59  async_msg(async_msg &&) = default;
60  async_msg &operator=(async_msg &&) = default;
61 #endif
62 
63  // construct from log_msg with given type
65  : log_msg_buffer{m}
66  , msg_type{the_type}
67  , worker_ptr{std::move(worker)}
68  {}
69 
71  : log_msg_buffer{}
72  , msg_type{the_type}
73  , worker_ptr{std::move(worker)}
74  {}
75 
76  explicit async_msg(async_msg_type the_type)
77  : async_msg{nullptr, the_type}
78  {}
79 };
80 
82 {
83 public:
86 
87  thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop);
88  thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
89  thread_pool(size_t q_max_items, size_t threads_n);
90 
91  // message all threads to terminate gracefully and join them
92  ~thread_pool();
93 
94  thread_pool(const thread_pool &) = delete;
95  thread_pool &operator=(thread_pool &&) = delete;
96 
97  void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
98  void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
99  size_t overrun_counter();
100  size_t queue_size();
101 
102 private:
104 
105  std::vector<std::thread> threads_;
106 
107  void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
108  void worker_loop_();
109 
110  // process next message in the queue
111  // return true if this thread should still be active (while no terminate msg
112  // was received)
113  bool process_next_msg_();
114 };
115 
116 } // namespace details
117 } // namespace spdlog
118 
119 #ifdef SPDLOG_HEADER_ONLY
120 # include "thread_pool-inl.h"
121 #endif
spdlog::details::async_msg::async_msg
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
Definition: thread_pool.h:70
spdlog::details::async_msg::msg_type
async_msg_type msg_type
Definition: thread_pool.h:34
spdlog::details::async_msg_type
async_msg_type
Definition: thread_pool.h:23
spdlog::details::async_msg::async_msg
async_msg(async_msg_type the_type)
Definition: thread_pool.h:76
spdlog::details::async_msg::operator=
async_msg & operator=(async_msg &&)=default
mpmc_blocking_q.h
spdlog::details::mpmc_blocking_queue< item_type >
spdlog::details::async_logger_ptr
std::shared_ptr< spdlog::async_logger > async_logger_ptr
Definition: thread_pool.h:21
spdlog::details::async_msg::async_msg
async_msg()=default
spdlog::details::thread_pool
Definition: thread_pool.h:81
spdlog::details::thread_pool::threads_
std::vector< std::thread > threads_
Definition: thread_pool.h:105
spdlog
Definition: async.h:25
spdlog::details::async_msg::async_msg
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
Definition: thread_pool.h:64
spdlog::details::async_msg::~async_msg
~async_msg()=default
os.h
spdlog::details::log_msg_buffer
Definition: log_msg_buffer.h:14
spdlog::details::async_msg
Definition: thread_pool.h:32
spdlog::details::async_msg::worker_ptr
async_logger_ptr worker_ptr
Definition: thread_pool.h:35
thread_pool-inl.h
function
function[avg_proc_duration, std_proc_duration]
Definition: parse_dl_file.m:1
spdlog::details::thread_pool::q_
q_type q_
Definition: thread_pool.h:103
spdlog::details::log_msg
Definition: log_msg.h:11
std
Definition: json.hpp:5213
extract_version.m
m
Definition: extract_version.py:13
spdlog::thread_pool
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:95
log_msg_buffer.h
spdlog::details::async_msg_type::flush
@ flush
spdlog::details::async_msg_type::log
@ log
SPDLOG_API
#define SPDLOG_API
Definition: common.h:40
spdlog::async_overflow_policy
async_overflow_policy
Definition: async_logger.h:22
spdlog::details::async_msg_type::terminate
@ terminate