Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 09:27:48

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #ifndef SPDLOG_HEADER_ONLY
0007 #    include <spdlog/details/thread_pool.h>
0008 #endif
0009 
0010 #include <spdlog/common.h>
0011 #include <cassert>
0012 
0013 namespace spdlog {
0014 namespace details {
0015 
0016 SPDLOG_INLINE thread_pool::thread_pool(
0017     size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
0018     : q_(q_max_items)
0019 {
0020     if (threads_n == 0 || threads_n > 1000)
0021     {
0022         throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
0023                         "range is 1-1000)");
0024     }
0025     for (size_t i = 0; i < threads_n; i++)
0026     {
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, size_t threads_n, std::function<void()> on_thread_start)
0036     : thread_pool(q_max_items, threads_n, on_thread_start, [] {})
0037 {}
0038 
0039 SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
0040     : thread_pool(
0041           q_max_items, threads_n, [] {}, [] {})
0042 {}
0043 
0044 // message all threads to terminate gracefully join them
0045 SPDLOG_INLINE thread_pool::~thread_pool()
0046 {
0047     SPDLOG_TRY
0048     {
0049         for (size_t i = 0; i < threads_.size(); i++)
0050         {
0051             post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
0052         }
0053 
0054         for (auto &t : threads_)
0055         {
0056             t.join();
0057         }
0058     }
0059     SPDLOG_CATCH_STD
0060 }
0061 
0062 void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
0063 {
0064     async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
0065     post_async_msg_(std::move(async_m), overflow_policy);
0066 }
0067 
0068 void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
0069 {
0070     post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
0071 }
0072 
0073 size_t SPDLOG_INLINE thread_pool::overrun_counter()
0074 {
0075     return q_.overrun_counter();
0076 }
0077 
0078 void SPDLOG_INLINE thread_pool::reset_overrun_counter()
0079 {
0080     q_.reset_overrun_counter();
0081 }
0082 
0083 size_t SPDLOG_INLINE thread_pool::queue_size()
0084 {
0085     return q_.size();
0086 }
0087 
0088 void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
0089 {
0090     if (overflow_policy == async_overflow_policy::block)
0091     {
0092         q_.enqueue(std::move(new_msg));
0093     }
0094     else
0095     {
0096         q_.enqueue_nowait(std::move(new_msg));
0097     }
0098 }
0099 
0100 void SPDLOG_INLINE thread_pool::worker_loop_()
0101 {
0102     while (process_next_msg_()) {}
0103 }
0104 
0105 // process next message in the queue
0106 // return true if this thread should still be active (while no terminate msg
0107 // was received)
0108 bool SPDLOG_INLINE thread_pool::process_next_msg_()
0109 {
0110     async_msg incoming_async_msg;
0111     bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
0112     if (!dequeued)
0113     {
0114         return true;
0115     }
0116 
0117     switch (incoming_async_msg.msg_type)
0118     {
0119     case async_msg_type::log: {
0120         incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
0121         return true;
0122     }
0123     case async_msg_type::flush: {
0124         incoming_async_msg.worker_ptr->backend_flush_();
0125         return true;
0126     }
0127 
0128     case async_msg_type::terminate: {
0129         return false;
0130     }
0131 
0132     default: {
0133         assert(false);
0134     }
0135     }
0136 
0137     return true;
0138 }
0139 
0140 } // namespace details
0141 } // namespace spdlog