reactive_wait_op.hpp 4.1 KB

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