Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:47

0001 //
0002 // detail/win_object_handle_service.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 
0012 #ifndef BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
0013 #define BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
0014 
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0018 
0019 #include <boost/asio/detail/config.hpp>
0020 
0021 #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
0022 
0023 #include <boost/asio/detail/memory.hpp>
0024 #include <boost/asio/detail/wait_handler.hpp>
0025 #include <boost/asio/error.hpp>
0026 #include <boost/asio/execution_context.hpp>
0027 
0028 #if defined(BOOST_ASIO_HAS_IOCP)
0029 # include <boost/asio/detail/win_iocp_io_context.hpp>
0030 #else // defined(BOOST_ASIO_HAS_IOCP)
0031 # include <boost/asio/detail/scheduler.hpp>
0032 #endif // defined(BOOST_ASIO_HAS_IOCP)
0033 
0034 #include <boost/asio/detail/push_options.hpp>
0035 
0036 namespace boost {
0037 namespace asio {
0038 namespace detail {
0039 
0040 class win_object_handle_service :
0041   public execution_context_service_base<win_object_handle_service>
0042 {
0043 public:
0044   // The native type of an object handle.
0045   typedef HANDLE native_handle_type;
0046 
0047   // The implementation type of the object handle.
0048   class implementation_type
0049   {
0050    public:
0051     // Default constructor.
0052     implementation_type()
0053       : handle_(INVALID_HANDLE_VALUE),
0054         wait_handle_(INVALID_HANDLE_VALUE),
0055         owner_(0),
0056         next_(0),
0057         prev_(0)
0058     {
0059     }
0060 
0061   private:
0062     // Only this service will have access to the internal values.
0063     friend class win_object_handle_service;
0064 
0065     // The native object handle representation. May be accessed or modified
0066     // without locking the mutex.
0067     native_handle_type handle_;
0068 
0069     // The handle used to unregister the wait operation. The mutex must be
0070     // locked when accessing or modifying this member.
0071     HANDLE wait_handle_;
0072 
0073     // The operations waiting on the object handle. If there is a registered
0074     // wait then the mutex must be locked when accessing or modifying this
0075     // member
0076     op_queue<wait_op> op_queue_;
0077 
0078     // The service instance that owns the object handle implementation.
0079     win_object_handle_service* owner_;
0080 
0081     // Pointers to adjacent handle implementations in linked list. The mutex
0082     // must be locked when accessing or modifying these members.
0083     implementation_type* next_;
0084     implementation_type* prev_;
0085   };
0086 
0087   // Constructor.
0088   BOOST_ASIO_DECL win_object_handle_service(execution_context& context);
0089 
0090   // Destroy all user-defined handler objects owned by the service.
0091   BOOST_ASIO_DECL void shutdown();
0092 
0093   // Construct a new handle implementation.
0094   BOOST_ASIO_DECL void construct(implementation_type& impl);
0095 
0096   // Move-construct a new handle implementation.
0097   BOOST_ASIO_DECL void move_construct(implementation_type& impl,
0098       implementation_type& other_impl);
0099 
0100   // Move-assign from another handle implementation.
0101   BOOST_ASIO_DECL void move_assign(implementation_type& impl,
0102       win_object_handle_service& other_service,
0103       implementation_type& other_impl);
0104 
0105   // Destroy a handle implementation.
0106   BOOST_ASIO_DECL void destroy(implementation_type& impl);
0107 
0108   // Assign a native handle to a handle implementation.
0109   BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
0110       const native_handle_type& handle, boost::system::error_code& ec);
0111 
0112   // Determine whether the handle is open.
0113   bool is_open(const implementation_type& impl) const
0114   {
0115     return impl.handle_ != INVALID_HANDLE_VALUE && impl.handle_ != 0;
0116   }
0117 
0118   // Destroy a handle implementation.
0119   BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
0120       boost::system::error_code& ec);
0121 
0122   // Get the native handle representation.
0123   native_handle_type native_handle(const implementation_type& impl) const
0124   {
0125     return impl.handle_;
0126   }
0127 
0128   // Cancel all operations associated with the handle.
0129   BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
0130       boost::system::error_code& ec);
0131 
0132   // Perform a synchronous wait for the object to enter a signalled state.
0133   BOOST_ASIO_DECL void wait(implementation_type& impl,
0134       boost::system::error_code& ec);
0135 
0136   /// Start an asynchronous wait.
0137   template <typename Handler, typename IoExecutor>
0138   void async_wait(implementation_type& impl,
0139       Handler& handler, const IoExecutor& io_ex)
0140   {
0141     // Allocate and construct an operation to wrap the handler.
0142     typedef wait_handler<Handler, IoExecutor> op;
0143     typename op::ptr p = { boost::asio::detail::addressof(handler),
0144       op::ptr::allocate(handler), 0 };
0145     p.p = new (p.v) op(handler, io_ex);
0146 
0147     BOOST_ASIO_HANDLER_CREATION((scheduler_.context(), *p.p, "object_handle",
0148           &impl, reinterpret_cast<uintmax_t>(impl.wait_handle_), "async_wait"));
0149 
0150     start_wait_op(impl, p.p);
0151     p.v = p.p = 0;
0152   }
0153 
0154 private:
0155   // Helper function to start an asynchronous wait operation.
0156   BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, wait_op* op);
0157 
0158   // Helper function to register a wait operation.
0159   BOOST_ASIO_DECL void register_wait_callback(
0160       implementation_type& impl, mutex::scoped_lock& lock);
0161 
0162   // Callback function invoked when the registered wait completes.
0163   static BOOST_ASIO_DECL VOID CALLBACK wait_callback(
0164       PVOID param, BOOLEAN timeout);
0165 
0166   // The scheduler used to post completions.
0167 #if defined(BOOST_ASIO_HAS_IOCP)
0168   typedef class win_iocp_io_context scheduler_impl;
0169 #else
0170   typedef class scheduler scheduler_impl;
0171 #endif
0172   scheduler_impl& scheduler_;
0173 
0174   // Mutex to protect access to internal state.
0175   mutex mutex_;
0176 
0177   // The head of a linked list of all implementations.
0178   implementation_type* impl_list_;
0179 
0180   // Flag to indicate that the dispatcher has been shut down.
0181   bool shutdown_;
0182 };
0183 
0184 } // namespace detail
0185 } // namespace asio
0186 } // namespace boost
0187 
0188 #include <boost/asio/detail/pop_options.hpp>
0189 
0190 #if defined(BOOST_ASIO_HEADER_ONLY)
0191 # include <boost/asio/detail/impl/win_object_handle_service.ipp>
0192 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0193 
0194 #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
0195 
0196 #endif // BOOST_ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP