Clementine
strand_service.hpp
1 //
2 // detail/impl/strand_service.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_DETAIL_IMPL_STRAND_SERVICE_HPP
12 #define ASIO_DETAIL_IMPL_STRAND_SERVICE_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/call_stack.hpp"
19 #include "asio/detail/completion_handler.hpp"
20 #include "asio/detail/fenced_block.hpp"
21 #include "asio/detail/handler_alloc_helpers.hpp"
22 #include "asio/detail/handler_invoke_helpers.hpp"
23 #include "asio/detail/memory.hpp"
24 
25 #include "asio/detail/push_options.hpp"
26 
27 namespace asio {
28 namespace detail {
29 
30 inline strand_service::strand_impl::strand_impl()
31  : operation(&strand_service::do_complete),
32  locked_(false)
33 {
34 }
35 
37 {
38  io_context_impl* io_context_impl_;
39  strand_impl* impl_;
40 
42  {
43  impl_->mutex_.lock();
44  impl_->ready_queue_.push(impl_->waiting_queue_);
45  bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
46  impl_->mutex_.unlock();
47 
48  if (more_handlers)
49  io_context_impl_->post_immediate_completion(impl_, false);
50  }
51 };
52 
53 template <typename Handler>
54 void strand_service::dispatch(strand_service::implementation_type& impl,
55  Handler& handler)
56 {
57  // If we are already in the strand then the handler can run immediately.
59  {
60  fenced_block b(fenced_block::full);
61  asio_handler_invoke_helpers::invoke(handler, handler);
62  return;
63  }
64 
65  // Allocate and construct an operation to wrap the handler.
67  typename op::ptr p = { asio::detail::addressof(handler),
68  op::ptr::allocate(handler), 0 };
69  p.p = new (p.v) op(handler, io_context_.get_executor());
70 
71  ASIO_HANDLER_CREATION((this->context(),
72  *p.p, "strand", impl, 0, "dispatch"));
73 
74  bool dispatch_immediately = do_dispatch(impl, p.p);
75  operation* o = p.p;
76  p.v = p.p = 0;
77 
78  if (dispatch_immediately)
79  {
80  // Indicate that this strand is executing on the current thread.
82 
83  // Ensure the next handler, if any, is scheduled on block exit.
84  on_dispatch_exit on_exit = { &io_context_impl_, impl };
85  (void)on_exit;
86 
87  op::do_complete(&io_context_impl_, o, asio::error_code(), 0);
88  }
89 }
90 
91 // Request the io_context to invoke the given handler and return immediately.
92 template <typename Handler>
93 void strand_service::post(strand_service::implementation_type& impl,
94  Handler& handler)
95 {
96  bool is_continuation =
97  asio_handler_cont_helpers::is_continuation(handler);
98 
99  // Allocate and construct an operation to wrap the handler.
101  typename op::ptr p = { asio::detail::addressof(handler),
102  op::ptr::allocate(handler), 0 };
103  p.p = new (p.v) op(handler, io_context_.get_executor());
104 
105  ASIO_HANDLER_CREATION((this->context(),
106  *p.p, "strand", impl, 0, "post"));
107 
108  do_post(impl, p.p, is_continuation);
109  p.v = p.p = 0;
110 }
111 
112 } // namespace detail
113 } // namespace asio
114 
115 #include "asio/detail/pop_options.hpp"
116 
117 #endif // ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
Definition: strand_service.hpp:44
Definition: strand_service.hpp:36
Definition: chrono.h:284
Definition: null_fenced_block.hpp:25
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: call_stack.hpp:30
execution_context & context()
Get the context object that owns the service.
Definition: execution_context.hpp:100
Definition: call_stack.hpp:34
Definition: any_io_executor.hpp:28
Definition: scheduler.hpp:38
Definition: completion_handler.hpp:31