You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 line
3.2KB

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