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 <functional>
12#include <future>
13#include <memory>
14#include <thread>
15#include <vector>
16
17namespace spdlog {
18class async_logger;
19
20namespace details {
21
22using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
23
24enum class async_msg_type { log, flush, terminate };
25
26// Async msg to move to/from the queue
27// Movable only. should never be copied
28struct async_msg : log_msg_buffer {
29 async_msg_type msg_type{async_msg_type::log};
30 async_logger_ptr worker_ptr;
31 std::promise<void> flush_promise;
32
33 async_msg() = default;
34 ~async_msg() = default;
35
36 // should only be moved in or out of the queue..
37 async_msg(const async_msg &) = delete;
38
39// support for vs2013 move
40#if defined(_MSC_VER) && _MSC_VER <= 1800
41 async_msg(async_msg &&other)
42 : log_msg_buffer(std::move(other)),
43 msg_type(other.msg_type),
44 worker_ptr(std::move(other.worker_ptr)) {}
45
46 async_msg &operator=(async_msg &&other) {
47 *static_cast<log_msg_buffer *>(this) = std::move(other);
48 msg_type = other.msg_type;
49 worker_ptr = std::move(other.worker_ptr);
50 return *this;
51 }
52#else // (_MSC_VER) && _MSC_VER <= 1800
53 async_msg(async_msg &&) = default;
54 async_msg &operator=(async_msg &&) = default;
55#endif
56
57 // construct from log_msg with given type
58 async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
59 : log_msg_buffer{m},
60 msg_type{the_type},
61 worker_ptr{std::move(worker)},
62 flush_promise{} {}
63
64 async_msg(async_logger_ptr &&worker, async_msg_type the_type)
65 : log_msg_buffer{},
66 msg_type{the_type},
67 worker_ptr{std::move(worker)},
68 flush_promise{} {}
69
70 async_msg(async_logger_ptr &&worker, async_msg_type the_type, std::promise<void> &&promise)
71 : log_msg_buffer{},
72 msg_type{the_type},
73 worker_ptr{std::move(worker)},
74 flush_promise{std::move(promise)} {}
75
76 explicit async_msg(async_msg_type the_type)
77 : async_msg{nullptr, the_type} {}
78};
79
80class SPDLOG_API thread_pool {
81public:
82 using item_type = async_msg;
83 using q_type = details::mpmc_blocking_queue<item_type>;
84
85 thread_pool(size_t q_max_items,
86 size_t threads_n,
87 std::function<void()> on_thread_start,
88 std::function<void()> on_thread_stop);
89 thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
90 thread_pool(size_t q_max_items, size_t threads_n);
91
92 // message all threads to terminate gracefully and join them
93 ~thread_pool();
94
95 thread_pool(const thread_pool &) = delete;
96 thread_pool &operator=(thread_pool &&) = delete;
97
98 void post_log(async_logger_ptr &&worker_ptr,
99 const details::log_msg &msg,
100 async_overflow_policy overflow_policy);
101 std::future<void> post_flush(async_logger_ptr &&worker_ptr,
102 async_overflow_policy overflow_policy);
103 size_t overrun_counter();
104 void reset_overrun_counter();
105 size_t discard_counter();
106 void reset_discard_counter();
107 size_t queue_size();
108
109private:
110 q_type q_;
111
112 std::vector<std::thread> threads_;
113
114 void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
115 void worker_loop_();
116
117 // process next message in the queue
118 // return true if this thread should still be active (while no terminate msg
119 // was received)
120 bool process_next_msg_();
121};
122
123} // namespace details
124} // namespace spdlog
125
126#ifdef SPDLOG_HEADER_ONLY
127 #include "thread_pool-inl.h"
128#endif
129