Clementine
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 ASIO_IMPL_IO_CONTEXT_HPP
12 #define 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 "asio/detail/completion_handler.hpp"
19 #include "asio/detail/executor_op.hpp"
20 #include "asio/detail/fenced_block.hpp"
21 #include "asio/detail/handler_type_requirements.hpp"
22 #include "asio/detail/non_const_lvalue.hpp"
23 #include "asio/detail/recycling_allocator.hpp"
24 #include "asio/detail/service_registry.hpp"
25 #include "asio/detail/throw_error.hpp"
26 #include "asio/detail/type_traits.hpp"
27 
28 #include "asio/detail/push_options.hpp"
29 
30 namespace asio {
31 
32 #if !defined(GENERATING_DOCUMENTATION)
33 
34 template <typename Service>
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 <>
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 
54 io_context::get_executor() ASIO_NOEXCEPT
55 {
56  return executor_type(*this);
57 }
58 
59 #if defined(ASIO_HAS_CHRONO)
60 
61 template <typename Rep, typename Period>
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>
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>
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>
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 
98  std::size_t s = impl_.wait_one(
99  static_cast<long>(chrono::duration_cast<
100  chrono::microseconds>(rel_time).count()), ec);
101  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(ASIO_HAS_CHRONO)
113 
114 #if !defined(ASIO_NO_DEPRECATED)
115 
116 inline void io_context::reset()
117 {
118  restart();
119 }
120 
122 {
123  template <typename LegacyCompletionHandler>
124  void operator()(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  ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
130  LegacyCompletionHandler, handler) type_check;
131 
133  if (self->impl_.can_dispatch())
134  {
135  detail::fenced_block b(detail::fenced_block::full);
136  asio_handler_invoke_helpers::invoke(
137  handler2.value, handler2.value);
138  }
139  else
140  {
141  // Allocate and construct an operation to wrap the 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  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>
158 ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
159 io_context::dispatch(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
160 {
161  return async_initiate<LegacyCompletionHandler, void ()>(
162  initiate_dispatch(), handler, this);
163 }
164 
166 {
167  template <typename LegacyCompletionHandler>
168  void operator()(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  ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
174  LegacyCompletionHandler, handler) type_check;
175 
177 
178  bool is_continuation =
179  asio_handler_cont_helpers::is_continuation(handler2.value);
180 
181  // Allocate and construct an operation to wrap the 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  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>
197 ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
198 io_context::post(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
209 #endif
210 io_context::wrap(Handler handler)
211 {
212  return detail::wrapped_handler<io_context&, Handler>(*this, handler);
213 }
214 
215 #endif // !defined(ASIO_NO_DEPRECATED)
216 
217 template <typename Allocator, unsigned int Bits>
220  const basic_executor_type& other) 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(ASIO_HAS_MOVE)
240 template <typename Allocator, unsigned int Bits>
243  basic_executor_type&& other) ASIO_NOEXCEPT
244 {
245  if (this != &other)
246  {
247  io_context_ = other.io_context_;
248  allocator_ = std::move(other.allocator_);
249  bits_ = other.bits_;
250  if (Bits & outstanding_work_tracked)
251  other.io_context_ = 0;
252  }
253  return *this;
254 }
255 #endif // defined(ASIO_HAS_MOVE)
256 
257 template <typename Allocator, unsigned int Bits>
258 inline bool io_context::basic_executor_type<Allocator,
259  Bits>::running_in_this_thread() const ASIO_NOEXCEPT
260 {
261  return io_context_->impl_.can_dispatch();
262 }
263 
264 template <typename Allocator, unsigned int Bits>
265 template <typename Function>
267  ASIO_MOVE_ARG(Function) f) const
268 {
269  typedef typename decay<Function>::type function_type;
270 
271  // Invoke immediately if the blocking.possibly property is enabled and we are
272  // already inside the thread pool.
273  if ((bits_ & blocking_never) == 0 && io_context_->impl_.can_dispatch())
274  {
275  // Make a local, non-const copy of the function.
276  function_type tmp(ASIO_MOVE_CAST(Function)(f));
277 
278 #if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
279  && !defined(ASIO_NO_EXCEPTIONS)
280  try
281  {
282 #endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
283  // && !defined(ASIO_NO_EXCEPTIONS)
284  detail::fenced_block b(detail::fenced_block::full);
285  asio_handler_invoke_helpers::invoke(tmp, tmp);
286  return;
287 #if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
288  && !defined(ASIO_NO_EXCEPTIONS)
289  }
290  catch (...)
291  {
292  io_context_->impl_.capture_current_exception();
293  return;
294  }
295 #endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
296  // && !defined(ASIO_NO_EXCEPTIONS)
297  }
298 
299  // Allocate and construct an operation to wrap the function.
301  typename op::ptr p = { detail::addressof(allocator_),
302  op::ptr::allocate(allocator_), 0 };
303  p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), allocator_);
304 
305  ASIO_HANDLER_CREATION((*io_context_, *p.p,
306  "io_context", io_context_, 0, "execute"));
307 
308  io_context_->impl_.post_immediate_completion(p.p,
309  (bits_ & relationship_continuation) != 0);
310  p.v = p.p = 0;
311 }
312 
313 #if !defined(ASIO_NO_TS_EXECUTORS)
314 template <typename Allocator, unsigned int Bits>
316  Allocator, Bits>::context() const ASIO_NOEXCEPT
317 {
318  return *io_context_;
319 }
320 
321 template <typename Allocator, unsigned int Bits>
322 inline void io_context::basic_executor_type<Allocator,
323  Bits>::on_work_started() const ASIO_NOEXCEPT
324 {
325  io_context_->impl_.work_started();
326 }
327 
328 template <typename Allocator, unsigned int Bits>
329 inline void io_context::basic_executor_type<Allocator,
330  Bits>::on_work_finished() const ASIO_NOEXCEPT
331 {
332  io_context_->impl_.work_finished();
333 }
334 
335 template <typename Allocator, unsigned int Bits>
336 template <typename Function, typename OtherAllocator>
338  ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
339 {
340  typedef typename decay<Function>::type function_type;
341 
342  // Invoke immediately if we are already inside the thread pool.
343  if (io_context_->impl_.can_dispatch())
344  {
345  // Make a local, non-const copy of the function.
346  function_type tmp(ASIO_MOVE_CAST(Function)(f));
347 
348  detail::fenced_block b(detail::fenced_block::full);
349  asio_handler_invoke_helpers::invoke(tmp, tmp);
350  return;
351  }
352 
353  // Allocate and construct an operation to wrap the function.
354  typedef detail::executor_op<function_type,
355  OtherAllocator, detail::operation> op;
356  typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
357  p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
358 
359  ASIO_HANDLER_CREATION((*io_context_, *p.p,
360  "io_context", io_context_, 0, "dispatch"));
361 
362  io_context_->impl_.post_immediate_completion(p.p, false);
363  p.v = p.p = 0;
364 }
365 
366 template <typename Allocator, unsigned int Bits>
367 template <typename Function, typename OtherAllocator>
369  ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
370 {
371  typedef typename decay<Function>::type function_type;
372 
373  // Allocate and construct an operation to wrap the function.
374  typedef detail::executor_op<function_type,
375  OtherAllocator, detail::operation> op;
376  typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
377  p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
378 
379  ASIO_HANDLER_CREATION((*io_context_, *p.p,
380  "io_context", io_context_, 0, "post"));
381 
382  io_context_->impl_.post_immediate_completion(p.p, false);
383  p.v = p.p = 0;
384 }
385 
386 template <typename Allocator, unsigned int Bits>
387 template <typename Function, typename OtherAllocator>
389  ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
390 {
391  typedef typename decay<Function>::type function_type;
392 
393  // Allocate and construct an operation to wrap the function.
394  typedef detail::executor_op<function_type,
395  OtherAllocator, detail::operation> op;
396  typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
397  p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
398 
399  ASIO_HANDLER_CREATION((*io_context_, *p.p,
400  "io_context", io_context_, 0, "defer"));
401 
402  io_context_->impl_.post_immediate_completion(p.p, true);
403  p.v = p.p = 0;
404 }
405 #endif // !defined(ASIO_NO_TS_EXECUTORS)
406 
407 #if !defined(ASIO_NO_DEPRECATED)
409  : io_context_impl_(io_context.impl_)
410 {
411  io_context_impl_.work_started();
412 }
413 
414 inline io_context::work::work(const work& other)
415  : io_context_impl_(other.io_context_impl_)
416 {
417  io_context_impl_.work_started();
418 }
419 
421 {
422  io_context_impl_.work_finished();
423 }
424 
426 {
427  return static_cast<asio::io_context&>(io_context_impl_.context());
428 }
429 #endif // !defined(ASIO_NO_DEPRECATED)
430 
432 {
433  return static_cast<asio::io_context&>(context());
434 }
435 
436 } // namespace asio
437 
438 #include "asio/detail/pop_options.hpp"
439 
440 #endif // ASIO_IMPL_IO_CONTEXT_HPP
void defer(ASIO_MOVE_ARG(Function) f, const OtherAllocator &a) const
Request the io_context to invoke the given function object.
Definition: io_context.hpp:388
detail::wrapped_handler< io_context &, Handler > wrap(Handler handler)
(Deprecated: Use asio::bind_executor().) Create a new handler that automatically dispatches the wrapp...
Definition: io_context.hpp:210
post(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
(Deprecated: Use asio::post().) Request the io_context to invoke the given handler and return immedia...
Definition: io_context.hpp:198
Definition: blocking.hpp:208
basic_executor_type< std::allocator< void >, 0 > executor_type
Executor used to submit functions to an io_context.
Definition: io_context.hpp:228
ASIO_INITFN_AUTO_RESULT_TYPE(RangeConnectHandler, void(asio::error_code, typename Protocol::endpoint)) async_connect(basic_socket< Protocol
Asynchronously establishes a socket connection by trying each endpoint in a sequence.
Provides core I/O functionality.
Definition: io_context.hpp:211
void dispatch(ASIO_MOVE_ARG(Function) f, const OtherAllocator &a) const
Request the io_context to invoke the given function object.
Definition: io_context.hpp:337
Definition: executor_op.hpp:31
Definition: non_const_lvalue.hpp:27
Executor implementation type used to submit functions to an io_context.
Definition: io_context.hpp:222
(Deprecated: Use executor_work_guard.) Class to inform the io_context when it has work to do...
Definition: io_context.hpp:1145
dispatch(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
(Deprecated: Use asio::dispatch().) Request the io_context to invoke the given handler.
Definition: io_context.hpp:159
void post(ASIO_MOVE_ARG(Function) f, const OtherAllocator &a) const
Request the io_context to invoke the given function object.
Definition: io_context.hpp:368
Definition: null_fenced_block.hpp:25
work(asio::io_context &io_context)
Constructor notifies the io_context that work is starting.
Definition: io_context.hpp:408
void reset()
(Deprecated: Use restart().) Reset the io_context in preparation for a subsequent run() invocation...
Definition: io_context.hpp:116
Class to represent an error code value.
Definition: error_code.hpp:80
basic_executor_type & operator=(const basic_executor_type &other) ASIO_NOEXCEPT
Assignment operator.
Definition: io_context.hpp:219
void execute(ASIO_MOVE_ARG(Function) f) const
Execution function.
Definition: io_context.hpp:266
Definition: io_context.hpp:121
asio::io_context & get_io_context()
Get the io_context object that owns the service.
Definition: io_context.hpp:431
executor_type get_executor() ASIO_NOEXCEPT
Obtains the executor associated with the io_context.
Definition: io_context.hpp:54
execution_context & context()
Get the context object that owns the service.
Definition: execution_context.hpp:100
~work()
Destructor notifies the io_context that the work is complete.
Definition: io_context.hpp:420
ASIO_DECL void restart()
Restart the io_context in preparation for a subsequent run() invocation.
Definition: io_context.ipp:129
Definition: wrapped_handler.hpp:48
Definition: io_context.hpp:165
Definition: any_io_executor.hpp:28
Definition: scheduler.hpp:38
Definition: completion_handler.hpp:31
asio::io_context & get_io_context()
Get the io_context associated with the work.
Definition: io_context.hpp:425