2 // detail/strand_executor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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/detail/type_traits.hpp>
27 #include <boost/asio/execution.hpp>
28 #include <boost/asio/execution_context.hpp>
30 #include <boost/asio/detail/push_options.hpp>
36 // Default service implementation for a strand.
37 class strand_executor_service
38 : public execution_context_service_base<strand_executor_service>
41 // The underlying implementation of a strand.
45 BOOST_ASIO_DECL ~strand_impl();
48 friend class strand_executor_service;
50 // Mutex to protect access to internal data.
53 // Indicates whether the strand is currently "locked" by a handler. This
54 // means that there is a handler upcall in progress, or that the strand
55 // itself has been scheduled in order to invoke some pending handlers.
58 // Indicates that the strand has been shut down and will accept no further
62 // The handlers that are waiting on the strand but should not be run until
63 // after the next time the strand is scheduled. This queue must only be
64 // modified while the mutex is locked.
65 op_queue<scheduler_operation> waiting_queue_;
67 // The handlers that are ready to be run. Logically speaking, these are the
68 // handlers that hold the strand's lock. The ready queue is only modified
69 // from within the strand and so may be accessed without locking the mutex.
70 op_queue<scheduler_operation> ready_queue_;
72 // Pointers to adjacent handle implementations in linked list.
76 // The strand service in where the implementation is held.
77 strand_executor_service* service_;
80 typedef shared_ptr<strand_impl> implementation_type;
82 // Construct a new strand service for the specified context.
83 BOOST_ASIO_DECL explicit strand_executor_service(execution_context& context);
85 // Destroy all user-defined handler objects owned by the service.
86 BOOST_ASIO_DECL void shutdown();
88 // Create a new strand_executor implementation.
89 BOOST_ASIO_DECL implementation_type create_implementation();
91 // Request invocation of the given function.
92 template <typename Executor, typename Function>
93 static void execute(const implementation_type& impl, Executor& ex,
94 BOOST_ASIO_MOVE_ARG(Function) function,
96 can_query<Executor, execution::allocator_t<void> >::value
99 // Request invocation of the given function.
100 template <typename Executor, typename Function>
101 static void execute(const implementation_type& impl, Executor& ex,
102 BOOST_ASIO_MOVE_ARG(Function) function,
104 !can_query<Executor, execution::allocator_t<void> >::value
107 // Request invocation of the given function.
108 template <typename Executor, typename Function, typename Allocator>
109 static void dispatch(const implementation_type& impl, Executor& ex,
110 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
112 // Request invocation of the given function and return immediately.
113 template <typename Executor, typename Function, typename Allocator>
114 static void post(const implementation_type& impl, Executor& ex,
115 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
117 // Request invocation of the given function and return immediately.
118 template <typename Executor, typename Function, typename Allocator>
119 static void defer(const implementation_type& impl, Executor& ex,
120 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
122 // Determine whether the strand is running in the current thread.
123 BOOST_ASIO_DECL static bool running_in_this_thread(
124 const implementation_type& impl);
127 friend class strand_impl;
128 template <typename F, typename Allocator> class allocator_binder;
129 template <typename Executor, typename = void> class invoker;
131 // Adds a function to the strand. Returns true if it acquires the lock.
132 BOOST_ASIO_DECL static bool enqueue(const implementation_type& impl,
133 scheduler_operation* op);
135 // Transfers waiting handlers to the ready queue. Returns true if one or more
136 // handlers were transferred.
137 BOOST_ASIO_DECL static bool push_waiting_to_ready(implementation_type& impl);
139 // Invokes all ready-to-run handlers.
140 BOOST_ASIO_DECL static void run_ready_handlers(implementation_type& impl);
142 // Helper function to request invocation of the given function.
143 template <typename Executor, typename Function, typename Allocator>
144 static void do_execute(const implementation_type& impl, Executor& ex,
145 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
147 // Mutex to protect access to the service-wide state.
150 // Number of mutexes shared between all strand objects.
151 enum { num_mutexes = 193 };
154 scoped_ptr<mutex> mutexes_[num_mutexes];
156 // Extra value used when hashing to prevent recycled memory locations from
157 // getting the same mutex.
160 // The head of a linked list of all implementations.
161 strand_impl* impl_list_;
164 } // namespace detail
168 #include <boost/asio/detail/pop_options.hpp>
170 #include <boost/asio/detail/impl/strand_executor_service.hpp>
171 #if defined(BOOST_ASIO_HEADER_ONLY)
172 # include <boost/asio/detail/impl/strand_executor_service.ipp>
173 #endif // defined(BOOST_ASIO_HEADER_ONLY)
175 #endif // BOOST_ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP