resolver_thread_pool.ipp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // detail/impl/resolver_thread_pool.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IMPL_RESOLVER_THREAD_POOL_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_RESOLVER_THREAD_POOL_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/config.hpp>
  17. #include <boost/asio/detail/memory.hpp>
  18. #include <boost/asio/detail/resolver_thread_pool.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. class resolver_thread_pool::work_scheduler_runner
  25. {
  26. public:
  27. work_scheduler_runner(scheduler_impl& work_scheduler)
  28. : work_scheduler_(work_scheduler)
  29. {
  30. }
  31. void operator()()
  32. {
  33. boost::system::error_code ec;
  34. work_scheduler_.run(ec);
  35. }
  36. private:
  37. scheduler_impl& work_scheduler_;
  38. };
  39. resolver_thread_pool::resolver_thread_pool(execution_context& context)
  40. : execution_context_service_base<resolver_thread_pool>(context),
  41. scheduler_(boost::asio::use_service<scheduler_impl>(context)),
  42. work_scheduler_(scheduler_impl::internal(), context),
  43. work_threads_(execution_context::allocator<void>(context)),
  44. num_work_threads_(config(context).get("resolver", "threads", 0U)),
  45. scheduler_locking_(config(context).get("scheduler", "locking", true)),
  46. shutdown_(false)
  47. {
  48. work_scheduler_.work_started();
  49. if (num_work_threads_ > 0)
  50. start_work_threads();
  51. else
  52. num_work_threads_ = 1;
  53. }
  54. resolver_thread_pool::~resolver_thread_pool()
  55. {
  56. shutdown();
  57. }
  58. void resolver_thread_pool::shutdown()
  59. {
  60. if (!shutdown_)
  61. {
  62. work_scheduler_.work_finished();
  63. work_scheduler_.stop();
  64. work_threads_.join();
  65. work_scheduler_.shutdown();
  66. shutdown_ = true;
  67. }
  68. }
  69. void resolver_thread_pool::notify_fork(execution_context::fork_event fork_ev)
  70. {
  71. if (!work_threads_.empty())
  72. {
  73. if (fork_ev == execution_context::fork_prepare)
  74. {
  75. work_scheduler_.stop();
  76. work_threads_.join();
  77. }
  78. }
  79. else if (fork_ev != execution_context::fork_prepare)
  80. {
  81. work_scheduler_.restart();
  82. }
  83. }
  84. void resolver_thread_pool::start_resolve_op(resolve_op* op)
  85. {
  86. if (scheduler_locking_)
  87. {
  88. start_work_threads();
  89. scheduler_.work_started();
  90. work_scheduler_.post_immediate_completion(op, false);
  91. }
  92. else
  93. {
  94. op->ec_ = boost::asio::error::operation_not_supported;
  95. scheduler_.post_immediate_completion(op, false);
  96. }
  97. }
  98. void resolver_thread_pool::start_work_threads()
  99. {
  100. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  101. if (work_threads_.empty())
  102. for (unsigned int i = 0; i < num_work_threads_; ++i)
  103. work_threads_.create_thread(work_scheduler_runner(work_scheduler_));
  104. }
  105. } // namespace detail
  106. } // namespace asio
  107. } // namespace boost
  108. #include <boost/asio/detail/pop_options.hpp>
  109. #endif // BOOST_ASIO_DETAIL_IMPL_RESOLVER_THREAD_POOL_IPP