诸暨麻将添加redis
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

124 行
3.1 KiB

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