File indexing completed on 2025-07-12 08:51:43
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(
0042 q_max_items, threads_n, [] {}, [] {}) {}
0043
0044
0045 SPDLOG_INLINE thread_pool::~thread_pool() {
0046 SPDLOG_TRY {
0047 for (size_t i = 0; i < threads_.size(); i++) {
0048 post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
0049 }
0050
0051 for (auto &t : threads_) {
0052 t.join();
0053 }
0054 }
0055 SPDLOG_CATCH_STD
0056 }
0057
0058 void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr,
0059 const details::log_msg &msg,
0060 async_overflow_policy overflow_policy) {
0061 async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
0062 post_async_msg_(std::move(async_m), overflow_policy);
0063 }
0064
0065 std::future<void> SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr,
0066 async_overflow_policy overflow_policy) {
0067 std::promise<void> promise;
0068 std::future<void> future = promise.get_future();
0069 post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush, std::move(promise)),
0070 overflow_policy);
0071 return future;
0072 }
0073
0074 size_t SPDLOG_INLINE thread_pool::overrun_counter() { return q_.overrun_counter(); }
0075
0076 void SPDLOG_INLINE thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); }
0077
0078 size_t SPDLOG_INLINE thread_pool::discard_counter() { return q_.discard_counter(); }
0079
0080 void SPDLOG_INLINE thread_pool::reset_discard_counter() { q_.reset_discard_counter(); }
0081
0082 size_t SPDLOG_INLINE thread_pool::queue_size() { return q_.size(); }
0083
0084 void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg,
0085 async_overflow_policy overflow_policy) {
0086 if (overflow_policy == async_overflow_policy::block) {
0087 q_.enqueue(std::move(new_msg));
0088 } else if (overflow_policy == async_overflow_policy::overrun_oldest) {
0089 q_.enqueue_nowait(std::move(new_msg));
0090 } else {
0091 assert(overflow_policy == async_overflow_policy::discard_new);
0092 q_.enqueue_if_have_room(std::move(new_msg));
0093 }
0094 }
0095
0096 void SPDLOG_INLINE thread_pool::worker_loop_() {
0097 while (process_next_msg_()) {
0098 }
0099 }
0100
0101
0102
0103
0104 bool SPDLOG_INLINE thread_pool::process_next_msg_() {
0105 async_msg incoming_async_msg;
0106 q_.dequeue(incoming_async_msg);
0107
0108 switch (incoming_async_msg.msg_type) {
0109 case async_msg_type::log: {
0110 incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
0111 return true;
0112 }
0113 case async_msg_type::flush: {
0114 incoming_async_msg.worker_ptr->backend_flush_();
0115 incoming_async_msg.flush_promise.set_value();
0116 return true;
0117 }
0118
0119 case async_msg_type::terminate: {
0120 return false;
0121 }
0122
0123 default: {
0124 assert(false);
0125 }
0126 }
0127
0128 return true;
0129 }
0130
0131 }
0132 }