123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- //
- // detail/impl/win_object_handle_service.ipp
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef ASIO_DETAIL_IMPL_WIN_OBJECT_HANDLE_SERVICE_IPP
- #define ASIO_DETAIL_IMPL_WIN_OBJECT_HANDLE_SERVICE_IPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include "asio/detail/config.hpp"
- #if defined(ASIO_HAS_WINDOWS_OBJECT_HANDLE)
- #include "asio/detail/win_object_handle_service.hpp"
- #include "asio/detail/push_options.hpp"
- namespace asio {
- namespace detail {
- win_object_handle_service::win_object_handle_service(execution_context& context)
- : execution_context_service_base<win_object_handle_service>(context),
- scheduler_(asio::use_service<scheduler_impl>(context)),
- mutex_(),
- impl_list_(0),
- shutdown_(false)
- {
- }
- void win_object_handle_service::shutdown()
- {
- mutex::scoped_lock lock(mutex_);
- // Setting this flag to true prevents new objects from being registered, and
- // new asynchronous wait operations from being started. We only need to worry
- // about cleaning up the operations that are currently in progress.
- shutdown_ = true;
- op_queue<operation> ops;
- for (implementation_type* impl = impl_list_; impl; impl = impl->next_)
- ops.push(impl->op_queue_);
- lock.unlock();
- scheduler_.abandon_operations(ops);
- }
- void win_object_handle_service::construct(
- win_object_handle_service::implementation_type& impl)
- {
- impl.handle_ = INVALID_HANDLE_VALUE;
- impl.wait_handle_ = INVALID_HANDLE_VALUE;
- impl.owner_ = this;
- // Insert implementation into linked list of all implementations.
- mutex::scoped_lock lock(mutex_);
- if (!shutdown_)
- {
- impl.next_ = impl_list_;
- impl.prev_ = 0;
- if (impl_list_)
- impl_list_->prev_ = &impl;
- impl_list_ = &impl;
- }
- }
- void win_object_handle_service::move_construct(
- win_object_handle_service::implementation_type& impl,
- win_object_handle_service::implementation_type& other_impl)
- {
- mutex::scoped_lock lock(mutex_);
- // Insert implementation into linked list of all implementations.
- if (!shutdown_)
- {
- impl.next_ = impl_list_;
- impl.prev_ = 0;
- if (impl_list_)
- impl_list_->prev_ = &impl;
- impl_list_ = &impl;
- }
- impl.handle_ = other_impl.handle_;
- other_impl.handle_ = INVALID_HANDLE_VALUE;
- impl.wait_handle_ = other_impl.wait_handle_;
- other_impl.wait_handle_ = INVALID_HANDLE_VALUE;
- impl.op_queue_.push(other_impl.op_queue_);
- impl.owner_ = this;
- // We must not hold the lock while calling UnregisterWaitEx. This is because
- // the registered callback function might be invoked while we are waiting for
- // UnregisterWaitEx to complete.
- lock.unlock();
- if (impl.wait_handle_ != INVALID_HANDLE_VALUE)
- ::UnregisterWaitEx(impl.wait_handle_, INVALID_HANDLE_VALUE);
- if (!impl.op_queue_.empty())
- register_wait_callback(impl, lock);
- }
- void win_object_handle_service::move_assign(
- win_object_handle_service::implementation_type& impl,
- win_object_handle_service& other_service,
- win_object_handle_service::implementation_type& other_impl)
- {
- asio::error_code ignored_ec;
- close(impl, ignored_ec);
- mutex::scoped_lock lock(mutex_);
- if (this != &other_service)
- {
- // Remove implementation from linked list of all implementations.
- if (impl_list_ == &impl)
- impl_list_ = impl.next_;
- if (impl.prev_)
- impl.prev_->next_ = impl.next_;
- if (impl.next_)
- impl.next_->prev_= impl.prev_;
- impl.next_ = 0;
- impl.prev_ = 0;
- }
- impl.handle_ = other_impl.handle_;
- other_impl.handle_ = INVALID_HANDLE_VALUE;
- impl.wait_handle_ = other_impl.wait_handle_;
- other_impl.wait_handle_ = INVALID_HANDLE_VALUE;
- impl.op_queue_.push(other_impl.op_queue_);
- impl.owner_ = this;
- if (this != &other_service)
- {
- // Insert implementation into linked list of all implementations.
- impl.next_ = other_service.impl_list_;
- impl.prev_ = 0;
- if (other_service.impl_list_)
- other_service.impl_list_->prev_ = &impl;
- other_service.impl_list_ = &impl;
- }
- // We must not hold the lock while calling UnregisterWaitEx. This is because
- // the registered callback function might be invoked while we are waiting for
- // UnregisterWaitEx to complete.
- lock.unlock();
- if (impl.wait_handle_ != INVALID_HANDLE_VALUE)
- ::UnregisterWaitEx(impl.wait_handle_, INVALID_HANDLE_VALUE);
- if (!impl.op_queue_.empty())
- register_wait_callback(impl, lock);
- }
- void win_object_handle_service::destroy(
- win_object_handle_service::implementation_type& impl)
- {
- mutex::scoped_lock lock(mutex_);
- // Remove implementation from linked list of all implementations.
- if (impl_list_ == &impl)
- impl_list_ = impl.next_;
- if (impl.prev_)
- impl.prev_->next_ = impl.next_;
- if (impl.next_)
- impl.next_->prev_= impl.prev_;
- impl.next_ = 0;
- impl.prev_ = 0;
- if (is_open(impl))
- {
- ASIO_HANDLER_OPERATION((scheduler_.context(), "object_handle",
- &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "close"));
- HANDLE wait_handle = impl.wait_handle_;
- impl.wait_handle_ = INVALID_HANDLE_VALUE;
- op_queue<operation> ops;
- while (wait_op* op = impl.op_queue_.front())
- {
- op->ec_ = asio::error::operation_aborted;
- impl.op_queue_.pop();
- ops.push(op);
- }
- // We must not hold the lock while calling UnregisterWaitEx. This is
- // because the registered callback function might be invoked while we are
- // waiting for UnregisterWaitEx to complete.
- lock.unlock();
- if (wait_handle != INVALID_HANDLE_VALUE)
- ::UnregisterWaitEx(wait_handle, INVALID_HANDLE_VALUE);
- ::CloseHandle(impl.handle_);
- impl.handle_ = INVALID_HANDLE_VALUE;
- scheduler_.post_deferred_completions(ops);
- }
- }
- asio::error_code win_object_handle_service::assign(
- win_object_handle_service::implementation_type& impl,
- const native_handle_type& handle, asio::error_code& ec)
- {
- if (is_open(impl))
- {
- ec = asio::error::already_open;
- ASIO_ERROR_LOCATION(ec);
- return ec;
- }
- impl.handle_ = handle;
- ec = asio::error_code();
- return ec;
- }
- asio::error_code win_object_handle_service::close(
- win_object_handle_service::implementation_type& impl,
- asio::error_code& ec)
- {
- if (is_open(impl))
- {
- ASIO_HANDLER_OPERATION((scheduler_.context(), "object_handle",
- &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "close"));
- mutex::scoped_lock lock(mutex_);
- HANDLE wait_handle = impl.wait_handle_;
- impl.wait_handle_ = INVALID_HANDLE_VALUE;
- op_queue<operation> completed_ops;
- while (wait_op* op = impl.op_queue_.front())
- {
- impl.op_queue_.pop();
- op->ec_ = asio::error::operation_aborted;
- completed_ops.push(op);
- }
- // We must not hold the lock while calling UnregisterWaitEx. This is
- // because the registered callback function might be invoked while we are
- // waiting for UnregisterWaitEx to complete.
- lock.unlock();
- if (wait_handle != INVALID_HANDLE_VALUE)
- ::UnregisterWaitEx(wait_handle, INVALID_HANDLE_VALUE);
- if (::CloseHandle(impl.handle_))
- {
- impl.handle_ = INVALID_HANDLE_VALUE;
- ec = asio::error_code();
- }
- else
- {
- DWORD last_error = ::GetLastError();
- ec = asio::error_code(last_error,
- asio::error::get_system_category());
- }
- scheduler_.post_deferred_completions(completed_ops);
- }
- else
- {
- ec = asio::error_code();
- }
- ASIO_ERROR_LOCATION(ec);
- return ec;
- }
- asio::error_code win_object_handle_service::cancel(
- win_object_handle_service::implementation_type& impl,
- asio::error_code& ec)
- {
- if (is_open(impl))
- {
- ASIO_HANDLER_OPERATION((scheduler_.context(), "object_handle",
- &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "cancel"));
- mutex::scoped_lock lock(mutex_);
- HANDLE wait_handle = impl.wait_handle_;
- impl.wait_handle_ = INVALID_HANDLE_VALUE;
- op_queue<operation> completed_ops;
- while (wait_op* op = impl.op_queue_.front())
- {
- op->ec_ = asio::error::operation_aborted;
- impl.op_queue_.pop();
- completed_ops.push(op);
- }
- // We must not hold the lock while calling UnregisterWaitEx. This is
- // because the registered callback function might be invoked while we are
- // waiting for UnregisterWaitEx to complete.
- lock.unlock();
- if (wait_handle != INVALID_HANDLE_VALUE)
- ::UnregisterWaitEx(wait_handle, INVALID_HANDLE_VALUE);
- ec = asio::error_code();
- scheduler_.post_deferred_completions(completed_ops);
- }
- else
- {
- ec = asio::error::bad_descriptor;
- }
- ASIO_ERROR_LOCATION(ec);
- return ec;
- }
- void win_object_handle_service::wait(
- win_object_handle_service::implementation_type& impl,
- asio::error_code& ec)
- {
- switch (::WaitForSingleObject(impl.handle_, INFINITE))
- {
- case WAIT_FAILED:
- {
- DWORD last_error = ::GetLastError();
- ec = asio::error_code(last_error,
- asio::error::get_system_category());
- ASIO_ERROR_LOCATION(ec);
- break;
- }
- case WAIT_OBJECT_0:
- case WAIT_ABANDONED:
- default:
- ec = asio::error_code();
- break;
- }
- }
- void win_object_handle_service::start_wait_op(
- win_object_handle_service::implementation_type& impl, wait_op* op)
- {
- scheduler_.work_started();
- if (is_open(impl))
- {
- mutex::scoped_lock lock(mutex_);
- if (!shutdown_)
- {
- impl.op_queue_.push(op);
- // Only the first operation to be queued gets to register a wait callback.
- // Subsequent operations have to wait for the first to finish.
- if (impl.op_queue_.front() == op)
- register_wait_callback(impl, lock);
- }
- else
- {
- lock.unlock();
- scheduler_.post_deferred_completion(op);
- }
- }
- else
- {
- op->ec_ = asio::error::bad_descriptor;
- scheduler_.post_deferred_completion(op);
- }
- }
- void win_object_handle_service::register_wait_callback(
- win_object_handle_service::implementation_type& impl,
- mutex::scoped_lock& lock)
- {
- lock.lock();
- if (!RegisterWaitForSingleObject(&impl.wait_handle_,
- impl.handle_, &win_object_handle_service::wait_callback,
- &impl, INFINITE, WT_EXECUTEONLYONCE))
- {
- DWORD last_error = ::GetLastError();
- asio::error_code ec(last_error,
- asio::error::get_system_category());
- op_queue<operation> completed_ops;
- while (wait_op* op = impl.op_queue_.front())
- {
- op->ec_ = ec;
- impl.op_queue_.pop();
- completed_ops.push(op);
- }
- lock.unlock();
- scheduler_.post_deferred_completions(completed_ops);
- }
- }
- void win_object_handle_service::wait_callback(PVOID param, BOOLEAN)
- {
- implementation_type* impl = static_cast<implementation_type*>(param);
- mutex::scoped_lock lock(impl->owner_->mutex_);
- if (impl->wait_handle_ != INVALID_HANDLE_VALUE)
- {
- ::UnregisterWaitEx(impl->wait_handle_, NULL);
- impl->wait_handle_ = INVALID_HANDLE_VALUE;
- }
- if (wait_op* op = impl->op_queue_.front())
- {
- op_queue<operation> completed_ops;
- op->ec_ = asio::error_code();
- impl->op_queue_.pop();
- completed_ops.push(op);
- if (!impl->op_queue_.empty())
- {
- if (!RegisterWaitForSingleObject(&impl->wait_handle_,
- impl->handle_, &win_object_handle_service::wait_callback,
- param, INFINITE, WT_EXECUTEONLYONCE))
- {
- DWORD last_error = ::GetLastError();
- asio::error_code ec(last_error,
- asio::error::get_system_category());
- while ((op = impl->op_queue_.front()) != 0)
- {
- op->ec_ = ec;
- impl->op_queue_.pop();
- completed_ops.push(op);
- }
- }
- }
- scheduler_impl& sched = impl->owner_->scheduler_;
- lock.unlock();
- sched.post_deferred_completions(completed_ops);
- }
- }
- } // namespace detail
- } // namespace asio
- #include "asio/detail/pop_options.hpp"
- #endif // defined(ASIO_HAS_WINDOWS_OBJECT_HANDLE)
- #endif // ASIO_DETAIL_IMPL_WIN_OBJECT_HANDLE_SERVICE_IPP
|