2 // detail/impl/resolver_service_base.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/resolver_service_base.hpp>
21 #include <boost/asio/detail/push_options.hpp>
27 class resolver_service_base::work_io_context_runner
30 work_io_context_runner(boost::asio::io_context& io_context)
31 : io_context_(io_context) {}
32 void operator()() { io_context_.run(); }
34 boost::asio::io_context& io_context_;
37 resolver_service_base::resolver_service_base(
38 boost::asio::io_context& io_context)
39 : io_context_impl_(boost::asio::use_service<io_context_impl>(io_context)),
40 work_io_context_(new boost::asio::io_context(-1)),
41 work_io_context_impl_(boost::asio::use_service<
42 io_context_impl>(*work_io_context_)),
43 work_(boost::asio::make_work_guard(*work_io_context_)),
48 resolver_service_base::~resolver_service_base()
53 void resolver_service_base::base_shutdown()
56 if (work_io_context_.get())
58 work_io_context_->stop();
59 if (work_thread_.get())
64 work_io_context_.reset();
68 void resolver_service_base::base_notify_fork(
69 boost::asio::io_context::fork_event fork_ev)
71 if (work_thread_.get())
73 if (fork_ev == boost::asio::io_context::fork_prepare)
75 work_io_context_->stop();
80 work_io_context_->restart();
81 work_thread_.reset(new boost::asio::detail::thread(
82 work_io_context_runner(*work_io_context_)));
87 void resolver_service_base::construct(
88 resolver_service_base::implementation_type& impl)
90 impl.reset(static_cast<void*>(0), socket_ops::noop_deleter());
93 void resolver_service_base::destroy(
94 resolver_service_base::implementation_type& impl)
96 BOOST_ASIO_HANDLER_OPERATION((io_context_impl_.context(),
97 "resolver", &impl, 0, "cancel"));
102 void resolver_service_base::move_construct(implementation_type& impl,
103 implementation_type& other_impl)
105 impl = BOOST_ASIO_MOVE_CAST(implementation_type)(other_impl);
108 void resolver_service_base::move_assign(implementation_type& impl,
109 resolver_service_base&, implementation_type& other_impl)
112 impl = BOOST_ASIO_MOVE_CAST(implementation_type)(other_impl);
115 void resolver_service_base::cancel(
116 resolver_service_base::implementation_type& impl)
118 BOOST_ASIO_HANDLER_OPERATION((io_context_impl_.context(),
119 "resolver", &impl, 0, "cancel"));
121 impl.reset(static_cast<void*>(0), socket_ops::noop_deleter());
124 void resolver_service_base::start_resolve_op(resolve_op* op)
126 if (BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(SCHEDULER,
127 io_context_impl_.concurrency_hint()))
130 io_context_impl_.work_started();
131 work_io_context_impl_.post_immediate_completion(op, false);
135 op->ec_ = boost::asio::error::operation_not_supported;
136 io_context_impl_.post_immediate_completion(op, false);
140 void resolver_service_base::start_work_thread()
142 boost::asio::detail::mutex::scoped_lock lock(mutex_);
143 if (!work_thread_.get())
145 work_thread_.reset(new boost::asio::detail::thread(
146 work_io_context_runner(*work_io_context_)));
150 } // namespace detail
154 #include <boost/asio/detail/pop_options.hpp>
156 #endif // BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP