win_object_handle_service.hpp 5.9 KB

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