2 // detail/strand_executor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/atomic_count.hpp>
20 #include <boost/asio/detail/executor_op.hpp>
21 #include <boost/asio/detail/memory.hpp>
22 #include <boost/asio/detail/mutex.hpp>
23 #include <boost/asio/detail/op_queue.hpp>
24 #include <boost/asio/detail/scheduler_operation.hpp>
25 #include <boost/asio/detail/scoped_ptr.hpp>
26 #include <boost/asio/execution_context.hpp>
28 #include <boost/asio/detail/push_options.hpp>
34 // Default service implementation for a strand.
35 class strand_executor_service
36 : public execution_context_service_base<strand_executor_service>
39 // The underlying implementation of a strand.
43 BOOST_ASIO_DECL ~strand_impl();
46 friend class strand_executor_service;
48 // Mutex to protect access to internal data.
51 // Indicates whether the strand is currently "locked" by a handler. This
52 // means that there is a handler upcall in progress, or that the strand
53 // itself has been scheduled in order to invoke some pending handlers.
56 // Indicates that the strand has been shut down and will accept no further
60 // The handlers that are waiting on the strand but should not be run until
61 // after the next time the strand is scheduled. This queue must only be
62 // modified while the mutex is locked.
63 op_queue<scheduler_operation> waiting_queue_;
65 // The handlers that are ready to be run. Logically speaking, these are the
66 // handlers that hold the strand's lock. The ready queue is only modified
67 // from within the strand and so may be accessed without locking the mutex.
68 op_queue<scheduler_operation> ready_queue_;
70 // Pointers to adjacent handle implementations in linked list.
74 // The strand service in where the implementation is held.
75 strand_executor_service* service_;
78 typedef shared_ptr<strand_impl> implementation_type;
80 // Construct a new strand service for the specified context.
81 BOOST_ASIO_DECL explicit strand_executor_service(execution_context& context);
83 // Destroy all user-defined handler objects owned by the service.
84 BOOST_ASIO_DECL void shutdown();
86 // Create a new strand_executor implementation.
87 BOOST_ASIO_DECL implementation_type create_implementation();
89 // Request invocation of the given function.
90 template <typename Executor, typename Function, typename Allocator>
91 static void dispatch(const implementation_type& impl, Executor& ex,
92 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
94 // Request invocation of the given function and return immediately.
95 template <typename Executor, typename Function, typename Allocator>
96 static void post(const implementation_type& impl, Executor& ex,
97 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
99 // Request invocation of the given function and return immediately.
100 template <typename Executor, typename Function, typename Allocator>
101 static void defer(const implementation_type& impl, Executor& ex,
102 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
104 // Determine whether the strand is running in the current thread.
105 BOOST_ASIO_DECL static bool running_in_this_thread(
106 const implementation_type& impl);
109 friend class strand_impl;
110 template <typename Executor> class invoker;
112 // Adds a function to the strand. Returns true if it acquires the lock.
113 BOOST_ASIO_DECL static bool enqueue(const implementation_type& impl,
114 scheduler_operation* op);
116 // Mutex to protect access to the service-wide state.
119 // Number of mutexes shared between all strand objects.
120 enum { num_mutexes = 193 };
123 scoped_ptr<mutex> mutexes_[num_mutexes];
125 // Extra value used when hashing to prevent recycled memory locations from
126 // getting the same mutex.
129 // The head of a linked list of all implementations.
130 strand_impl* impl_list_;
133 } // namespace detail
137 #include <boost/asio/detail/pop_options.hpp>
139 #include <boost/asio/detail/impl/strand_executor_service.hpp>
140 #if defined(BOOST_ASIO_HEADER_ONLY)
141 # include <boost/asio/detail/impl/strand_executor_service.ipp>
142 #endif // defined(BOOST_ASIO_HEADER_ONLY)
144 #endif // BOOST_ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP