win_object_handle_service.hpp 6.2 KB

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