win_iocp_overlapped_ptr.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // win_iocp_overlapped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_WIN_IOCP_OVERLAPPED_PTR_HPP
  11. #define ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/win_iocp_io_service_fwd.hpp"
  17. #if defined(ASIO_HAS_IOCP)
  18. #include "asio/detail/fenced_block.hpp"
  19. #include "asio/detail/noncopyable.hpp"
  20. #include "asio/detail/win_iocp_io_service.hpp"
  21. #include "asio/detail/win_iocp_operation.hpp"
  22. namespace asio {
  23. namespace detail {
  24. // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
  25. class win_iocp_overlapped_ptr
  26. : private noncopyable
  27. {
  28. public:
  29. // Construct an empty win_iocp_overlapped_ptr.
  30. win_iocp_overlapped_ptr()
  31. : ptr_(0),
  32. iocp_service_(0)
  33. {
  34. }
  35. // Construct an win_iocp_overlapped_ptr to contain the specified handler.
  36. template <typename Handler>
  37. explicit win_iocp_overlapped_ptr(
  38. asio::io_service& io_service, Handler handler)
  39. : ptr_(0),
  40. iocp_service_(0)
  41. {
  42. this->reset(io_service, handler);
  43. }
  44. // Destructor automatically frees the OVERLAPPED object unless released.
  45. ~win_iocp_overlapped_ptr()
  46. {
  47. reset();
  48. }
  49. // Reset to empty.
  50. void reset()
  51. {
  52. if (ptr_)
  53. {
  54. ptr_->destroy();
  55. ptr_ = 0;
  56. iocp_service_->work_finished();
  57. iocp_service_ = 0;
  58. }
  59. }
  60. // Reset to contain the specified handler, freeing any current OVERLAPPED
  61. // object.
  62. template <typename Handler>
  63. void reset(asio::io_service& io_service, Handler handler)
  64. {
  65. typedef overlapped_op<Handler> value_type;
  66. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  67. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  68. handler_ptr<alloc_traits> ptr(raw_ptr, handler);
  69. io_service.impl_.work_started();
  70. reset();
  71. ptr_ = ptr.release();
  72. iocp_service_ = &io_service.impl_;
  73. }
  74. // Get the contained OVERLAPPED object.
  75. OVERLAPPED* get()
  76. {
  77. return ptr_;
  78. }
  79. // Get the contained OVERLAPPED object.
  80. const OVERLAPPED* get() const
  81. {
  82. return ptr_;
  83. }
  84. // Release ownership of the OVERLAPPED object.
  85. OVERLAPPED* release()
  86. {
  87. if (ptr_)
  88. iocp_service_->on_pending(ptr_);
  89. OVERLAPPED* tmp = ptr_;
  90. ptr_ = 0;
  91. iocp_service_ = 0;
  92. return tmp;
  93. }
  94. // Post completion notification for overlapped operation. Releases ownership.
  95. void complete(const asio::error_code& ec,
  96. std::size_t bytes_transferred)
  97. {
  98. if (ptr_)
  99. {
  100. iocp_service_->on_completion(ptr_, ec,
  101. static_cast<DWORD>(bytes_transferred));
  102. ptr_ = 0;
  103. iocp_service_ = 0;
  104. }
  105. }
  106. private:
  107. template <typename Handler>
  108. struct overlapped_op : public win_iocp_operation
  109. {
  110. overlapped_op(Handler handler)
  111. : win_iocp_operation(&overlapped_op::do_complete),
  112. handler_(handler)
  113. {
  114. }
  115. static void do_complete(io_service_impl* owner, operation* base,
  116. asio::error_code ec, std::size_t bytes_transferred)
  117. {
  118. // Take ownership of the operation object.
  119. overlapped_op* o(static_cast<overlapped_op*>(base));
  120. typedef handler_alloc_traits<Handler, overlapped_op> alloc_traits;
  121. handler_ptr<alloc_traits> ptr(o->handler_, o);
  122. // Make the upcall if required.
  123. if (owner)
  124. {
  125. // Make a copy of the handler so that the memory can be deallocated
  126. // before the upcall is made. Even if we're not about to make an
  127. // upcall, a sub-object of the handler may be the true owner of the
  128. // memory associated with the handler. Consequently, a local copy of
  129. // the handler is required to ensure that any owning sub-object remains
  130. // valid until after we have deallocated the memory here.
  131. detail::binder2<Handler, asio::error_code, std::size_t>
  132. handler(o->handler_, ec, bytes_transferred);
  133. ptr.reset();
  134. asio::detail::fenced_block b;
  135. asio_handler_invoke_helpers::invoke(handler, handler);
  136. }
  137. }
  138. private:
  139. Handler handler_;
  140. };
  141. win_iocp_operation* ptr_;
  142. win_iocp_io_service* iocp_service_;
  143. };
  144. } // namespace detail
  145. } // namespace asio
  146. #endif // defined(ASIO_HAS_IOCP)
  147. #include "asio/detail/pop_options.hpp"
  148. #endif // ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP