thread_pool-inl.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. # include <spdlog/details/thread_pool.h>
  6. #endif
  7. #include <spdlog/common.h>
  8. #include <cassert>
  9. namespace spdlog {
  10. namespace details {
  11. SPDLOG_INLINE thread_pool::thread_pool(
  12. size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
  13. : q_(q_max_items)
  14. {
  15. if (threads_n == 0 || threads_n > 1000)
  16. {
  17. throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
  18. "range is 1-1000)");
  19. }
  20. for (size_t i = 0; i < threads_n; i++)
  21. {
  22. threads_.emplace_back([this, on_thread_start, on_thread_stop] {
  23. on_thread_start();
  24. this->thread_pool::worker_loop_();
  25. on_thread_stop();
  26. });
  27. }
  28. }
  29. SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start)
  30. : thread_pool(q_max_items, threads_n, on_thread_start, [] {})
  31. {}
  32. SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
  33. : thread_pool(
  34. q_max_items, threads_n, [] {}, [] {})
  35. {}
  36. // message all threads to terminate gracefully join them
  37. SPDLOG_INLINE thread_pool::~thread_pool()
  38. {
  39. SPDLOG_TRY
  40. {
  41. for (size_t i = 0; i < threads_.size(); i++)
  42. {
  43. post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
  44. }
  45. for (auto &t : threads_)
  46. {
  47. t.join();
  48. }
  49. }
  50. SPDLOG_CATCH_STD
  51. }
  52. void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
  53. {
  54. async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
  55. post_async_msg_(std::move(async_m), overflow_policy);
  56. }
  57. void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
  58. {
  59. post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
  60. }
  61. size_t SPDLOG_INLINE thread_pool::overrun_counter()
  62. {
  63. return q_.overrun_counter();
  64. }
  65. void SPDLOG_INLINE thread_pool::reset_overrun_counter()
  66. {
  67. q_.reset_overrun_counter();
  68. }
  69. size_t SPDLOG_INLINE thread_pool::queue_size()
  70. {
  71. return q_.size();
  72. }
  73. void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
  74. {
  75. if (overflow_policy == async_overflow_policy::block)
  76. {
  77. q_.enqueue(std::move(new_msg));
  78. }
  79. else
  80. {
  81. q_.enqueue_nowait(std::move(new_msg));
  82. }
  83. }
  84. void SPDLOG_INLINE thread_pool::worker_loop_()
  85. {
  86. while (process_next_msg_()) {}
  87. }
  88. // process next message in the queue
  89. // return true if this thread should still be active (while no terminate msg
  90. // was received)
  91. bool SPDLOG_INLINE thread_pool::process_next_msg_()
  92. {
  93. async_msg incoming_async_msg;
  94. q_.dequeue(incoming_async_msg);
  95. switch (incoming_async_msg.msg_type)
  96. {
  97. case async_msg_type::log: {
  98. incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
  99. return true;
  100. }
  101. case async_msg_type::flush: {
  102. incoming_async_msg.worker_ptr->backend_flush_();
  103. return true;
  104. }
  105. case async_msg_type::terminate: {
  106. return false;
  107. }
  108. default: {
  109. assert(false);
  110. }
  111. }
  112. return true;
  113. }
  114. } // namespace details
  115. } // namespace spdlog