2 // detail/bulk_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_BULK_EXECUTOR_OP_HPP
12 #define BOOST_ASIO_DETAIL_BULK_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/bind_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>
23 #include <boost/asio/detail/scheduler_operation.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 template <typename Handler, typename Alloc,
32 typename Operation = scheduler_operation>
33 class bulk_executor_op : public Operation
36 BOOST_ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(bulk_executor_op);
39 bulk_executor_op(BOOST_ASIO_MOVE_ARG(H) h,
40 const Alloc& allocator, std::size_t i)
41 : Operation(&bulk_executor_op::do_complete),
42 handler_(BOOST_ASIO_MOVE_CAST(H)(h)),
43 allocator_(allocator),
48 static void do_complete(void* owner, Operation* base,
49 const boost::system::error_code& /*ec*/,
50 std::size_t /*bytes_transferred*/)
52 // Take ownership of the handler object.
53 bulk_executor_op* o(static_cast<bulk_executor_op*>(base));
54 Alloc allocator(o->allocator_);
55 ptr p = { detail::addressof(allocator), o, o };
57 BOOST_ASIO_HANDLER_COMPLETION((*o));
59 // Make a copy of the handler so that the memory can be deallocated before
60 // the upcall is made. Even if we're not about to make an upcall, a
61 // sub-object of the handler may be the true owner of the memory associated
62 // with the handler. Consequently, a local copy of the handler is required
63 // to ensure that any owning sub-object remains valid until after we have
64 // deallocated the memory here.
65 detail::binder1<Handler, std::size_t> handler(o->handler_, o->index_);
68 // Make the upcall if required.
71 fenced_block b(fenced_block::half);
72 BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
73 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
74 BOOST_ASIO_HANDLER_INVOCATION_END;
88 #include <boost/asio/detail/pop_options.hpp>
90 #endif // BOOST_ASIO_DETAIL_BULK_EXECUTOR_OP_HPP