Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:38

0001 //
0002 // detail/executor_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_EXECUTOR_OP_HPP
0012 #define BOOST_ASIO_DETAIL_EXECUTOR_OP_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/fenced_block.hpp>
0020 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0021 #include <boost/asio/detail/scheduler_operation.hpp>
0022 
0023 #include <boost/asio/detail/push_options.hpp>
0024 
0025 namespace boost {
0026 namespace asio {
0027 namespace detail {
0028 
0029 template <typename Handler, typename Alloc,
0030     typename Operation = scheduler_operation>
0031 class executor_op : public Operation
0032 {
0033 public:
0034   BOOST_ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(executor_op);
0035 
0036   template <typename H>
0037   executor_op(H&& h, const Alloc& allocator)
0038     : Operation(&executor_op::do_complete),
0039       handler_(static_cast<H&&>(h)),
0040       allocator_(allocator)
0041   {
0042   }
0043 
0044   static void do_complete(void* owner, Operation* base,
0045       const boost::system::error_code& /*ec*/,
0046       std::size_t /*bytes_transferred*/)
0047   {
0048     // Take ownership of the handler object.
0049     BOOST_ASIO_ASSUME(base != 0);
0050     executor_op* o(static_cast<executor_op*>(base));
0051     Alloc allocator(o->allocator_);
0052     ptr p = { detail::addressof(allocator), o, o };
0053 
0054     BOOST_ASIO_HANDLER_COMPLETION((*o));
0055 
0056     // Make a copy of the handler so that the memory can be deallocated before
0057     // the upcall is made. Even if we're not about to make an upcall, a
0058     // sub-object of the handler may be the true owner of the memory associated
0059     // with the handler. Consequently, a local copy of the handler is required
0060     // to ensure that any owning sub-object remains valid until after we have
0061     // deallocated the memory here.
0062     Handler handler(static_cast<Handler&&>(o->handler_));
0063     p.reset();
0064 
0065     // Make the upcall if required.
0066     if (owner)
0067     {
0068       fenced_block b(fenced_block::half);
0069       BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
0070       static_cast<Handler&&>(handler)();
0071       BOOST_ASIO_HANDLER_INVOCATION_END;
0072     }
0073   }
0074 
0075 private:
0076   Handler handler_;
0077   Alloc allocator_;
0078 };
0079 
0080 } // namespace detail
0081 } // namespace asio
0082 } // namespace boost
0083 
0084 #include <boost/asio/detail/pop_options.hpp>
0085 
0086 #endif // BOOST_ASIO_DETAIL_EXECUTOR_OP_HPP