reactive_wait_op.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // detail/reactive_wait_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #ifndef BOOST_ASIO_DETAIL_REACTIVE_WAIT_OP_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_WAIT_OP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/bind_handler.hpp>
  17. #include <boost/asio/detail/fenced_block.hpp>
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  20. #include <boost/asio/detail/handler_work.hpp>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/reactor_op.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace detail {
  27. template <typename Handler, typename IoExecutor>
  28. class reactive_wait_op : public reactor_op
  29. {
  30. public:
  31. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_wait_op);
  32. reactive_wait_op(const boost::system::error_code& success_ec,
  33. Handler& handler, const IoExecutor& io_ex)
  34. : reactor_op(success_ec, &reactive_wait_op::do_perform,
  35. &reactive_wait_op::do_complete),
  36. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  37. work_(handler_, io_ex)
  38. {
  39. }
  40. static status do_perform(reactor_op*)
  41. {
  42. return done;
  43. }
  44. static void do_complete(void* owner, operation* base,
  45. const boost::system::error_code& /*ec*/,
  46. std::size_t /*bytes_transferred*/)
  47. {
  48. // Take ownership of the handler object.
  49. reactive_wait_op* o(static_cast<reactive_wait_op*>(base));
  50. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  51. BOOST_ASIO_HANDLER_COMPLETION((*o));
  52. // Take ownership of the operation's outstanding work.
  53. handler_work<Handler, IoExecutor> w(
  54. BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
  55. o->work_));
  56. // Make a copy of the handler so that the memory can be deallocated before
  57. // the upcall is made. Even if we're not about to make an upcall, a
  58. // sub-object of the handler may be the true owner of the memory associated
  59. // with the handler. Consequently, a local copy of the handler is required
  60. // to ensure that any owning sub-object remains valid until after we have
  61. // deallocated the memory here.
  62. detail::binder1<Handler, boost::system::error_code>
  63. handler(o->handler_, o->ec_);
  64. p.h = boost::asio::detail::addressof(handler.handler_);
  65. p.reset();
  66. // Make the upcall if required.
  67. if (owner)
  68. {
  69. fenced_block b(fenced_block::half);
  70. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  71. w.complete(handler, handler.handler_);
  72. BOOST_ASIO_HANDLER_INVOCATION_END;
  73. }
  74. }
  75. private:
  76. Handler handler_;
  77. handler_work<Handler, IoExecutor> work_;
  78. };
  79. } // namespace detail
  80. } // namespace asio
  81. } // namespace boost
  82. #include <boost/asio/detail/pop_options.hpp>
  83. #endif // BOOST_ASIO_DETAIL_REACTIVE_WAIT_OP_HPP