Photon 1.0.0
Loading...
Searching...
No Matches
thread_pool-inl.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
6#ifndef SPDLOG_HEADER_ONLY
8#endif
9
10#include <spdlog/common.h>
11#include <cassert>
12
13namespace spdlog
14{
15 namespace details
16 {
17
19 size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
20 : q_(q_max_items)
21 {
22 if (threads_n == 0 || threads_n > 1000)
23 {
24 throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
25 "range is 1-1000)");
26 }
27 for (size_t i = 0; i < threads_n; i++)
28 {
29 threads_.emplace_back([this, on_thread_start, on_thread_stop] {
30 on_thread_start();
32 on_thread_stop();
33 });
34 }
35 }
36
37 SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start)
38 : thread_pool(q_max_items, threads_n, on_thread_start, [] {})
39 {
40 }
41
42 SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
44 q_max_items, threads_n, [] {}, [] {})
45 {
46 }
47
48 // message all threads to terminate gracefully join them
50 {
52 {
53 for (size_t i = 0; i < threads_.size(); i++)
54 {
56 }
57
58 for (auto& t : threads_)
59 {
60 t.join();
61 }
62 }
64 }
65
67 {
68 async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
69 post_async_msg_(std::move(async_m), overflow_policy);
70 }
71
73 {
74 post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
75 }
76
81
86
88 {
89 return q_.size();
90 }
91
93 {
94 if (overflow_policy == async_overflow_policy::block)
95 {
96 q_.enqueue(std::move(new_msg));
97 }
98 else
99 {
100 q_.enqueue_nowait(std::move(new_msg));
101 }
102 }
103
105 {
106 while (process_next_msg_())
107 {
108 }
109 }
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)
115 {
116 async_msg incoming_async_msg;
117 q_.dequeue(incoming_async_msg);
118
119 switch (incoming_async_msg.msg_type)
120 {
121 case async_msg_type::log: {
122 incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
123 return true;
124 }
126 incoming_async_msg.worker_ptr->backend_flush_();
127 return true;
128 }
129
131 return false;
132 }
133
134 default: {
135 assert(false);
136 }
137 }
138
139 return true;
140 }
141
142 } // namespace details
143} // namespace spdlog
void enqueue_nowait(T &&item)
Definition mpmc_blocking_q.h:46
size_t overrun_counter()
Definition mpmc_blocking_q.h:132
size_t size()
Definition mpmc_blocking_q.h:138
void enqueue(T &&item)
Definition mpmc_blocking_q.h:35
void dequeue(T &popped_item)
Definition mpmc_blocking_q.h:73
void reset_overrun_counter()
Definition mpmc_blocking_q.h:144
Definition thread_pool.h:82
bool process_next_msg_()
Definition thread_pool-inl.h:114
~thread_pool()
Definition thread_pool-inl.h:49
size_t overrun_counter()
Definition thread_pool-inl.h:77
q_type q_
Definition thread_pool.h:104
thread_pool(size_t q_max_items, size_t threads_n, std::function< void()> on_thread_start, std::function< void()> on_thread_stop)
Definition thread_pool-inl.h:18
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
Definition thread_pool-inl.h:66
void worker_loop_()
Definition thread_pool-inl.h:104
void reset_overrun_counter()
Definition thread_pool-inl.h:82
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
Definition thread_pool-inl.h:92
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
Definition thread_pool-inl.h:72
size_t queue_size()
Definition thread_pool-inl.h:87
std::vector< std::thread > threads_
Definition thread_pool.h:106
#define SPDLOG_CATCH_STD
Definition common.h:109
#define SPDLOG_TRY
Definition common.h:107
#define SPDLOG_INLINE
Definition common.h:47
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
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
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
Definition log_msg.h:14
annotation details
Definition tag_strings.h:125
i
Definition tag_strings.h:60