]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/blocking_executor_op.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / detail / blocking_executor_op.hpp
1 //
2 // detail/blocking_executor_op.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 BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
12 #define BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/event.hpp>
20 #include <boost/asio/detail/fenced_block.hpp>
21 #include <boost/asio/detail/handler_invoke_helpers.hpp>
22 #include <boost/asio/detail/mutex.hpp>
23 #include <boost/asio/detail/scheduler_operation.hpp>
24
25 #include <boost/asio/detail/push_options.hpp>
26
27 namespace boost {
28 namespace asio {
29 namespace detail {
30
31 template <typename Operation = scheduler_operation>
32 class blocking_executor_op_base : public Operation
33 {
34 public:
35 blocking_executor_op_base(typename Operation::func_type complete_func)
36 : Operation(complete_func),
37 is_complete_(false)
38 {
39 }
40
41 void wait()
42 {
43 boost::asio::detail::mutex::scoped_lock lock(mutex_);
44 while (!is_complete_)
45 event_.wait(lock);
46 }
47
48 protected:
49 struct do_complete_cleanup
50 {
51 ~do_complete_cleanup()
52 {
53 boost::asio::detail::mutex::scoped_lock lock(op_->mutex_);
54 op_->is_complete_ = true;
55 op_->event_.unlock_and_signal_one_for_destruction(lock);
56 }
57
58 blocking_executor_op_base* op_;
59 };
60
61 private:
62 boost::asio::detail::mutex mutex_;
63 boost::asio::detail::event event_;
64 bool is_complete_;
65 };
66
67 template <typename Handler, typename Operation = scheduler_operation>
68 class blocking_executor_op : public blocking_executor_op_base<Operation>
69 {
70 public:
71 blocking_executor_op(Handler& h)
72 : blocking_executor_op_base<Operation>(&blocking_executor_op::do_complete),
73 handler_(h)
74 {
75 }
76
77 static void do_complete(void* owner, Operation* base,
78 const boost::system::error_code& /*ec*/,
79 std::size_t /*bytes_transferred*/)
80 {
81 blocking_executor_op* o(static_cast<blocking_executor_op*>(base));
82
83 typename blocking_executor_op_base<Operation>::do_complete_cleanup
84 on_exit = { o };
85 (void)on_exit;
86
87 BOOST_ASIO_HANDLER_COMPLETION((*o));
88
89 // Make the upcall if required.
90 if (owner)
91 {
92 fenced_block b(fenced_block::half);
93 BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
94 boost_asio_handler_invoke_helpers::invoke(o->handler_, o->handler_);
95 BOOST_ASIO_HANDLER_INVOCATION_END;
96 }
97 }
98
99 private:
100 Handler& handler_;
101 };
102
103 } // namespace detail
104 } // namespace asio
105 } // namespace boost
106
107 #include <boost/asio/detail/pop_options.hpp>
108
109 #endif // BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP