reactive_wait_op.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // detail/reactive_wait_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_work.hpp>
  20. #include <boost/asio/detail/memory.hpp>
  21. #include <boost/asio/detail/reactor_op.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. template <typename Handler, typename IoExecutor>
  27. class reactive_wait_op : public reactor_op
  28. {
  29. public:
  30. typedef Handler handler_type;
  31. typedef IoExecutor io_executor_type;
  32. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_wait_op);
  33. reactive_wait_op(const boost::system::error_code& success_ec,
  34. Handler& handler, const IoExecutor& io_ex)
  35. : reactor_op(success_ec, &reactive_wait_op::do_perform,
  36. &reactive_wait_op::do_complete),
  37. handler_(static_cast<Handler&&>(handler)),
  38. work_(handler_, io_ex)
  39. {
  40. }
  41. static status do_perform(reactor_op*)
  42. {
  43. return done;
  44. }
  45. static void do_complete(void* owner, operation* base,
  46. const boost::system::error_code& /*ec*/,
  47. std::size_t /*bytes_transferred*/)
  48. {
  49. // Take ownership of the handler object.
  50. BOOST_ASIO_ASSUME(base != 0);
  51. reactive_wait_op* o(static_cast<reactive_wait_op*>(base));
  52. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  53. BOOST_ASIO_HANDLER_COMPLETION((*o));
  54. // Take ownership of the operation's outstanding work.
  55. handler_work<Handler, IoExecutor> w(
  56. static_cast<handler_work<Handler, IoExecutor>&&>(
  57. o->work_));
  58. // Make a copy of the handler so that the memory can be deallocated before
  59. // the upcall is made. Even if we're not about to make an upcall, a
  60. // sub-object of the handler may be the true owner of the memory associated
  61. // with the handler. Consequently, a local copy of the handler is required
  62. // to ensure that any owning sub-object remains valid until after we have
  63. // deallocated the memory here.
  64. detail::binder1<Handler, boost::system::error_code>
  65. handler(o->handler_, o->ec_);
  66. p.h = boost::asio::detail::addressof(handler.handler_);
  67. p.reset();
  68. // Make the upcall if required.
  69. if (owner)
  70. {
  71. fenced_block b(fenced_block::half);
  72. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  73. w.complete(handler, handler.handler_);
  74. BOOST_ASIO_HANDLER_INVOCATION_END;
  75. }
  76. }
  77. static void do_immediate(operation* base, bool, const void* io_ex)
  78. {
  79. // Take ownership of the handler object.
  80. BOOST_ASIO_ASSUME(base != 0);
  81. reactive_wait_op* o(static_cast<reactive_wait_op*>(base));
  82. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  83. BOOST_ASIO_HANDLER_COMPLETION((*o));
  84. // Take ownership of the operation's outstanding work.
  85. immediate_handler_work<Handler, IoExecutor> w(
  86. static_cast<handler_work<Handler, IoExecutor>&&>(
  87. o->work_));
  88. // Make a copy of the handler so that the memory can be deallocated before
  89. // the upcall is made. Even if we're not about to make an upcall, a
  90. // sub-object of the handler may be the true owner of the memory associated
  91. // with the handler. Consequently, a local copy of the handler is required
  92. // to ensure that any owning sub-object remains valid until after we have
  93. // deallocated the memory here.
  94. detail::binder1<Handler, boost::system::error_code>
  95. handler(o->handler_, o->ec_);
  96. p.h = boost::asio::detail::addressof(handler.handler_);
  97. p.reset();
  98. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  99. w.complete(handler, handler.handler_, io_ex);
  100. BOOST_ASIO_HANDLER_INVOCATION_END;
  101. }
  102. private:
  103. Handler handler_;
  104. handler_work<Handler, IoExecutor> work_;
  105. };
  106. } // namespace detail
  107. } // namespace asio
  108. } // namespace boost
  109. #include <boost/asio/detail/pop_options.hpp>
  110. #endif // BOOST_ASIO_DETAIL_REACTIVE_WAIT_OP_HPP