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_STRAND_HPP
12 #define BOOST_ASIO_STRAND_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/strand_executor_service.hpp>
20 #include <boost/asio/detail/type_traits.hpp>
22 #include <boost/asio/detail/push_options.hpp>
27 /// Provides serialised function invocation for any executor type.
28 template <typename Executor>
32 /// The type of the underlying executor.
33 typedef Executor inner_executor_type;
35 /// Default constructor.
37 * This constructor is only valid if the underlying executor type is default
42 impl_(use_service<detail::strand_executor_service>(
43 executor_.context()).create_implementation())
47 /// Construct a strand for the specified executor.
48 explicit strand(const Executor& e)
50 impl_(use_service<detail::strand_executor_service>(
51 executor_.context()).create_implementation())
56 strand(const strand& other) BOOST_ASIO_NOEXCEPT
57 : executor_(other.executor_),
62 /// Converting constructor.
64 * This constructor is only valid if the @c OtherExecutor type is convertible
67 template <class OtherExecutor>
69 const strand<OtherExecutor>& other) BOOST_ASIO_NOEXCEPT
70 : executor_(other.executor_),
75 /// Assignment operator.
76 strand& operator=(const strand& other) BOOST_ASIO_NOEXCEPT
78 executor_ = other.executor_;
83 /// Converting assignment operator.
85 * This assignment operator is only valid if the @c OtherExecutor type is
86 * convertible to @c Executor.
88 template <class OtherExecutor>
90 const strand<OtherExecutor>& other) BOOST_ASIO_NOEXCEPT
92 executor_ = other.executor_;
97 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
99 strand(strand&& other) BOOST_ASIO_NOEXCEPT
100 : executor_(BOOST_ASIO_MOVE_CAST(Executor)(other.executor_)),
101 impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_))
105 /// Converting move constructor.
107 * This constructor is only valid if the @c OtherExecutor type is convertible
110 template <class OtherExecutor>
111 strand(strand<OtherExecutor>&& other) BOOST_ASIO_NOEXCEPT
112 : executor_(BOOST_ASIO_MOVE_CAST(OtherExecutor)(other.executor_)),
113 impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_))
117 /// Move assignment operator.
118 strand& operator=(strand&& other) BOOST_ASIO_NOEXCEPT
120 executor_ = BOOST_ASIO_MOVE_CAST(Executor)(other.executor_);
121 impl_ = BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_);
125 /// Converting move assignment operator.
127 * This assignment operator is only valid if the @c OtherExecutor type is
128 * convertible to @c Executor.
130 template <class OtherExecutor>
131 strand& operator=(strand<OtherExecutor>&& other) BOOST_ASIO_NOEXCEPT
133 executor_ = BOOST_ASIO_MOVE_CAST(OtherExecutor)(other.executor_);
134 impl_ = BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_);
137 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
144 /// Obtain the underlying executor.
145 inner_executor_type get_inner_executor() const BOOST_ASIO_NOEXCEPT
150 /// Obtain the underlying execution context.
151 execution_context& context() const BOOST_ASIO_NOEXCEPT
153 return executor_.context();
156 /// Inform the strand that it has some outstanding work to do.
158 * The strand delegates this call to its underlying executor.
160 void on_work_started() const BOOST_ASIO_NOEXCEPT
162 executor_.on_work_started();
165 /// Inform the strand that some work is no longer outstanding.
167 * The strand delegates this call to its underlying executor.
169 void on_work_finished() const BOOST_ASIO_NOEXCEPT
171 executor_.on_work_finished();
174 /// Request the strand to invoke the given function object.
176 * This function is used to ask the strand to execute the given function
177 * object on its underlying executor. The function object will be executed
178 * inside this function if the strand is not otherwise busy and if the
179 * underlying executor's @c dispatch() function is also able to execute the
180 * function before returning.
182 * @param f The function object to be called. The executor will make
183 * a copy of the handler object as required. The function signature of the
184 * function object must be: @code void function(); @endcode
186 * @param a An allocator that may be used by the executor to allocate the
187 * internal storage needed for function invocation.
189 template <typename Function, typename Allocator>
190 void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
192 detail::strand_executor_service::dispatch(impl_,
193 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
196 /// Request the strand to invoke the given function object.
198 * This function is used to ask the executor to execute the given function
199 * object. The function object will never be executed inside this function.
200 * Instead, it will be scheduled by the underlying executor's defer function.
202 * @param f The function object to be called. The executor will make
203 * a copy of the handler object as required. The function signature of the
204 * function object must be: @code void function(); @endcode
206 * @param a An allocator that may be used by the executor to allocate the
207 * internal storage needed for function invocation.
209 template <typename Function, typename Allocator>
210 void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
212 detail::strand_executor_service::post(impl_,
213 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
216 /// Request the strand to invoke the given function object.
218 * This function is used to ask the executor to execute the given function
219 * object. The function object will never be executed inside this function.
220 * Instead, it will be scheduled by the underlying executor's defer function.
222 * @param f The function object to be called. The executor will make
223 * a copy of the handler object as required. The function signature of the
224 * function object must be: @code void function(); @endcode
226 * @param a An allocator that may be used by the executor to allocate the
227 * internal storage needed for function invocation.
229 template <typename Function, typename Allocator>
230 void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
232 detail::strand_executor_service::defer(impl_,
233 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
236 /// Determine whether the strand is running in the current thread.
238 * @return @c true if the current thread is executing a function that was
239 * submitted to the strand using post(), dispatch() or defer(). Otherwise
242 bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT
244 return detail::strand_executor_service::running_in_this_thread(impl_);
247 /// Compare two strands for equality.
249 * Two strands are equal if they refer to the same ordered, non-concurrent
252 friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
254 return a.impl_ == b.impl_;
257 /// Compare two strands for inequality.
259 * Two strands are equal if they refer to the same ordered, non-concurrent
262 friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
264 return a.impl_ != b.impl_;
267 #if defined(GENERATING_DOCUMENTATION)
269 #endif // defined(GENERATING_DOCUMENTATION)
271 typedef detail::strand_executor_service::implementation_type
273 implementation_type impl_;
276 /** @defgroup make_strand boost::asio::make_strand
278 * @brief The boost::asio::make_strand function creates a @ref strand object for
279 * an executor or execution context.
283 /// Create a @ref strand object for an executor.
284 template <typename Executor>
285 inline strand<Executor> make_strand(const Executor& ex,
286 typename enable_if<is_executor<Executor>::value>::type* = 0)
288 return strand<Executor>(ex);
291 /// Create a @ref strand object for an execution context.
292 template <typename ExecutionContext>
293 inline strand<typename ExecutionContext::executor_type>
294 make_strand(ExecutionContext& ctx,
296 is_convertible<ExecutionContext&, execution_context&>::value>::type* = 0)
298 return strand<typename ExecutionContext::executor_type>(ctx.get_executor());
306 #include <boost/asio/detail/pop_options.hpp>
308 // If both io_context.hpp and strand.hpp have been included, automatically
309 // include the header file needed for the io_context::strand class.
310 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
311 # if defined(BOOST_ASIO_IO_CONTEXT_HPP)
312 # include <boost/asio/io_context_strand.hpp>
313 # endif // defined(BOOST_ASIO_IO_CONTEXT_HPP)
314 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
316 #endif // BOOST_ASIO_STRAND_HPP