win_object_handle_service.hpp 5.9 KB

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