Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/strand_service.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP
0012 #define BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/io_context.hpp>
0020 #include <boost/asio/detail/mutex.hpp>
0021 #include <boost/asio/detail/op_queue.hpp>
0022 #include <boost/asio/detail/operation.hpp>
0023 #include <boost/asio/detail/scoped_ptr.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 // Default service implementation for a strand.
0032 class strand_service
0033   : public boost::asio::detail::service_base<strand_service>
0034 {
0035 private:
0036   // Helper class to re-post the strand on exit.
0037   struct on_do_complete_exit;
0038 
0039   // Helper class to re-post the strand on exit.
0040   struct on_dispatch_exit;
0041 
0042 public:
0043 
0044   // The underlying implementation of a strand.
0045   class strand_impl
0046     : public operation
0047   {
0048   public:
0049     strand_impl();
0050 
0051   private:
0052     // Only this service will have access to the internal values.
0053     friend class strand_service;
0054     friend struct on_do_complete_exit;
0055     friend struct on_dispatch_exit;
0056 
0057     // Mutex to protect access to internal data.
0058     boost::asio::detail::mutex mutex_;
0059 
0060     // Indicates whether the strand is currently "locked" by a handler. This
0061     // means that there is a handler upcall in progress, or that the strand
0062     // itself has been scheduled in order to invoke some pending handlers.
0063     bool locked_;
0064 
0065     // The handlers that are waiting on the strand but should not be run until
0066     // after the next time the strand is scheduled. This queue must only be
0067     // modified while the mutex is locked.
0068     op_queue<operation> waiting_queue_;
0069 
0070     // The handlers that are ready to be run. Logically speaking, these are the
0071     // handlers that hold the strand's lock. The ready queue is only modified
0072     // from within the strand and so may be accessed without locking the mutex.
0073     op_queue<operation> ready_queue_;
0074   };
0075 
0076   typedef strand_impl* implementation_type;
0077 
0078   // Construct a new strand service for the specified io_context.
0079   BOOST_ASIO_DECL explicit strand_service(boost::asio::io_context& io_context);
0080 
0081   // Destroy all user-defined handler objects owned by the service.
0082   BOOST_ASIO_DECL void shutdown();
0083 
0084   // Construct a new strand implementation.
0085   BOOST_ASIO_DECL void construct(implementation_type& impl);
0086 
0087   // Request the io_context to invoke the given handler.
0088   template <typename Handler>
0089   void dispatch(implementation_type& impl, Handler& handler);
0090 
0091   // Request the io_context to invoke the given handler and return immediately.
0092   template <typename Handler>
0093   void post(implementation_type& impl, Handler& handler);
0094 
0095   // Determine whether the strand is running in the current thread.
0096   BOOST_ASIO_DECL bool running_in_this_thread(
0097       const implementation_type& impl) const;
0098 
0099 private:
0100   // Helper function to dispatch a handler.
0101   BOOST_ASIO_DECL void do_dispatch(implementation_type& impl, operation* op);
0102 
0103   // Helper function to post a handler.
0104   BOOST_ASIO_DECL void do_post(implementation_type& impl,
0105       operation* op, bool is_continuation);
0106 
0107   BOOST_ASIO_DECL static void do_complete(void* owner,
0108       operation* base, const boost::system::error_code& ec,
0109       std::size_t bytes_transferred);
0110 
0111   // The io_context used to obtain an I/O executor.
0112   io_context& io_context_;
0113 
0114   // The io_context implementation used to post completions.
0115   io_context_impl& io_context_impl_;
0116 
0117   // Mutex to protect access to the array of implementations.
0118   boost::asio::detail::mutex mutex_;
0119 
0120   // Number of implementations shared between all strand objects.
0121 #if defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
0122   enum { num_implementations = BOOST_ASIO_STRAND_IMPLEMENTATIONS };
0123 #else // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
0124   enum { num_implementations = 193 };
0125 #endif // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
0126 
0127   // Pool of implementations.
0128   scoped_ptr<strand_impl> implementations_[num_implementations];
0129 
0130   // Extra value used when hashing to prevent recycled memory locations from
0131   // getting the same strand implementation.
0132   std::size_t salt_;
0133 };
0134 
0135 } // namespace detail
0136 } // namespace asio
0137 } // namespace boost
0138 
0139 #include <boost/asio/detail/pop_options.hpp>
0140 
0141 #include <boost/asio/detail/impl/strand_service.hpp>
0142 #if defined(BOOST_ASIO_HEADER_ONLY)
0143 # include <boost/asio/detail/impl/strand_service.ipp>
0144 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0145 
0146 #endif // BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP