Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/strand_executor_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_EXECUTOR_SERVICE_HPP
0012 #define BOOST_ASIO_DETAIL_STRAND_EXECUTOR_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/detail/atomic_count.hpp>
0020 #include <boost/asio/detail/executor_op.hpp>
0021 #include <boost/asio/detail/memory.hpp>
0022 #include <boost/asio/detail/mutex.hpp>
0023 #include <boost/asio/detail/op_queue.hpp>
0024 #include <boost/asio/detail/scheduler_operation.hpp>
0025 #include <boost/asio/detail/scoped_ptr.hpp>
0026 #include <boost/asio/detail/type_traits.hpp>
0027 #include <boost/asio/execution.hpp>
0028 #include <boost/asio/execution_context.hpp>
0029 
0030 #include <boost/asio/detail/push_options.hpp>
0031 
0032 namespace boost {
0033 namespace asio {
0034 namespace detail {
0035 
0036 // Default service implementation for a strand.
0037 class strand_executor_service
0038   : public execution_context_service_base<strand_executor_service>
0039 {
0040 public:
0041   // The underlying implementation of a strand.
0042   class strand_impl
0043   {
0044   public:
0045     BOOST_ASIO_DECL ~strand_impl();
0046 
0047   private:
0048     friend class strand_executor_service;
0049 
0050     // Mutex to protect access to internal data.
0051     mutex* mutex_;
0052 
0053     // Indicates whether the strand is currently "locked" by a handler. This
0054     // means that there is a handler upcall in progress, or that the strand
0055     // itself has been scheduled in order to invoke some pending handlers.
0056     bool locked_;
0057 
0058     // Indicates that the strand has been shut down and will accept no further
0059     // handlers.
0060     bool shutdown_;
0061 
0062     // The handlers that are waiting on the strand but should not be run until
0063     // after the next time the strand is scheduled. This queue must only be
0064     // modified while the mutex is locked.
0065     op_queue<scheduler_operation> waiting_queue_;
0066 
0067     // The handlers that are ready to be run. Logically speaking, these are the
0068     // handlers that hold the strand's lock. The ready queue is only modified
0069     // from within the strand and so may be accessed without locking the mutex.
0070     op_queue<scheduler_operation> ready_queue_;
0071 
0072     // Pointers to adjacent handle implementations in linked list.
0073     strand_impl* next_;
0074     strand_impl* prev_;
0075 
0076     // The strand service in where the implementation is held.
0077     strand_executor_service* service_;
0078   };
0079 
0080   typedef shared_ptr<strand_impl> implementation_type;
0081 
0082   // Construct a new strand service for the specified context.
0083   BOOST_ASIO_DECL explicit strand_executor_service(execution_context& context);
0084 
0085   // Destroy all user-defined handler objects owned by the service.
0086   BOOST_ASIO_DECL void shutdown();
0087 
0088   // Create a new strand_executor implementation.
0089   BOOST_ASIO_DECL implementation_type create_implementation();
0090 
0091   // Request invocation of the given function.
0092   template <typename Executor, typename Function>
0093   static void execute(const implementation_type& impl, Executor& ex,
0094       Function&& function,
0095       enable_if_t<
0096         can_query<Executor, execution::allocator_t<void>>::value
0097       >* = 0);
0098 
0099   // Request invocation of the given function.
0100   template <typename Executor, typename Function>
0101   static void execute(const implementation_type& impl, Executor& ex,
0102       Function&& function,
0103       enable_if_t<
0104         !can_query<Executor, execution::allocator_t<void>>::value
0105       >* = 0);
0106 
0107   // Request invocation of the given function.
0108   template <typename Executor, typename Function, typename Allocator>
0109   static void dispatch(const implementation_type& impl, Executor& ex,
0110       Function&& function, const Allocator& a);
0111 
0112   // Request invocation of the given function and return immediately.
0113   template <typename Executor, typename Function, typename Allocator>
0114   static void post(const implementation_type& impl, Executor& ex,
0115       Function&& function, const Allocator& a);
0116 
0117   // Request invocation of the given function and return immediately.
0118   template <typename Executor, typename Function, typename Allocator>
0119   static void defer(const implementation_type& impl, Executor& ex,
0120       Function&& function, const Allocator& a);
0121 
0122   // Determine whether the strand is running in the current thread.
0123   BOOST_ASIO_DECL static bool running_in_this_thread(
0124       const implementation_type& impl);
0125 
0126 private:
0127   friend class strand_impl;
0128   template <typename F, typename Allocator> class allocator_binder;
0129   template <typename Executor, typename = void> class invoker;
0130 
0131   // Adds a function to the strand. Returns true if it acquires the lock.
0132   BOOST_ASIO_DECL static bool enqueue(const implementation_type& impl,
0133       scheduler_operation* op);
0134 
0135   // Transfers waiting handlers to the ready queue. Returns true if one or more
0136   // handlers were transferred.
0137   BOOST_ASIO_DECL static bool push_waiting_to_ready(implementation_type& impl);
0138 
0139   // Invokes all ready-to-run handlers.
0140   BOOST_ASIO_DECL static void run_ready_handlers(implementation_type& impl);
0141 
0142   // Helper function to request invocation of the given function.
0143   template <typename Executor, typename Function, typename Allocator>
0144   static void do_execute(const implementation_type& impl, Executor& ex,
0145       Function&& function, const Allocator& a);
0146 
0147   // Mutex to protect access to the service-wide state.
0148   mutex mutex_;
0149 
0150   // Number of mutexes shared between all strand objects.
0151   enum { num_mutexes = 193 };
0152 
0153   // Pool of mutexes.
0154   scoped_ptr<mutex> mutexes_[num_mutexes];
0155 
0156   // Extra value used when hashing to prevent recycled memory locations from
0157   // getting the same mutex.
0158   std::size_t salt_;
0159 
0160   // The head of a linked list of all implementations.
0161   strand_impl* impl_list_;
0162 };
0163 
0164 } // namespace detail
0165 } // namespace asio
0166 } // namespace boost
0167 
0168 #include <boost/asio/detail/pop_options.hpp>
0169 
0170 #include <boost/asio/detail/impl/strand_executor_service.hpp>
0171 #if defined(BOOST_ASIO_HEADER_ONLY)
0172 # include <boost/asio/detail/impl/strand_executor_service.ipp>
0173 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0174 
0175 #endif // BOOST_ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP