]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // detail/impl/resolver_service_base.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2017 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 | ||
11 | #ifndef BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP | |
12 | #define BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | #include <boost/asio/detail/resolver_service_base.hpp> | |
20 | ||
21 | #include <boost/asio/detail/push_options.hpp> | |
22 | ||
23 | namespace boost { | |
24 | namespace asio { | |
25 | namespace detail { | |
26 | ||
27 | class resolver_service_base::work_io_context_runner | |
28 | { | |
29 | public: | |
30 | work_io_context_runner(boost::asio::io_context& io_context) | |
31 | : io_context_(io_context) {} | |
32 | void operator()() { io_context_.run(); } | |
33 | private: | |
34 | boost::asio::io_context& io_context_; | |
35 | }; | |
36 | ||
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_)), | |
44 | work_thread_(0) | |
45 | { | |
46 | } | |
47 | ||
48 | resolver_service_base::~resolver_service_base() | |
49 | { | |
50 | base_shutdown(); | |
51 | } | |
52 | ||
53 | void resolver_service_base::base_shutdown() | |
54 | { | |
55 | work_.reset(); | |
56 | if (work_io_context_.get()) | |
57 | { | |
58 | work_io_context_->stop(); | |
59 | if (work_thread_.get()) | |
60 | { | |
61 | work_thread_->join(); | |
62 | work_thread_.reset(); | |
63 | } | |
64 | work_io_context_.reset(); | |
65 | } | |
66 | } | |
67 | ||
68 | void resolver_service_base::base_notify_fork( | |
69 | boost::asio::io_context::fork_event fork_ev) | |
70 | { | |
71 | if (work_thread_.get()) | |
72 | { | |
73 | if (fork_ev == boost::asio::io_context::fork_prepare) | |
74 | { | |
75 | work_io_context_->stop(); | |
76 | work_thread_->join(); | |
77 | } | |
78 | else | |
79 | { | |
80 | work_io_context_->restart(); | |
81 | work_thread_.reset(new boost::asio::detail::thread( | |
82 | work_io_context_runner(*work_io_context_))); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | void resolver_service_base::construct( | |
88 | resolver_service_base::implementation_type& impl) | |
89 | { | |
90 | impl.reset(static_cast<void*>(0), socket_ops::noop_deleter()); | |
91 | } | |
92 | ||
93 | void resolver_service_base::destroy( | |
94 | resolver_service_base::implementation_type& impl) | |
95 | { | |
96 | BOOST_ASIO_HANDLER_OPERATION((io_context_impl_.context(), | |
97 | "resolver", &impl, 0, "cancel")); | |
98 | ||
99 | impl.reset(); | |
100 | } | |
101 | ||
102 | void resolver_service_base::move_construct(implementation_type& impl, | |
103 | implementation_type& other_impl) | |
104 | { | |
105 | impl = BOOST_ASIO_MOVE_CAST(implementation_type)(other_impl); | |
106 | } | |
107 | ||
108 | void resolver_service_base::move_assign(implementation_type& impl, | |
109 | resolver_service_base&, implementation_type& other_impl) | |
110 | { | |
111 | destroy(impl); | |
112 | impl = BOOST_ASIO_MOVE_CAST(implementation_type)(other_impl); | |
113 | } | |
114 | ||
115 | void resolver_service_base::cancel( | |
116 | resolver_service_base::implementation_type& impl) | |
117 | { | |
118 | BOOST_ASIO_HANDLER_OPERATION((io_context_impl_.context(), | |
119 | "resolver", &impl, 0, "cancel")); | |
120 | ||
121 | impl.reset(static_cast<void*>(0), socket_ops::noop_deleter()); | |
122 | } | |
123 | ||
124 | void resolver_service_base::start_resolve_op(resolve_op* op) | |
125 | { | |
126 | if (BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(SCHEDULER, | |
127 | io_context_impl_.concurrency_hint())) | |
128 | { | |
129 | start_work_thread(); | |
130 | io_context_impl_.work_started(); | |
131 | work_io_context_impl_.post_immediate_completion(op, false); | |
132 | } | |
133 | else | |
134 | { | |
135 | op->ec_ = boost::asio::error::operation_not_supported; | |
136 | io_context_impl_.post_immediate_completion(op, false); | |
137 | } | |
138 | } | |
139 | ||
140 | void resolver_service_base::start_work_thread() | |
141 | { | |
142 | boost::asio::detail::mutex::scoped_lock lock(mutex_); | |
143 | if (!work_thread_.get()) | |
144 | { | |
145 | work_thread_.reset(new boost::asio::detail::thread( | |
146 | work_io_context_runner(*work_io_context_))); | |
147 | } | |
148 | } | |
149 | ||
150 | } // namespace detail | |
151 | } // namespace asio | |
152 | } // namespace boost | |
153 | ||
154 | #include <boost/asio/detail/pop_options.hpp> | |
155 | ||
156 | #endif // BOOST_ASIO_DETAIL_IMPL_RESOLVER_SERVICE_BASE_IPP |