win_object_handle_service.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // detail/win_object_handle_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
  12. #define BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/memory.hpp>
  20. #include <boost/asio/detail/wait_handler.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/execution_context.hpp>
  23. #if defined(BOOST_ASIO_HAS_IOCP)
  24. # include <boost/asio/detail/win_iocp_io_context.hpp>
  25. #else // defined(BOOST_ASIO_HAS_IOCP)
  26. # include <boost/asio/detail/scheduler.hpp>
  27. #endif // defined(BOOST_ASIO_HAS_IOCP)
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace detail {
  32. class win_object_handle_service :
  33. public execution_context_service_base<win_object_handle_service>
  34. {
  35. public:
  36. // The native type of an object handle.
  37. typedef HANDLE native_handle_type;
  38. // The implementation type of the object handle.
  39. class implementation_type
  40. {
  41. public:
  42. // Default constructor.
  43. implementation_type()
  44. : handle_(INVALID_HANDLE_VALUE),
  45. wait_handle_(INVALID_HANDLE_VALUE),
  46. owner_(0),
  47. next_(0),
  48. prev_(0)
  49. {
  50. }
  51. private:
  52. // Only this service will have access to the internal values.
  53. friend class win_object_handle_service;
  54. // The native object handle representation. May be accessed or modified
  55. // without locking the mutex.
  56. native_handle_type handle_;
  57. // The handle used to unregister the wait operation. The mutex must be
  58. // locked when accessing or modifying this member.
  59. HANDLE wait_handle_;
  60. // The operations waiting on the object handle. If there is a registered
  61. // wait then the mutex must be locked when accessing or modifying this
  62. // member
  63. op_queue<wait_op> op_queue_;
  64. // The service instance that owns the object handle implementation.
  65. win_object_handle_service* owner_;
  66. // Pointers to adjacent handle implementations in linked list. The mutex
  67. // must be locked when accessing or modifying these members.
  68. implementation_type* next_;
  69. implementation_type* prev_;
  70. };
  71. // Constructor.
  72. BOOST_ASIO_DECL win_object_handle_service(execution_context& context);
  73. // Destroy all user-defined handler objects owned by the service.
  74. BOOST_ASIO_DECL void shutdown();
  75. // Construct a new handle implementation.
  76. BOOST_ASIO_DECL void construct(implementation_type& impl);
  77. // Move-construct a new handle implementation.
  78. BOOST_ASIO_DECL void move_construct(implementation_type& impl,
  79. implementation_type& other_impl);
  80. // Move-assign from another handle implementation.
  81. BOOST_ASIO_DECL void move_assign(implementation_type& impl,
  82. win_object_handle_service& other_service,
  83. implementation_type& other_impl);
  84. // Destroy a handle implementation.
  85. BOOST_ASIO_DECL void destroy(implementation_type& impl);
  86. // Assign a native handle to a handle implementation.
  87. BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
  88. const native_handle_type& handle, boost::system::error_code& ec);
  89. // Determine whether the handle is open.
  90. bool is_open(const implementation_type& impl) const
  91. {
  92. return impl.handle_ != INVALID_HANDLE_VALUE && impl.handle_ != 0;
  93. }
  94. // Destroy a handle implementation.
  95. BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
  96. boost::system::error_code& ec);
  97. // Get the native handle representation.
  98. native_handle_type native_handle(const implementation_type& impl) const
  99. {
  100. return impl.handle_;
  101. }
  102. // Cancel all operations associated with the handle.
  103. BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
  104. boost::system::error_code& ec);
  105. // Perform a synchronous wait for the object to enter a signalled state.
  106. BOOST_ASIO_DECL void wait(implementation_type& impl,
  107. boost::system::error_code& ec);
  108. /// Start an asynchronous wait.
  109. template <typename Handler, typename IoExecutor>
  110. void async_wait(implementation_type& impl,
  111. Handler& handler, const IoExecutor& io_ex)
  112. {
  113. // Allocate and construct an operation to wrap the handler.
  114. typedef wait_handler<Handler, IoExecutor> op;
  115. typename op::ptr p = { boost::asio::detail::addressof(handler),
  116. op::ptr::allocate(handler), 0 };
  117. p.p = new (p.v) op(handler, io_ex);
  118. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(), *p.p, "object_handle",
  119. &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "async_wait"));
  120. start_wait_op(impl, p.p);
  121. p.v = p.p = 0;
  122. }
  123. private:
  124. // Helper function to start an asynchronous wait operation.
  125. BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, wait_op* op);
  126. // Helper function to register a wait operation.
  127. BOOST_ASIO_DECL void register_wait_callback(
  128. implementation_type& impl, mutex::scoped_lock& lock);
  129. // Callback function invoked when the registered wait completes.
  130. static BOOST_ASIO_DECL VOID CALLBACK wait_callback(
  131. PVOID param, BOOLEAN timeout);
  132. // The scheduler used to post completions.
  133. #if defined(BOOST_ASIO_HAS_IOCP)
  134. typedef class win_iocp_io_context scheduler_impl;
  135. #else
  136. typedef class scheduler scheduler_impl;
  137. #endif
  138. scheduler_impl& scheduler_;
  139. // Mutex to protect access to internal state.
  140. mutex mutex_;
  141. // The head of a linked list of all implementations.
  142. implementation_type* impl_list_;
  143. // Flag to indicate that the dispatcher has been shut down.
  144. bool shutdown_;
  145. };
  146. } // namespace detail
  147. } // namespace asio
  148. } // namespace boost
  149. #include <boost/asio/detail/pop_options.hpp>
  150. #if defined(BOOST_ASIO_HEADER_ONLY)
  151. # include <boost/asio/detail/impl/win_object_handle_service.ipp>
  152. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  153. #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  154. #endif // BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP