resolver_service_base.ipp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // detail/impl/resolver_service_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_SERVICE_BASE_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_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/detail/resolver_service_base.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. class resolver_service_base::work_scheduler_runner
  22. {
  23. public:
  24. work_scheduler_runner(scheduler_impl& work_scheduler)
  25. : work_scheduler_(work_scheduler)
  26. {
  27. }
  28. void operator()()
  29. {
  30. boost::system::error_code ec;
  31. work_scheduler_.run(ec);
  32. }
  33. private:
  34. scheduler_impl& work_scheduler_;
  35. };
  36. resolver_service_base::resolver_service_base(execution_context& context)
  37. : scheduler_(boost::asio::use_service<scheduler_impl>(context)),
  38. work_scheduler_(new scheduler_impl(context, -1, false)),
  39. work_thread_(0)
  40. {
  41. work_scheduler_->work_started();
  42. }
  43. resolver_service_base::~resolver_service_base()
  44. {
  45. base_shutdown();
  46. }
  47. void resolver_service_base::base_shutdown()
  48. {
  49. if (work_scheduler_.get())
  50. {
  51. work_scheduler_->work_finished();
  52. work_scheduler_->stop();
  53. if (work_thread_.get())
  54. {
  55. work_thread_->join();
  56. work_thread_.reset();
  57. }
  58. work_scheduler_.reset();
  59. }
  60. }
  61. void resolver_service_base::base_notify_fork(
  62. execution_context::fork_event fork_ev)
  63. {
  64. if (work_thread_.get())
  65. {
  66. if (fork_ev == execution_context::fork_prepare)
  67. {
  68. work_scheduler_->stop();
  69. work_thread_->join();
  70. work_thread_.reset();
  71. }
  72. else
  73. {
  74. work_scheduler_->restart();
  75. work_thread_.reset(new boost::asio::detail::thread(
  76. work_scheduler_runner(*work_scheduler_)));
  77. }
  78. }
  79. }
  80. void resolver_service_base::construct(
  81. resolver_service_base::implementation_type& impl)
  82. {
  83. impl.reset(static_cast<void*>(0), socket_ops::noop_deleter());
  84. }
  85. void resolver_service_base::destroy(
  86. resolver_service_base::implementation_type& impl)
  87. {
  88. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  89. "resolver", &impl, 0, "cancel"));
  90. impl.reset();
  91. }
  92. void resolver_service_base::move_construct(implementation_type& impl,
  93. implementation_type& other_impl)
  94. {
  95. impl = BOOST_ASIO_MOVE_CAST(implementation_type)(other_impl);
  96. }
  97. void resolver_service_base::move_assign(implementation_type& impl,
  98. resolver_service_base&, implementation_type& other_impl)
  99. {
  100. destroy(impl);
  101. impl = BOOST_ASIO_MOVE_CAST(implementation_type)(other_impl);
  102. }
  103. void resolver_service_base::cancel(
  104. resolver_service_base::implementation_type& impl)
  105. {
  106. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  107. "resolver", &impl, 0, "cancel"));
  108. impl.reset(static_cast<void*>(0), socket_ops::noop_deleter());
  109. }
  110. void resolver_service_base::start_resolve_op(resolve_op* op)
  111. {
  112. if (BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(SCHEDULER,
  113. scheduler_.concurrency_hint()))
  114. {
  115. start_work_thread();
  116. scheduler_.work_started();
  117. work_scheduler_->post_immediate_completion(op, false);
  118. }
  119. else
  120. {
  121. op->ec_ = boost::asio::error::operation_not_supported;
  122. scheduler_.post_immediate_completion(op, false);
  123. }
  124. }
  125. void resolver_service_base::start_work_thread()
  126. {
  127. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  128. if (!work_thread_.get())
  129. {
  130. work_thread_.reset(new boost::asio::detail::thread(
  131. work_scheduler_runner(*work_scheduler_)));
  132. }
  133. }
  134. } // namespace detail
  135. } // namespace asio
  136. } // namespace boost
  137. #include <boost/asio/detail/pop_options.hpp>
  138. #endif // BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP