win_object_handle_service.hpp 5.8 KB

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