2 // detail/blocking_executor_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
12 #define BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
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>
25 #include <boost/asio/detail/push_options.hpp>
31 template <typename Operation = scheduler_operation>
32 class blocking_executor_op_base : public Operation
35 blocking_executor_op_base(typename Operation::func_type complete_func)
36 : Operation(complete_func),
43 boost::asio::detail::mutex::scoped_lock lock(mutex_);
49 struct do_complete_cleanup
51 ~do_complete_cleanup()
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);
58 blocking_executor_op_base* op_;
62 boost::asio::detail::mutex mutex_;
63 boost::asio::detail::event event_;
67 template <typename Handler, typename Operation = scheduler_operation>
68 class blocking_executor_op : public blocking_executor_op_base<Operation>
71 blocking_executor_op(Handler& h)
72 : blocking_executor_op_base<Operation>(&blocking_executor_op::do_complete),
77 static void do_complete(void* owner, Operation* base,
78 const boost::system::error_code& /*ec*/,
79 std::size_t /*bytes_transferred*/)
81 blocking_executor_op* o(static_cast<blocking_executor_op*>(base));
83 typename blocking_executor_op_base<Operation>::do_complete_cleanup
87 BOOST_ASIO_HANDLER_COMPLETION((*o));
89 // Make the upcall if required.
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;
103 } // namespace detail
107 #include <boost/asio/detail/pop_options.hpp>
109 #endif // BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP