Clementine
io_context_strand.hpp
1 //
2 // io_context_strand.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_IO_CONTEXT_STRAND_HPP
12 #define ASIO_IO_CONTEXT_STRAND_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/config.hpp"
19 
20 #if !defined(ASIO_NO_EXTENSIONS) \
21  && !defined(ASIO_NO_TS_EXECUTORS)
22 
23 #include "asio/async_result.hpp"
24 #include "asio/detail/handler_type_requirements.hpp"
25 #include "asio/detail/strand_service.hpp"
26 #include "asio/detail/wrapped_handler.hpp"
27 #include "asio/io_context.hpp"
28 
29 #include "asio/detail/push_options.hpp"
30 
31 namespace asio {
32 
34 
90 {
91 public:
93 
100  : service_(asio::use_service<
101  asio::detail::strand_service>(io_context))
102  {
103  service_.construct(impl_);
104  }
105 
107 
114  {
115  }
116 
118  asio::io_context& context() const ASIO_NOEXCEPT
119  {
120  return service_.get_io_context();
121  }
122 
124 
127  void on_work_started() const ASIO_NOEXCEPT
128  {
130  }
131 
133 
136  void on_work_finished() const ASIO_NOEXCEPT
137  {
139  }
140 
142 
156  template <typename Function, typename Allocator>
157  void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator& a) const
158  {
159  typename decay<Function>::type tmp(ASIO_MOVE_CAST(Function)(f));
160  service_.dispatch(impl_, tmp);
161  (void)a;
162  }
163 
164 #if !defined(ASIO_NO_DEPRECATED)
165 
185  template <typename LegacyCompletionHandler>
186  ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
187  dispatch(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
188  {
189  return async_initiate<LegacyCompletionHandler, void ()>(
190  initiate_dispatch(), handler, this);
191  }
192 #endif // !defined(ASIO_NO_DEPRECATED)
193 
195 
207  template <typename Function, typename Allocator>
208  void post(ASIO_MOVE_ARG(Function) f, const Allocator& a) const
209  {
210  typename decay<Function>::type tmp(ASIO_MOVE_CAST(Function)(f));
211  service_.post(impl_, tmp);
212  (void)a;
213  }
214 
215 #if !defined(ASIO_NO_DEPRECATED)
216 
232  template <typename LegacyCompletionHandler>
233  ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
234  post(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
235  {
236  return async_initiate<LegacyCompletionHandler, void ()>(
237  initiate_post(), handler, this);
238  }
239 #endif // !defined(ASIO_NO_DEPRECATED)
240 
242 
254  template <typename Function, typename Allocator>
255  void defer(ASIO_MOVE_ARG(Function) f, const Allocator& a) const
256  {
257  typename decay<Function>::type tmp(ASIO_MOVE_CAST(Function)(f));
258  service_.post(impl_, tmp);
259  (void)a;
260  }
261 
262 #if !defined(ASIO_NO_DEPRECATED)
263 
284  template <typename Handler>
285 #if defined(GENERATING_DOCUMENTATION)
286  unspecified
287 #else
289 #endif
290  wrap(Handler handler)
291  {
293  detail::is_continuation_if_running>(*this, handler);
294  }
295 #endif // !defined(ASIO_NO_DEPRECATED)
296 
298 
303  bool running_in_this_thread() const ASIO_NOEXCEPT
304  {
305  return service_.running_in_this_thread(impl_);
306  }
307 
309 
313  friend bool operator==(const strand& a, const strand& b) ASIO_NOEXCEPT
314  {
315  return a.impl_ == b.impl_;
316  }
317 
319 
323  friend bool operator!=(const strand& a, const strand& b) ASIO_NOEXCEPT
324  {
325  return a.impl_ != b.impl_;
326  }
327 
328 private:
329 #if !defined(ASIO_NO_DEPRECATED)
330  struct initiate_dispatch
331  {
332  template <typename LegacyCompletionHandler>
333  void operator()(ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
334  strand* self) const
335  {
336  // If you get an error on the following line it means that your
337  // handler does not meet the documented type requirements for a
338  // LegacyCompletionHandler.
339  ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
340  LegacyCompletionHandler, handler) type_check;
341 
343  self->service_.dispatch(self->impl_, handler2.value);
344  }
345  };
346 
347  struct initiate_post
348  {
349  template <typename LegacyCompletionHandler>
350  void operator()(ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
351  strand* self) const
352  {
353  // If you get an error on the following line it means that your
354  // handler does not meet the documented type requirements for a
355  // LegacyCompletionHandler.
356  ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
357  LegacyCompletionHandler, handler) type_check;
358 
360  self->service_.post(self->impl_, handler2.value);
361  }
362  };
363 #endif // !defined(ASIO_NO_DEPRECATED)
364 
367 };
368 
369 } // namespace asio
370 
371 #include "asio/detail/pop_options.hpp"
372 
373 #endif // !defined(ASIO_NO_EXTENSIONS)
374  // && !defined(ASIO_NO_TS_EXECUTORS)
375 
376 #endif // ASIO_IO_CONTEXT_STRAND_HPP
void on_work_started() const ASIO_NOEXCEPT
Inform the strand that it has some outstanding work to do.
Definition: io_context_strand.hpp:127
Definition: strand_service.hpp:44
strand(asio::io_context &io_context)
Constructor.
Definition: io_context_strand.hpp:99
void on_work_finished() const ASIO_NOEXCEPT
Inform the strand that some work is no longer outstanding.
Definition: io_context_strand.hpp:136
friend bool operator==(const strand &a, const strand &b) ASIO_NOEXCEPT
Compare two strands for equality.
Definition: io_context_strand.hpp:313
friend bool operator!=(const strand &a, const strand &b) ASIO_NOEXCEPT
Compare two strands for inequality.
Definition: io_context_strand.hpp:323
Provides serialised handler execution.
Definition: io_context_strand.hpp:89
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
asio::io_context & context() const ASIO_NOEXCEPT
Obtain the underlying execution context.
Definition: io_context_strand.hpp:118
Definition: chrono.h:284
bool running_in_this_thread() const ASIO_NOEXCEPT
Determine whether the strand is running in the current thread.
Definition: io_context_strand.hpp:303
void post(ASIO_MOVE_ARG(Function) f, const Allocator &a) const
Request the strand to invoke the given function object.
Definition: io_context_strand.hpp:208
detail::wrapped_handler< strand, Handler, detail::is_continuation_if_running > wrap(Handler handler)
(Deprecated: Use asio::bind_executor().) Create a new handler that automatically dispatches the wrapp...
Definition: io_context_strand.hpp:290
Definition: non_const_lvalue.hpp:27
Definition: strand_service.hpp:31
void defer(ASIO_MOVE_ARG(Function) f, const Allocator &a) const
Request the strand to invoke the given function object.
Definition: io_context_strand.hpp:255
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
void on_work_finished() const ASIO_NOEXCEPT
Inform the io_context that some work is no longer outstanding.
Definition: io_context.hpp:330
void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator &a) const
Request the strand to invoke the given function object.
Definition: io_context_strand.hpp:157
~strand()
Destructor.
Definition: io_context_strand.hpp:113
Definition: wrapped_handler.hpp:48
Definition: wrapped_handler.hpp:37
Definition: any_io_executor.hpp:28
friend Service & use_service(io_context &ioc)
Definition: io_context.hpp:35
void on_work_started() const ASIO_NOEXCEPT
Inform the io_context that it has some outstanding work to do.
Definition: io_context.hpp:323