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 | |
6 | #include <spdlog/details/log_msg_buffer.h> |
7 | #include <spdlog/details/mpmc_blocking_q.h> |
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 |
32 | struct async_msg : log_msg_buffer |
33 | { |
34 | async_msg_type msg_type{async_msg_type::log}; |
35 | async_logger_ptr worker_ptr; |
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 |
64 | async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m) |
65 | : log_msg_buffer{m} |
66 | , msg_type{the_type} |
67 | , worker_ptr{std::move(worker)} |
68 | {} |
69 | |
70 | async_msg(async_logger_ptr &&worker, async_msg_type the_type) |
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 | |
81 | class SPDLOG_API thread_pool |
82 | { |
83 | public: |
84 | using item_type = async_msg; |
85 | using q_type = details::mpmc_blocking_queue<item_type>; |
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 | void reset_overrun_counter(); |
101 | size_t queue_size(); |
102 | |
103 | private: |
104 | q_type q_; |
105 | |
106 | std::vector<std::thread> threads_; |
107 | |
108 | void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy); |
109 | void worker_loop_(); |
110 | |
111 | // process next message in the queue |
112 | // return true if this thread should still be active (while no terminate msg |
113 | // was received) |
114 | bool process_next_msg_(); |
115 | }; |
116 | |
117 | } // namespace details |
118 | } // namespace spdlog |
119 | |
120 | #ifdef SPDLOG_HEADER_ONLY |
121 | # include "thread_pool-inl.h" |
122 | #endif |
123 | |