2 // detail/impl/win_object_handle_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2023 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_IMPL_WIN_OBJECT_HANDLE_SERVICE_IPP
13 #define BOOST_ASIO_DETAIL_IMPL_WIN_OBJECT_HANDLE_SERVICE_IPP
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/win_object_handle_service.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 win_object_handle_service::win_object_handle_service(execution_context& context)
32 : execution_context_service_base<win_object_handle_service>(context),
33 scheduler_(boost::asio::use_service<scheduler_impl>(context)),
40 void win_object_handle_service::shutdown()
42 mutex::scoped_lock lock(mutex_);
44 // Setting this flag to true prevents new objects from being registered, and
45 // new asynchronous wait operations from being started. We only need to worry
46 // about cleaning up the operations that are currently in progress.
49 op_queue<operation> ops;
50 for (implementation_type* impl = impl_list_; impl; impl = impl->next_)
51 ops.push(impl->op_queue_);
55 scheduler_.abandon_operations(ops);
58 void win_object_handle_service::construct(
59 win_object_handle_service::implementation_type& impl)
61 impl.handle_ = INVALID_HANDLE_VALUE;
62 impl.wait_handle_ = INVALID_HANDLE_VALUE;
65 // Insert implementation into linked list of all implementations.
66 mutex::scoped_lock lock(mutex_);
69 impl.next_ = impl_list_;
72 impl_list_->prev_ = &impl;
77 void win_object_handle_service::move_construct(
78 win_object_handle_service::implementation_type& impl,
79 win_object_handle_service::implementation_type& other_impl)
81 mutex::scoped_lock lock(mutex_);
83 // Insert implementation into linked list of all implementations.
86 impl.next_ = impl_list_;
89 impl_list_->prev_ = &impl;
93 impl.handle_ = other_impl.handle_;
94 other_impl.handle_ = INVALID_HANDLE_VALUE;
95 impl.wait_handle_ = other_impl.wait_handle_;
96 other_impl.wait_handle_ = INVALID_HANDLE_VALUE;
97 impl.op_queue_.push(other_impl.op_queue_);
100 // We must not hold the lock while calling UnregisterWaitEx. This is because
101 // the registered callback function might be invoked while we are waiting for
102 // UnregisterWaitEx to complete.
105 if (impl.wait_handle_ != INVALID_HANDLE_VALUE)
106 ::UnregisterWaitEx(impl.wait_handle_, INVALID_HANDLE_VALUE);
108 if (!impl.op_queue_.empty())
109 register_wait_callback(impl, lock);
112 void win_object_handle_service::move_assign(
113 win_object_handle_service::implementation_type& impl,
114 win_object_handle_service& other_service,
115 win_object_handle_service::implementation_type& other_impl)
117 boost::system::error_code ignored_ec;
118 close(impl, ignored_ec);
120 mutex::scoped_lock lock(mutex_);
122 if (this != &other_service)
124 // Remove implementation from linked list of all implementations.
125 if (impl_list_ == &impl)
126 impl_list_ = impl.next_;
128 impl.prev_->next_ = impl.next_;
130 impl.next_->prev_= impl.prev_;
135 impl.handle_ = other_impl.handle_;
136 other_impl.handle_ = INVALID_HANDLE_VALUE;
137 impl.wait_handle_ = other_impl.wait_handle_;
138 other_impl.wait_handle_ = INVALID_HANDLE_VALUE;
139 impl.op_queue_.push(other_impl.op_queue_);
142 if (this != &other_service)
144 // Insert implementation into linked list of all implementations.
145 impl.next_ = other_service.impl_list_;
147 if (other_service.impl_list_)
148 other_service.impl_list_->prev_ = &impl;
149 other_service.impl_list_ = &impl;
152 // We must not hold the lock while calling UnregisterWaitEx. This is because
153 // the registered callback function might be invoked while we are waiting for
154 // UnregisterWaitEx to complete.
157 if (impl.wait_handle_ != INVALID_HANDLE_VALUE)
158 ::UnregisterWaitEx(impl.wait_handle_, INVALID_HANDLE_VALUE);
160 if (!impl.op_queue_.empty())
161 register_wait_callback(impl, lock);
164 void win_object_handle_service::destroy(
165 win_object_handle_service::implementation_type& impl)
167 mutex::scoped_lock lock(mutex_);
169 // Remove implementation from linked list of all implementations.
170 if (impl_list_ == &impl)
171 impl_list_ = impl.next_;
173 impl.prev_->next_ = impl.next_;
175 impl.next_->prev_= impl.prev_;
181 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(), "object_handle",
182 &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "close"));
184 HANDLE wait_handle = impl.wait_handle_;
185 impl.wait_handle_ = INVALID_HANDLE_VALUE;
187 op_queue<operation> ops;
188 while (wait_op* op = impl.op_queue_.front())
190 op->ec_ = boost::asio::error::operation_aborted;
191 impl.op_queue_.pop();
195 // We must not hold the lock while calling UnregisterWaitEx. This is
196 // because the registered callback function might be invoked while we are
197 // waiting for UnregisterWaitEx to complete.
200 if (wait_handle != INVALID_HANDLE_VALUE)
201 ::UnregisterWaitEx(wait_handle, INVALID_HANDLE_VALUE);
203 ::CloseHandle(impl.handle_);
204 impl.handle_ = INVALID_HANDLE_VALUE;
206 scheduler_.post_deferred_completions(ops);
210 boost::system::error_code win_object_handle_service::assign(
211 win_object_handle_service::implementation_type& impl,
212 const native_handle_type& handle, boost::system::error_code& ec)
216 ec = boost::asio::error::already_open;
217 BOOST_ASIO_ERROR_LOCATION(ec);
221 impl.handle_ = handle;
222 ec = boost::system::error_code();
226 boost::system::error_code win_object_handle_service::close(
227 win_object_handle_service::implementation_type& impl,
228 boost::system::error_code& ec)
232 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(), "object_handle",
233 &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "close"));
235 mutex::scoped_lock lock(mutex_);
237 HANDLE wait_handle = impl.wait_handle_;
238 impl.wait_handle_ = INVALID_HANDLE_VALUE;
240 op_queue<operation> completed_ops;
241 while (wait_op* op = impl.op_queue_.front())
243 impl.op_queue_.pop();
244 op->ec_ = boost::asio::error::operation_aborted;
245 completed_ops.push(op);
248 // We must not hold the lock while calling UnregisterWaitEx. This is
249 // because the registered callback function might be invoked while we are
250 // waiting for UnregisterWaitEx to complete.
253 if (wait_handle != INVALID_HANDLE_VALUE)
254 ::UnregisterWaitEx(wait_handle, INVALID_HANDLE_VALUE);
256 if (::CloseHandle(impl.handle_))
258 impl.handle_ = INVALID_HANDLE_VALUE;
259 ec = boost::system::error_code();
263 DWORD last_error = ::GetLastError();
264 ec = boost::system::error_code(last_error,
265 boost::asio::error::get_system_category());
268 scheduler_.post_deferred_completions(completed_ops);
272 ec = boost::system::error_code();
275 BOOST_ASIO_ERROR_LOCATION(ec);
279 boost::system::error_code win_object_handle_service::cancel(
280 win_object_handle_service::implementation_type& impl,
281 boost::system::error_code& ec)
285 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(), "object_handle",
286 &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "cancel"));
288 mutex::scoped_lock lock(mutex_);
290 HANDLE wait_handle = impl.wait_handle_;
291 impl.wait_handle_ = INVALID_HANDLE_VALUE;
293 op_queue<operation> completed_ops;
294 while (wait_op* op = impl.op_queue_.front())
296 op->ec_ = boost::asio::error::operation_aborted;
297 impl.op_queue_.pop();
298 completed_ops.push(op);
301 // We must not hold the lock while calling UnregisterWaitEx. This is
302 // because the registered callback function might be invoked while we are
303 // waiting for UnregisterWaitEx to complete.
306 if (wait_handle != INVALID_HANDLE_VALUE)
307 ::UnregisterWaitEx(wait_handle, INVALID_HANDLE_VALUE);
309 ec = boost::system::error_code();
311 scheduler_.post_deferred_completions(completed_ops);
315 ec = boost::asio::error::bad_descriptor;
318 BOOST_ASIO_ERROR_LOCATION(ec);
322 void win_object_handle_service::wait(
323 win_object_handle_service::implementation_type& impl,
324 boost::system::error_code& ec)
326 switch (::WaitForSingleObject(impl.handle_, INFINITE))
330 DWORD last_error = ::GetLastError();
331 ec = boost::system::error_code(last_error,
332 boost::asio::error::get_system_category());
333 BOOST_ASIO_ERROR_LOCATION(ec);
339 ec = boost::system::error_code();
344 void win_object_handle_service::start_wait_op(
345 win_object_handle_service::implementation_type& impl, wait_op* op)
347 scheduler_.work_started();
351 mutex::scoped_lock lock(mutex_);
355 impl.op_queue_.push(op);
357 // Only the first operation to be queued gets to register a wait callback.
358 // Subsequent operations have to wait for the first to finish.
359 if (impl.op_queue_.front() == op)
360 register_wait_callback(impl, lock);
365 scheduler_.post_deferred_completion(op);
370 op->ec_ = boost::asio::error::bad_descriptor;
371 scheduler_.post_deferred_completion(op);
375 void win_object_handle_service::register_wait_callback(
376 win_object_handle_service::implementation_type& impl,
377 mutex::scoped_lock& lock)
381 if (!RegisterWaitForSingleObject(&impl.wait_handle_,
382 impl.handle_, &win_object_handle_service::wait_callback,
383 &impl, INFINITE, WT_EXECUTEONLYONCE))
385 DWORD last_error = ::GetLastError();
386 boost::system::error_code ec(last_error,
387 boost::asio::error::get_system_category());
389 op_queue<operation> completed_ops;
390 while (wait_op* op = impl.op_queue_.front())
393 impl.op_queue_.pop();
394 completed_ops.push(op);
398 scheduler_.post_deferred_completions(completed_ops);
402 void win_object_handle_service::wait_callback(PVOID param, BOOLEAN)
404 implementation_type* impl = static_cast<implementation_type*>(param);
405 mutex::scoped_lock lock(impl->owner_->mutex_);
407 if (impl->wait_handle_ != INVALID_HANDLE_VALUE)
409 ::UnregisterWaitEx(impl->wait_handle_, NULL);
410 impl->wait_handle_ = INVALID_HANDLE_VALUE;
413 if (wait_op* op = impl->op_queue_.front())
415 op_queue<operation> completed_ops;
417 op->ec_ = boost::system::error_code();
418 impl->op_queue_.pop();
419 completed_ops.push(op);
421 if (!impl->op_queue_.empty())
423 if (!RegisterWaitForSingleObject(&impl->wait_handle_,
424 impl->handle_, &win_object_handle_service::wait_callback,
425 param, INFINITE, WT_EXECUTEONLYONCE))
427 DWORD last_error = ::GetLastError();
428 boost::system::error_code ec(last_error,
429 boost::asio::error::get_system_category());
431 while ((op = impl->op_queue_.front()) != 0)
434 impl->op_queue_.pop();
435 completed_ops.push(op);
440 scheduler_impl& sched = impl->owner_->scheduler_;
442 sched.post_deferred_completions(completed_ops);
446 } // namespace detail
450 #include <boost/asio/detail/pop_options.hpp>
452 #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
454 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_OBJECT_HANDLE_SERVICE_IPP