]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/strand_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_DETAIL_IMPL_STRAND_SERVICE_HPP | |
12 | #define BOOST_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 | ||
7c673cae FG |
18 | #include <boost/asio/detail/call_stack.hpp> |
19 | #include <boost/asio/detail/completion_handler.hpp> | |
20 | #include <boost/asio/detail/fenced_block.hpp> | |
21 | #include <boost/asio/detail/handler_alloc_helpers.hpp> | |
22 | #include <boost/asio/detail/handler_invoke_helpers.hpp> | |
b32b8144 | 23 | #include <boost/asio/detail/memory.hpp> |
7c673cae FG |
24 | |
25 | #include <boost/asio/detail/push_options.hpp> | |
26 | ||
27 | namespace boost { | |
28 | namespace asio { | |
29 | namespace detail { | |
30 | ||
31 | inline strand_service::strand_impl::strand_impl() | |
32 | : operation(&strand_service::do_complete), | |
33 | locked_(false) | |
34 | { | |
35 | } | |
36 | ||
37 | struct strand_service::on_dispatch_exit | |
38 | { | |
20effc67 | 39 | io_context_impl* io_context_impl_; |
7c673cae FG |
40 | strand_impl* impl_; |
41 | ||
42 | ~on_dispatch_exit() | |
43 | { | |
44 | impl_->mutex_.lock(); | |
45 | impl_->ready_queue_.push(impl_->waiting_queue_); | |
46 | bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty(); | |
47 | impl_->mutex_.unlock(); | |
48 | ||
49 | if (more_handlers) | |
20effc67 | 50 | io_context_impl_->post_immediate_completion(impl_, false); |
7c673cae FG |
51 | } |
52 | }; | |
53 | ||
54 | template <typename Handler> | |
55 | void strand_service::dispatch(strand_service::implementation_type& impl, | |
56 | Handler& handler) | |
57 | { | |
58 | // If we are already in the strand then the handler can run immediately. | |
59 | if (call_stack<strand_impl>::contains(impl)) | |
60 | { | |
61 | fenced_block b(fenced_block::full); | |
62 | boost_asio_handler_invoke_helpers::invoke(handler, handler); | |
63 | return; | |
64 | } | |
65 | ||
66 | // Allocate and construct an operation to wrap the handler. | |
20effc67 | 67 | typedef completion_handler<Handler, io_context::executor_type> op; |
7c673cae | 68 | typename op::ptr p = { boost::asio::detail::addressof(handler), |
b32b8144 | 69 | op::ptr::allocate(handler), 0 }; |
20effc67 | 70 | p.p = new (p.v) op(handler, io_context_.get_executor()); |
7c673cae | 71 | |
b32b8144 FG |
72 | BOOST_ASIO_HANDLER_CREATION((this->context(), |
73 | *p.p, "strand", impl, 0, "dispatch")); | |
7c673cae FG |
74 | |
75 | bool dispatch_immediately = do_dispatch(impl, p.p); | |
76 | operation* o = p.p; | |
77 | p.v = p.p = 0; | |
78 | ||
79 | if (dispatch_immediately) | |
80 | { | |
81 | // Indicate that this strand is executing on the current thread. | |
82 | call_stack<strand_impl>::context ctx(impl); | |
83 | ||
84 | // Ensure the next handler, if any, is scheduled on block exit. | |
20effc67 | 85 | on_dispatch_exit on_exit = { &io_context_impl_, impl }; |
7c673cae FG |
86 | (void)on_exit; |
87 | ||
20effc67 | 88 | op::do_complete(&io_context_impl_, o, boost::system::error_code(), 0); |
7c673cae FG |
89 | } |
90 | } | |
91 | ||
b32b8144 | 92 | // Request the io_context to invoke the given handler and return immediately. |
7c673cae FG |
93 | template <typename Handler> |
94 | void strand_service::post(strand_service::implementation_type& impl, | |
95 | Handler& handler) | |
96 | { | |
97 | bool is_continuation = | |
98 | boost_asio_handler_cont_helpers::is_continuation(handler); | |
99 | ||
100 | // Allocate and construct an operation to wrap the handler. | |
20effc67 | 101 | typedef completion_handler<Handler, io_context::executor_type> op; |
7c673cae | 102 | typename op::ptr p = { boost::asio::detail::addressof(handler), |
b32b8144 | 103 | op::ptr::allocate(handler), 0 }; |
20effc67 | 104 | p.p = new (p.v) op(handler, io_context_.get_executor()); |
7c673cae | 105 | |
b32b8144 FG |
106 | BOOST_ASIO_HANDLER_CREATION((this->context(), |
107 | *p.p, "strand", impl, 0, "post")); | |
7c673cae FG |
108 | |
109 | do_post(impl, p.p, is_continuation); | |
110 | p.v = p.p = 0; | |
111 | } | |
112 | ||
113 | } // namespace detail | |
114 | } // namespace asio | |
115 | } // namespace boost | |
116 | ||
117 | #include <boost/asio/detail/pop_options.hpp> | |
118 | ||
119 | #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP |