Photon 1.0.0
Loading...
Searching...
No Matches
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
16namespace spdlog
17{
18 class async_logger;
19
20 namespace details
21 {
22
23 using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
24
25 enum class async_msg_type
26 {
27 log,
28 flush,
30 };
31
32 // Async msg to move to/from the queue
33 // Movable only. should never be copied
35 {
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)
48 : log_msg_buffer(std::move(other)), msg_type(other.msg_type), worker_ptr(std::move(other.worker_ptr))
49 {
50 }
51
53 {
54 *static_cast<log_msg_buffer*>(this) = std::move(other);
55 msg_type = other.msg_type;
56 worker_ptr = std::move(other.worker_ptr);
57 return *this;
58 }
59#else // (_MSC_VER) && _MSC_VER <= 1800
60 async_msg(async_msg&&) = default;
62#endif
63
64 // construct from log_msg with given type
66 : log_msg_buffer{m}, msg_type{the_type}, worker_ptr{std::move(worker)}
67 {
68 }
69
71 : log_msg_buffer{}, msg_type{the_type}, worker_ptr{std::move(worker)}
72 {
73 }
74
75 explicit async_msg(async_msg_type the_type)
76 : async_msg{nullptr, the_type}
77 {
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
93
94 thread_pool(const 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:
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
Definition log_msg_buffer.h:17
Definition thread_pool.h:82
q_type q_
Definition thread_pool.h:104
thread_pool(const thread_pool &)=delete
thread_pool & operator=(thread_pool &&)=delete
std::vector< std::thread > threads_
Definition thread_pool.h:106
#define SPDLOG_API
Definition common.h:45
async_msg_type
Definition thread_pool.h:26
std::shared_ptr< spdlog::async_logger > async_logger_ptr
Definition thread_pool.h:23
Definition async.h:26
async_overflow_policy
Definition async_logger.h:24
Definition uuid.h:926
Definition thread_pool.h:35
async_msg_type msg_type
Definition thread_pool.h:36
async_logger_ptr worker_ptr
Definition thread_pool.h:37
async_msg(async_msg &&)=default
async_msg(async_msg_type the_type)
Definition thread_pool.h:75
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
Definition thread_pool.h:65
async_msg(const async_msg &)=delete
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
Definition thread_pool.h:70
async_msg & operator=(async_msg &&)=default
Definition log_msg.h:14
annotation details
Definition tag_strings.h:125