Chris@16: // Chris@16: // detail/win_object_handle_service.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Copyright (c) 2011 Boris Schaeling (boris@highscore.de) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP Chris@16: #define BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: class win_object_handle_service Chris@16: { Chris@16: public: Chris@16: // The native type of an object handle. Chris@16: typedef HANDLE native_handle_type; Chris@16: Chris@16: // The implementation type of the object handle. Chris@16: class implementation_type Chris@16: { Chris@16: public: Chris@16: // Default constructor. Chris@16: implementation_type() Chris@16: : handle_(INVALID_HANDLE_VALUE), Chris@16: wait_handle_(INVALID_HANDLE_VALUE), Chris@16: owner_(0), Chris@16: next_(0), Chris@16: prev_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: private: Chris@16: // Only this service will have access to the internal values. Chris@16: friend class win_object_handle_service; Chris@16: Chris@16: // The native object handle representation. May be accessed or modified Chris@16: // without locking the mutex. Chris@16: native_handle_type handle_; Chris@16: Chris@16: // The handle used to unregister the wait operation. The mutex must be Chris@16: // locked when accessing or modifying this member. Chris@16: HANDLE wait_handle_; Chris@16: Chris@16: // The operations waiting on the object handle. If there is a registered Chris@16: // wait then the mutex must be locked when accessing or modifying this Chris@16: // member Chris@16: op_queue op_queue_; Chris@16: Chris@16: // The service instance that owns the object handle implementation. Chris@16: win_object_handle_service* owner_; Chris@16: Chris@16: // Pointers to adjacent handle implementations in linked list. The mutex Chris@16: // must be locked when accessing or modifying these members. Chris@16: implementation_type* next_; Chris@16: implementation_type* prev_; Chris@16: }; Chris@16: Chris@16: // Constructor. Chris@16: BOOST_ASIO_DECL win_object_handle_service( Chris@16: boost::asio::io_service& io_service); Chris@16: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: BOOST_ASIO_DECL void shutdown_service(); Chris@16: Chris@16: // Construct a new handle implementation. Chris@16: BOOST_ASIO_DECL void construct(implementation_type& impl); Chris@16: Chris@16: // Move-construct a new handle implementation. Chris@16: BOOST_ASIO_DECL void move_construct(implementation_type& impl, Chris@16: implementation_type& other_impl); Chris@16: Chris@16: // Move-assign from another handle implementation. Chris@16: BOOST_ASIO_DECL void move_assign(implementation_type& impl, Chris@16: win_object_handle_service& other_service, Chris@16: implementation_type& other_impl); Chris@16: Chris@16: // Destroy a handle implementation. Chris@16: BOOST_ASIO_DECL void destroy(implementation_type& impl); Chris@16: Chris@16: // Assign a native handle to a handle implementation. Chris@16: BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl, Chris@16: const native_handle_type& handle, boost::system::error_code& ec); Chris@16: Chris@16: // Determine whether the handle is open. Chris@16: bool is_open(const implementation_type& impl) const Chris@16: { Chris@16: return impl.handle_ != INVALID_HANDLE_VALUE && impl.handle_ != 0; Chris@16: } Chris@16: Chris@16: // Destroy a handle implementation. Chris@16: BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl, Chris@16: boost::system::error_code& ec); Chris@16: Chris@16: // Get the native handle representation. Chris@16: native_handle_type native_handle(const implementation_type& impl) const Chris@16: { Chris@16: return impl.handle_; Chris@16: } Chris@16: Chris@16: // Cancel all operations associated with the handle. Chris@16: BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl, Chris@16: boost::system::error_code& ec); Chris@16: Chris@16: // Perform a synchronous wait for the object to enter a signalled state. Chris@16: BOOST_ASIO_DECL void wait(implementation_type& impl, Chris@16: boost::system::error_code& ec); Chris@16: Chris@16: /// Start an asynchronous wait. Chris@16: template Chris@16: void async_wait(implementation_type& impl, Handler& handler) Chris@16: { Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef wait_handler op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "object_handle", &impl, "async_wait")); Chris@16: Chris@16: start_wait_op(impl, p.p); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: Chris@16: private: Chris@16: // Helper function to start an asynchronous wait operation. Chris@16: BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, wait_op* op); Chris@16: Chris@16: // Helper function to register a wait operation. Chris@16: BOOST_ASIO_DECL void register_wait_callback( Chris@16: implementation_type& impl, mutex::scoped_lock& lock); Chris@16: Chris@16: // Callback function invoked when the registered wait completes. Chris@16: static BOOST_ASIO_DECL VOID CALLBACK wait_callback( Chris@16: PVOID param, BOOLEAN timeout); Chris@16: Chris@16: // The io_service implementation used to post completions. Chris@16: io_service_impl& io_service_; Chris@16: Chris@16: // Mutex to protect access to internal state. Chris@16: mutex mutex_; Chris@16: Chris@16: // The head of a linked list of all implementations. Chris@16: implementation_type* impl_list_; Chris@16: Chris@16: // Flag to indicate that the dispatcher has been shut down. Chris@16: bool shutdown_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP