2 // detail/win_object_handle_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
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)
12 #ifndef BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
13 #define BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
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/io_context.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 class win_object_handle_service :
36 public service_base<win_object_handle_service>
39 // The native type of an object handle.
40 typedef HANDLE native_handle_type;
42 // The implementation type of the object handle.
43 class implementation_type
46 // Default constructor.
48 : handle_(INVALID_HANDLE_VALUE),
49 wait_handle_(INVALID_HANDLE_VALUE),
57 // Only this service will have access to the internal values.
58 friend class win_object_handle_service;
60 // The native object handle representation. May be accessed or modified
61 // without locking the mutex.
62 native_handle_type handle_;
64 // The handle used to unregister the wait operation. The mutex must be
65 // locked when accessing or modifying this member.
68 // The operations waiting on the object handle. If there is a registered
69 // wait then the mutex must be locked when accessing or modifying this
71 op_queue<wait_op> op_queue_;
73 // The service instance that owns the object handle implementation.
74 win_object_handle_service* owner_;
76 // Pointers to adjacent handle implementations in linked list. The mutex
77 // must be locked when accessing or modifying these members.
78 implementation_type* next_;
79 implementation_type* prev_;
83 BOOST_ASIO_DECL win_object_handle_service(
84 boost::asio::io_context& io_context);
86 // Destroy all user-defined handler objects owned by the service.
87 BOOST_ASIO_DECL void shutdown();
89 // Construct a new handle implementation.
90 BOOST_ASIO_DECL void construct(implementation_type& impl);
92 // Move-construct a new handle implementation.
93 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
94 implementation_type& other_impl);
96 // Move-assign from another handle implementation.
97 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
98 win_object_handle_service& other_service,
99 implementation_type& other_impl);
101 // Destroy a handle implementation.
102 BOOST_ASIO_DECL void destroy(implementation_type& impl);
104 // Assign a native handle to a handle implementation.
105 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
106 const native_handle_type& handle, boost::system::error_code& ec);
108 // Determine whether the handle is open.
109 bool is_open(const implementation_type& impl) const
111 return impl.handle_ != INVALID_HANDLE_VALUE && impl.handle_ != 0;
114 // Destroy a handle implementation.
115 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
116 boost::system::error_code& ec);
118 // Get the native handle representation.
119 native_handle_type native_handle(const implementation_type& impl) const
124 // Cancel all operations associated with the handle.
125 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
126 boost::system::error_code& ec);
128 // Perform a synchronous wait for the object to enter a signalled state.
129 BOOST_ASIO_DECL void wait(implementation_type& impl,
130 boost::system::error_code& ec);
132 /// Start an asynchronous wait.
133 template <typename Handler>
134 void async_wait(implementation_type& impl, Handler& handler)
136 // Allocate and construct an operation to wrap the handler.
137 typedef wait_handler<Handler> op;
138 typename op::ptr p = { boost::asio::detail::addressof(handler),
139 op::ptr::allocate(handler), 0 };
140 p.p = new (p.v) op(handler);
142 BOOST_ASIO_HANDLER_CREATION((io_context_.context(), *p.p, "object_handle",
143 &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "async_wait"));
145 start_wait_op(impl, p.p);
150 // Helper function to start an asynchronous wait operation.
151 BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, wait_op* op);
153 // Helper function to register a wait operation.
154 BOOST_ASIO_DECL void register_wait_callback(
155 implementation_type& impl, mutex::scoped_lock& lock);
157 // Callback function invoked when the registered wait completes.
158 static BOOST_ASIO_DECL VOID CALLBACK wait_callback(
159 PVOID param, BOOLEAN timeout);
161 // The io_context implementation used to post completions.
162 io_context_impl& io_context_;
164 // Mutex to protect access to internal state.
167 // The head of a linked list of all implementations.
168 implementation_type* impl_list_;
170 // Flag to indicate that the dispatcher has been shut down.
174 } // namespace detail
178 #include <boost/asio/detail/pop_options.hpp>
180 #if defined(BOOST_ASIO_HEADER_ONLY)
181 # include <boost/asio/detail/impl/win_object_handle_service.ipp>
182 #endif // defined(BOOST_ASIO_HEADER_ONLY)
184 #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
186 #endif // BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP