File indexing completed on 2026-09-14 09:25:09
0001
0002
0003
0004 #pragma once
0005
0006 #ifndef SPDLOG_HEADER_ONLY
0007 #include <spdlog/details/thread_pool.h>
0008 #endif
0009
0010 #include <cassert>
0011 #include <spdlog/common.h>
0012
0013 namespace spdlog {
0014 namespace details {
0015
0016 SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items,
0017 size_t threads_n,
0018 std::function<void()> on_thread_start,
0019 std::function<void()> on_thread_stop)
0020 : q_(q_max_items) {
0021 if (threads_n == 0 || threads_n > 1000) {
0022 throw_spdlog_ex(
0023 "spdlog::thread_pool(): invalid threads_n param (valid "
0024 "range is 1-1000)");
0025 }
0026 for (size_t i = 0; i < threads_n; i++) {
0027 threads_.emplace_back([this, on_thread_start, on_thread_stop] {
0028 on_thread_start();
0029 this->thread_pool::worker_loop_();
0030 on_thread_stop();
0031 });
0032 }
0033 }
0034
0035 SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items,
0036 size_t threads_n,
0037 std::function<void()> on_thread_start)
0038 : thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {}
0039
0040 SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
0041 : thread_pool(q_max_items, threads_n, [] {}, [] {}) {}
0042
0043
0044 SPDLOG_INLINE thread_pool::~thread_pool() {
0045 SPDLOG_TRY {
0046 for (size_t i = 0; i < threads_.size(); i++) {
0047 post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
0048 }
0049
0050 for (auto &t : threads_) {
0051 t.join();
0052 }
0053 }
0054 SPDLOG_CATCH_STD
0055 }
0056
0057 void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr,
0058 const details::log_msg &msg,
0059 async_overflow_policy overflow_policy) {
0060 async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
0061 post_async_msg_(std::move(async_m), overflow_policy);
0062 }
0063
0064 void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr,
0065 async_overflow_policy overflow_policy) {
0066 post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
0067 }
0068
0069 size_t SPDLOG_INLINE thread_pool::overrun_counter() { return q_.overrun_counter(); }
0070
0071 void SPDLOG_INLINE thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); }
0072
0073 size_t SPDLOG_INLINE thread_pool::discard_counter() { return q_.discard_counter(); }
0074
0075 void SPDLOG_INLINE thread_pool::reset_discard_counter() { q_.reset_discard_counter(); }
0076
0077 size_t SPDLOG_INLINE thread_pool::queue_size() { return q_.size(); }
0078
0079 void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg,
0080 async_overflow_policy overflow_policy) {
0081 if (overflow_policy == async_overflow_policy::block) {
0082 q_.enqueue(std::move(new_msg));
0083 } else if (overflow_policy == async_overflow_policy::overrun_oldest) {
0084 q_.enqueue_nowait(std::move(new_msg));
0085 } else {
0086 assert(overflow_policy == async_overflow_policy::discard_new);
0087 q_.enqueue_if_have_room(std::move(new_msg));
0088 }
0089 }
0090
0091 void SPDLOG_INLINE thread_pool::worker_loop_() {
0092 while (process_next_msg_()) {
0093 }
0094 }
0095
0096
0097
0098
0099 bool SPDLOG_INLINE thread_pool::process_next_msg_() {
0100 async_msg incoming_async_msg;
0101 q_.dequeue(incoming_async_msg);
0102
0103 switch (incoming_async_msg.msg_type) {
0104 case async_msg_type::log: {
0105 incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
0106 return true;
0107 }
0108 case async_msg_type::flush: {
0109 incoming_async_msg.worker_ptr->backend_flush_();
0110 return true;
0111 }
0112
0113 case async_msg_type::terminate: {
0114 return false;
0115 }
0116
0117 default: {
0118 assert(false);
0119 }
0120 }
0121
0122 return true;
0123 }
0124
0125 }
0126 }