thread_pool.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <memory>
  9. #include <thread>
  10. #include <vector>
  11. #include <functional>
  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
  17. {
  18. log,
  19. flush,
  20. terminate
  21. };
  22. // Async msg to move to/from the queue
  23. // Movable only. should never be copied
  24. struct async_msg : log_msg_buffer
  25. {
  26. async_msg_type msg_type{async_msg_type::log};
  27. async_logger_ptr worker_ptr;
  28. async_msg() = default;
  29. ~async_msg() = default;
  30. // should only be moved in or out of the queue..
  31. async_msg(const async_msg &) = delete;
  32. // support for vs2013 move
  33. #if defined(_MSC_VER) && _MSC_VER <= 1800
  34. async_msg(async_msg &&other)
  35. : log_msg_buffer(std::move(other))
  36. , msg_type(other.msg_type)
  37. , worker_ptr(std::move(other.worker_ptr))
  38. {}
  39. async_msg &operator=(async_msg &&other)
  40. {
  41. *static_cast<log_msg_buffer *>(this) = std::move(other);
  42. msg_type = other.msg_type;
  43. worker_ptr = std::move(other.worker_ptr);
  44. return *this;
  45. }
  46. #else // (_MSC_VER) && _MSC_VER <= 1800
  47. async_msg(async_msg &&) = default;
  48. async_msg &operator=(async_msg &&) = default;
  49. #endif
  50. // construct from log_msg with given type
  51. async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
  52. : log_msg_buffer{m}
  53. , msg_type{the_type}
  54. , worker_ptr{std::move(worker)}
  55. {}
  56. async_msg(async_logger_ptr &&worker, async_msg_type the_type)
  57. : log_msg_buffer{}
  58. , msg_type{the_type}
  59. , worker_ptr{std::move(worker)}
  60. {}
  61. explicit async_msg(async_msg_type the_type)
  62. : async_msg{nullptr, the_type}
  63. {}
  64. };
  65. class SPDLOG_API thread_pool
  66. {
  67. public:
  68. using item_type = async_msg;
  69. using q_type = details::mpmc_blocking_queue<item_type>;
  70. thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop);
  71. thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
  72. thread_pool(size_t q_max_items, size_t threads_n);
  73. // message all threads to terminate gracefully and join them
  74. ~thread_pool();
  75. thread_pool(const thread_pool &) = delete;
  76. thread_pool &operator=(thread_pool &&) = delete;
  77. void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
  78. void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
  79. size_t overrun_counter();
  80. void reset_overrun_counter();
  81. size_t queue_size();
  82. private:
  83. q_type q_;
  84. std::vector<std::thread> threads_;
  85. void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
  86. void worker_loop_();
  87. // process next message in the queue
  88. // return true if this thread should still be active (while no terminate msg
  89. // was received)
  90. bool process_next_msg_();
  91. };
  92. } // namespace details
  93. } // namespace spdlog
  94. #ifdef SPDLOG_HEADER_ONLY
  95. # include "thread_pool-inl.h"
  96. #endif