Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/wait_handler.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_WAIT_HANDLER_HPP
0012 #define BOOST_ASIO_DETAIL_WAIT_HANDLER_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/bind_handler.hpp>
0020 #include <boost/asio/detail/fenced_block.hpp>
0021 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0022 #include <boost/asio/detail/handler_work.hpp>
0023 #include <boost/asio/detail/memory.hpp>
0024 #include <boost/asio/detail/wait_op.hpp>
0025 
0026 #include <boost/asio/detail/push_options.hpp>
0027 
0028 namespace boost {
0029 namespace asio {
0030 namespace detail {
0031 
0032 template <typename Handler, typename IoExecutor>
0033 class wait_handler : public wait_op
0034 {
0035 public:
0036   BOOST_ASIO_DEFINE_HANDLER_PTR(wait_handler);
0037 
0038   wait_handler(Handler& h, const IoExecutor& io_ex)
0039     : wait_op(&wait_handler::do_complete),
0040       handler_(static_cast<Handler&&>(h)),
0041       work_(handler_, io_ex)
0042   {
0043   }
0044 
0045   static void do_complete(void* owner, operation* base,
0046       const boost::system::error_code& /*ec*/,
0047       std::size_t /*bytes_transferred*/)
0048   {
0049     // Take ownership of the handler object.
0050     wait_handler* h(static_cast<wait_handler*>(base));
0051     ptr p = { boost::asio::detail::addressof(h->handler_), h, h };
0052 
0053     BOOST_ASIO_HANDLER_COMPLETION((*h));
0054 
0055     // Take ownership of the operation's outstanding work.
0056     handler_work<Handler, IoExecutor> w(
0057         static_cast<handler_work<Handler, IoExecutor>&&>(
0058           h->work_));
0059 
0060     // Make a copy of the handler so that the memory can be deallocated before
0061     // the upcall is made. Even if we're not about to make an upcall, a
0062     // sub-object of the handler may be the true owner of the memory associated
0063     // with the handler. Consequently, a local copy of the handler is required
0064     // to ensure that any owning sub-object remains valid until after we have
0065     // deallocated the memory here.
0066     detail::binder1<Handler, boost::system::error_code>
0067       handler(h->handler_, h->ec_);
0068     p.h = boost::asio::detail::addressof(handler.handler_);
0069     p.reset();
0070 
0071     // Make the upcall if required.
0072     if (owner)
0073     {
0074       fenced_block b(fenced_block::half);
0075       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
0076       w.complete(handler, handler.handler_);
0077       BOOST_ASIO_HANDLER_INVOCATION_END;
0078     }
0079   }
0080 
0081 private:
0082   Handler handler_;
0083   handler_work<Handler, IoExecutor> work_;
0084 };
0085 
0086 } // namespace detail
0087 } // namespace asio
0088 } // namespace boost
0089 
0090 #include <boost/asio/detail/pop_options.hpp>
0091 
0092 #endif // BOOST_ASIO_DETAIL_WAIT_HANDLER_HPP