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