]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/impl/io_context.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / impl / io_context.hpp
1 //
2 // impl/io_context.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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_IMPL_IO_CONTEXT_HPP
12 #define BOOST_ASIO_IMPL_IO_CONTEXT_HPP
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/completion_handler.hpp>
19 #include <boost/asio/detail/executor_op.hpp>
20 #include <boost/asio/detail/fenced_block.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/non_const_lvalue.hpp>
23 #include <boost/asio/detail/recycling_allocator.hpp>
24 #include <boost/asio/detail/service_registry.hpp>
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/detail/type_traits.hpp>
27
28 #include <boost/asio/detail/push_options.hpp>
29
30 namespace boost {
31 namespace asio {
32
33 #if !defined(GENERATING_DOCUMENTATION)
34
35 template <typename Service>
36 inline Service& use_service(io_context& ioc)
37 {
38 // Check that Service meets the necessary type requirements.
39 (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
40 (void)static_cast<const execution_context::id*>(&Service::id);
41
42 return ioc.service_registry_->template use_service<Service>(ioc);
43 }
44
45 template <>
46 inline detail::io_context_impl& use_service<detail::io_context_impl>(
47 io_context& ioc)
48 {
49 return ioc.impl_;
50 }
51
52 #endif // !defined(GENERATING_DOCUMENTATION)
53
54 inline io_context::executor_type
55 io_context::get_executor() BOOST_ASIO_NOEXCEPT
56 {
57 return executor_type(*this);
58 }
59
60 #if defined(BOOST_ASIO_HAS_CHRONO)
61
62 template <typename Rep, typename Period>
63 std::size_t io_context::run_for(
64 const chrono::duration<Rep, Period>& rel_time)
65 {
66 return this->run_until(chrono::steady_clock::now() + rel_time);
67 }
68
69 template <typename Clock, typename Duration>
70 std::size_t io_context::run_until(
71 const chrono::time_point<Clock, Duration>& abs_time)
72 {
73 std::size_t n = 0;
74 while (this->run_one_until(abs_time))
75 if (n != (std::numeric_limits<std::size_t>::max)())
76 ++n;
77 return n;
78 }
79
80 template <typename Rep, typename Period>
81 std::size_t io_context::run_one_for(
82 const chrono::duration<Rep, Period>& rel_time)
83 {
84 return this->run_one_until(chrono::steady_clock::now() + rel_time);
85 }
86
87 template <typename Clock, typename Duration>
88 std::size_t io_context::run_one_until(
89 const chrono::time_point<Clock, Duration>& abs_time)
90 {
91 typename Clock::time_point now = Clock::now();
92 while (now < abs_time)
93 {
94 typename Clock::duration rel_time = abs_time - now;
95 if (rel_time > chrono::seconds(1))
96 rel_time = chrono::seconds(1);
97
98 boost::system::error_code ec;
99 std::size_t s = impl_.wait_one(
100 static_cast<long>(chrono::duration_cast<
101 chrono::microseconds>(rel_time).count()), ec);
102 boost::asio::detail::throw_error(ec);
103
104 if (s || impl_.stopped())
105 return s;
106
107 now = Clock::now();
108 }
109
110 return 0;
111 }
112
113 #endif // defined(BOOST_ASIO_HAS_CHRONO)
114
115 #if !defined(BOOST_ASIO_NO_DEPRECATED)
116
117 inline void io_context::reset()
118 {
119 restart();
120 }
121
122 struct io_context::initiate_dispatch
123 {
124 template <typename LegacyCompletionHandler>
125 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
126 io_context* self) const
127 {
128 // If you get an error on the following line it means that your handler does
129 // not meet the documented type requirements for a LegacyCompletionHandler.
130 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
131 LegacyCompletionHandler, handler) type_check;
132
133 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
134 if (self->impl_.can_dispatch())
135 {
136 detail::fenced_block b(detail::fenced_block::full);
137 boost_asio_handler_invoke_helpers::invoke(
138 handler2.value, handler2.value);
139 }
140 else
141 {
142 // Allocate and construct an operation to wrap the handler.
143 typedef detail::completion_handler<
144 typename decay<LegacyCompletionHandler>::type, executor_type> op;
145 typename op::ptr p = { detail::addressof(handler2.value),
146 op::ptr::allocate(handler2.value), 0 };
147 p.p = new (p.v) op(handler2.value, self->get_executor());
148
149 BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
150 "io_context", self, 0, "dispatch"));
151
152 self->impl_.do_dispatch(p.p);
153 p.v = p.p = 0;
154 }
155 }
156 };
157
158 template <typename LegacyCompletionHandler>
159 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
160 io_context::dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
161 {
162 return async_initiate<LegacyCompletionHandler, void ()>(
163 initiate_dispatch(), handler, this);
164 }
165
166 struct io_context::initiate_post
167 {
168 template <typename LegacyCompletionHandler>
169 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
170 io_context* self) const
171 {
172 // If you get an error on the following line it means that your handler does
173 // not meet the documented type requirements for a LegacyCompletionHandler.
174 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
175 LegacyCompletionHandler, handler) type_check;
176
177 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
178
179 bool is_continuation =
180 boost_asio_handler_cont_helpers::is_continuation(handler2.value);
181
182 // Allocate and construct an operation to wrap the handler.
183 typedef detail::completion_handler<
184 typename decay<LegacyCompletionHandler>::type, executor_type> op;
185 typename op::ptr p = { detail::addressof(handler2.value),
186 op::ptr::allocate(handler2.value), 0 };
187 p.p = new (p.v) op(handler2.value, self->get_executor());
188
189 BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
190 "io_context", self, 0, "post"));
191
192 self->impl_.post_immediate_completion(p.p, is_continuation);
193 p.v = p.p = 0;
194 }
195 };
196
197 template <typename LegacyCompletionHandler>
198 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
199 io_context::post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
200 {
201 return async_initiate<LegacyCompletionHandler, void ()>(
202 initiate_post(), handler, this);
203 }
204
205 template <typename Handler>
206 #if defined(GENERATING_DOCUMENTATION)
207 unspecified
208 #else
209 inline detail::wrapped_handler<io_context&, Handler>
210 #endif
211 io_context::wrap(Handler handler)
212 {
213 return detail::wrapped_handler<io_context&, Handler>(*this, handler);
214 }
215
216 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
217
218 template <typename Allocator, unsigned int Bits>
219 io_context::basic_executor_type<Allocator, Bits>&
220 io_context::basic_executor_type<Allocator, Bits>::operator=(
221 const basic_executor_type& other) BOOST_ASIO_NOEXCEPT
222 {
223 if (this != &other)
224 {
225 io_context* old_io_context = io_context_;
226 io_context_ = other.io_context_;
227 allocator_ = other.allocator_;
228 bits_ = other.bits_;
229 if (Bits & outstanding_work_tracked)
230 {
231 if (io_context_)
232 io_context_->impl_.work_started();
233 if (old_io_context)
234 old_io_context->impl_.work_finished();
235 }
236 }
237 return *this;
238 }
239
240 #if defined(BOOST_ASIO_HAS_MOVE)
241 template <typename Allocator, unsigned int Bits>
242 io_context::basic_executor_type<Allocator, Bits>&
243 io_context::basic_executor_type<Allocator, Bits>::operator=(
244 basic_executor_type&& other) BOOST_ASIO_NOEXCEPT
245 {
246 if (this != &other)
247 {
248 io_context_ = other.io_context_;
249 allocator_ = std::move(other.allocator_);
250 bits_ = other.bits_;
251 if (Bits & outstanding_work_tracked)
252 other.io_context_ = 0;
253 }
254 return *this;
255 }
256 #endif // defined(BOOST_ASIO_HAS_MOVE)
257
258 template <typename Allocator, unsigned int Bits>
259 inline bool io_context::basic_executor_type<Allocator,
260 Bits>::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
261 {
262 return io_context_->impl_.can_dispatch();
263 }
264
265 template <typename Allocator, unsigned int Bits>
266 template <typename Function>
267 void io_context::basic_executor_type<Allocator, Bits>::execute(
268 BOOST_ASIO_MOVE_ARG(Function) f) const
269 {
270 typedef typename decay<Function>::type function_type;
271
272 // Invoke immediately if the blocking.possibly property is enabled and we are
273 // already inside the thread pool.
274 if ((bits_ & blocking_never) == 0 && io_context_->impl_.can_dispatch())
275 {
276 // Make a local, non-const copy of the function.
277 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
278
279 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
280 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
281 try
282 {
283 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
284 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
285 detail::fenced_block b(detail::fenced_block::full);
286 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
287 return;
288 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
289 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
290 }
291 catch (...)
292 {
293 io_context_->impl_.capture_current_exception();
294 return;
295 }
296 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
297 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
298 }
299
300 // Allocate and construct an operation to wrap the function.
301 typedef detail::executor_op<function_type, Allocator, detail::operation> op;
302 typename op::ptr p = { detail::addressof(allocator_),
303 op::ptr::allocate(allocator_), 0 };
304 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), allocator_);
305
306 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
307 "io_context", io_context_, 0, "execute"));
308
309 io_context_->impl_.post_immediate_completion(p.p,
310 (bits_ & relationship_continuation) != 0);
311 p.v = p.p = 0;
312 }
313
314 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
315 template <typename Allocator, unsigned int Bits>
316 inline io_context& io_context::basic_executor_type<
317 Allocator, Bits>::context() const BOOST_ASIO_NOEXCEPT
318 {
319 return *io_context_;
320 }
321
322 template <typename Allocator, unsigned int Bits>
323 inline void io_context::basic_executor_type<Allocator,
324 Bits>::on_work_started() const BOOST_ASIO_NOEXCEPT
325 {
326 io_context_->impl_.work_started();
327 }
328
329 template <typename Allocator, unsigned int Bits>
330 inline void io_context::basic_executor_type<Allocator,
331 Bits>::on_work_finished() const BOOST_ASIO_NOEXCEPT
332 {
333 io_context_->impl_.work_finished();
334 }
335
336 template <typename Allocator, unsigned int Bits>
337 template <typename Function, typename OtherAllocator>
338 void io_context::basic_executor_type<Allocator, Bits>::dispatch(
339 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
340 {
341 typedef typename decay<Function>::type function_type;
342
343 // Invoke immediately if we are already inside the thread pool.
344 if (io_context_->impl_.can_dispatch())
345 {
346 // Make a local, non-const copy of the function.
347 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
348
349 detail::fenced_block b(detail::fenced_block::full);
350 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
351 return;
352 }
353
354 // Allocate and construct an operation to wrap the function.
355 typedef detail::executor_op<function_type,
356 OtherAllocator, detail::operation> op;
357 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
358 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
359
360 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
361 "io_context", io_context_, 0, "dispatch"));
362
363 io_context_->impl_.post_immediate_completion(p.p, false);
364 p.v = p.p = 0;
365 }
366
367 template <typename Allocator, unsigned int Bits>
368 template <typename Function, typename OtherAllocator>
369 void io_context::basic_executor_type<Allocator, Bits>::post(
370 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
371 {
372 typedef typename decay<Function>::type function_type;
373
374 // Allocate and construct an operation to wrap the function.
375 typedef detail::executor_op<function_type,
376 OtherAllocator, detail::operation> op;
377 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
378 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
379
380 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
381 "io_context", io_context_, 0, "post"));
382
383 io_context_->impl_.post_immediate_completion(p.p, false);
384 p.v = p.p = 0;
385 }
386
387 template <typename Allocator, unsigned int Bits>
388 template <typename Function, typename OtherAllocator>
389 void io_context::basic_executor_type<Allocator, Bits>::defer(
390 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
391 {
392 typedef typename decay<Function>::type function_type;
393
394 // Allocate and construct an operation to wrap the function.
395 typedef detail::executor_op<function_type,
396 OtherAllocator, detail::operation> op;
397 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
398 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
399
400 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
401 "io_context", io_context_, 0, "defer"));
402
403 io_context_->impl_.post_immediate_completion(p.p, true);
404 p.v = p.p = 0;
405 }
406 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
407
408 #if !defined(BOOST_ASIO_NO_DEPRECATED)
409 inline io_context::work::work(boost::asio::io_context& io_context)
410 : io_context_impl_(io_context.impl_)
411 {
412 io_context_impl_.work_started();
413 }
414
415 inline io_context::work::work(const work& other)
416 : io_context_impl_(other.io_context_impl_)
417 {
418 io_context_impl_.work_started();
419 }
420
421 inline io_context::work::~work()
422 {
423 io_context_impl_.work_finished();
424 }
425
426 inline boost::asio::io_context& io_context::work::get_io_context()
427 {
428 return static_cast<boost::asio::io_context&>(io_context_impl_.context());
429 }
430 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
431
432 inline boost::asio::io_context& io_context::service::get_io_context()
433 {
434 return static_cast<boost::asio::io_context&>(context());
435 }
436
437 } // namespace asio
438 } // namespace boost
439
440 #include <boost/asio/detail/pop_options.hpp>
441
442 #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP