]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/asio/detail/impl/strand_service.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / asio / detail / impl / strand_service.hpp
CommitLineData
7c673cae
FG
1//
2// detail/impl/strand_service.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
92f5a8d4 5// Copyright (c) 2003-2019 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
27namespace boost {
28namespace asio {
29namespace detail {
30
31inline strand_service::strand_impl::strand_impl()
32 : operation(&strand_service::do_complete),
33 locked_(false)
34{
35}
36
37struct strand_service::on_dispatch_exit
38{
b32b8144 39 io_context_impl* io_context_;
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)
b32b8144 50 io_context_->post_immediate_completion(impl_, false);
7c673cae
FG
51 }
52};
53
54template <typename Handler>
55void 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.
67 typedef completion_handler<Handler> op;
68 typename op::ptr p = { boost::asio::detail::addressof(handler),
b32b8144 69 op::ptr::allocate(handler), 0 };
7c673cae
FG
70 p.p = new (p.v) op(handler);
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.
b32b8144 85 on_dispatch_exit on_exit = { &io_context_, impl };
7c673cae
FG
86 (void)on_exit;
87
88 completion_handler<Handler>::do_complete(
b32b8144 89 &io_context_, o, boost::system::error_code(), 0);
7c673cae
FG
90 }
91}
92
b32b8144 93// Request the io_context to invoke the given handler and return immediately.
7c673cae
FG
94template <typename Handler>
95void strand_service::post(strand_service::implementation_type& impl,
96 Handler& handler)
97{
98 bool is_continuation =
99 boost_asio_handler_cont_helpers::is_continuation(handler);
100
101 // Allocate and construct an operation to wrap the handler.
102 typedef completion_handler<Handler> op;
103 typename op::ptr p = { boost::asio::detail::addressof(handler),
b32b8144 104 op::ptr::allocate(handler), 0 };
7c673cae
FG
105 p.p = new (p.v) op(handler);
106
b32b8144
FG
107 BOOST_ASIO_HANDLER_CREATION((this->context(),
108 *p.p, "strand", impl, 0, "post"));
7c673cae
FG
109
110 do_post(impl, p.p, is_continuation);
111 p.v = p.p = 0;
112}
113
114} // namespace detail
115} // namespace asio
116} // namespace boost
117
118#include <boost/asio/detail/pop_options.hpp>
119
120#endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP