win_object_handle_service.hpp 5.5 KB

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