thread_pool.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <spdlog/details/log_msg_buffer.h>
  5. #include <spdlog/details/mpmc_blocking_q.h>
  6. #include <spdlog/details/os.h>
  7. #include <chrono>
  8. #include <functional>
  9. #include <memory>
  10. #include <thread>
  11. #include <vector>
  12. namespace spdlog {
  13. class async_logger;
  14. namespace details {
  15. using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
  16. enum class async_msg_type { log, flush, terminate };
  17. // Async msg to move to/from the queue
  18. // Movable only. should never be copied
  19. struct async_msg : log_msg_buffer {
  20. async_msg_type msg_type{async_msg_type::log};
  21. async_logger_ptr worker_ptr;
  22. async_msg() = default;
  23. ~async_msg() = default;
  24. // should only be moved in or out of the queue..
  25. async_msg(const async_msg &) = delete;
  26. // support for vs2013 move
  27. #if defined(_MSC_VER) && _MSC_VER <= 1800
  28. async_msg(async_msg &&other)
  29. : log_msg_buffer(std::move(other)),
  30. msg_type(other.msg_type),
  31. worker_ptr(std::move(other.worker_ptr)) {}
  32. async_msg &operator=(async_msg &&other) {
  33. *static_cast<log_msg_buffer *>(this) = std::move(other);
  34. msg_type = other.msg_type;
  35. worker_ptr = std::move(other.worker_ptr);
  36. return *this;
  37. }
  38. #else // (_MSC_VER) && _MSC_VER <= 1800
  39. async_msg(async_msg &&) = default;
  40. async_msg &operator=(async_msg &&) = default;
  41. #endif
  42. // construct from log_msg with given type
  43. async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
  44. : log_msg_buffer{m},
  45. msg_type{the_type},
  46. worker_ptr{std::move(worker)} {}
  47. async_msg(async_logger_ptr &&worker, async_msg_type the_type)
  48. : log_msg_buffer{},
  49. msg_type{the_type},
  50. worker_ptr{std::move(worker)} {}
  51. explicit async_msg(async_msg_type the_type)
  52. : async_msg{nullptr, the_type} {}
  53. };
  54. class SPDLOG_API thread_pool {
  55. public:
  56. using item_type = async_msg;
  57. using q_type = details::mpmc_blocking_queue<item_type>;
  58. thread_pool(size_t q_max_items,
  59. size_t threads_n,
  60. std::function<void()> on_thread_start,
  61. std::function<void()> on_thread_stop);
  62. thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
  63. thread_pool(size_t q_max_items, size_t threads_n);
  64. // message all threads to terminate gracefully and join them
  65. ~thread_pool();
  66. thread_pool(const thread_pool &) = delete;
  67. thread_pool &operator=(thread_pool &&) = delete;
  68. void post_log(async_logger_ptr &&worker_ptr,
  69. const details::log_msg &msg,
  70. async_overflow_policy overflow_policy);
  71. void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
  72. size_t overrun_counter();
  73. void reset_overrun_counter();
  74. size_t discard_counter();
  75. void reset_discard_counter();
  76. size_t queue_size();
  77. private:
  78. q_type q_;
  79. std::vector<std::thread> threads_;
  80. void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
  81. void worker_loop_();
  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 process_next_msg_();
  86. };
  87. } // namespace details
  88. } // namespace spdlog
  89. #ifdef SPDLOG_HEADER_ONLY
  90. #include "thread_pool-inl.h"
  91. #endif