thread_pool-inl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <cassert>
  8. #include <spdlog/common.h>
  9. namespace spdlog {
  10. namespace details {
  11. SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items,
  12. size_t threads_n,
  13. std::function<void()> on_thread_start,
  14. std::function<void()> on_thread_stop)
  15. : q_(q_max_items) {
  16. if (threads_n == 0 || threads_n > 1000) {
  17. throw_spdlog_ex(
  18. "spdlog::thread_pool(): invalid threads_n param (valid "
  19. "range is 1-1000)");
  20. }
  21. for (size_t i = 0; i < threads_n; i++) {
  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,
  30. size_t threads_n,
  31. std::function<void()> on_thread_start)
  32. : thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {}
  33. SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
  34. : thread_pool(
  35. q_max_items, threads_n, [] {}, [] {}) {}
  36. // message all threads to terminate gracefully join them
  37. SPDLOG_INLINE thread_pool::~thread_pool() {
  38. SPDLOG_TRY {
  39. for (size_t i = 0; i < threads_.size(); i++) {
  40. post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
  41. }
  42. for (auto &t : threads_) {
  43. t.join();
  44. }
  45. }
  46. SPDLOG_CATCH_STD
  47. }
  48. void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr,
  49. const details::log_msg &msg,
  50. async_overflow_policy overflow_policy) {
  51. async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
  52. post_async_msg_(std::move(async_m), overflow_policy);
  53. }
  54. std::future<void> SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr,
  55. async_overflow_policy overflow_policy) {
  56. std::promise<void> promise;
  57. std::future<void> future = promise.get_future();
  58. post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush, std::move(promise)),
  59. overflow_policy);
  60. return future;
  61. }
  62. size_t SPDLOG_INLINE thread_pool::overrun_counter() { return q_.overrun_counter(); }
  63. void SPDLOG_INLINE thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); }
  64. size_t SPDLOG_INLINE thread_pool::discard_counter() { return q_.discard_counter(); }
  65. void SPDLOG_INLINE thread_pool::reset_discard_counter() { q_.reset_discard_counter(); }
  66. size_t SPDLOG_INLINE thread_pool::queue_size() { return q_.size(); }
  67. void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg,
  68. async_overflow_policy overflow_policy) {
  69. if (overflow_policy == async_overflow_policy::block) {
  70. q_.enqueue(std::move(new_msg));
  71. } else if (overflow_policy == async_overflow_policy::overrun_oldest) {
  72. q_.enqueue_nowait(std::move(new_msg));
  73. } else {
  74. assert(overflow_policy == async_overflow_policy::discard_new);
  75. q_.enqueue_if_have_room(std::move(new_msg));
  76. }
  77. }
  78. void SPDLOG_INLINE thread_pool::worker_loop_() {
  79. while (process_next_msg_()) {
  80. }
  81. }
  82. // process next message in the queue
  83. // return true if this thread should still be active (while no terminate msg
  84. // was received)
  85. bool SPDLOG_INLINE thread_pool::process_next_msg_() {
  86. async_msg incoming_async_msg;
  87. q_.dequeue(incoming_async_msg);
  88. switch (incoming_async_msg.msg_type) {
  89. case async_msg_type::log: {
  90. incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
  91. return true;
  92. }
  93. case async_msg_type::flush: {
  94. incoming_async_msg.worker_ptr->backend_flush_();
  95. incoming_async_msg.flush_promise.set_value();
  96. return true;
  97. }
  98. case async_msg_type::terminate: {
  99. return false;
  100. }
  101. default: {
  102. assert(false);
  103. }
  104. }
  105. return true;
  106. }
  107. } // namespace details
  108. } // namespace spdlog