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