5 // Copyright (c) 2003-2018 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)),
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);
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>
132 const strand<OtherExecutor>&& other) BOOST_ASIO_NOEXCEPT
134 executor_ = BOOST_ASIO_MOVE_CAST(OtherExecutor)(other);
135 impl_ = BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_);
138 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
145 /// Obtain the underlying executor.
146 inner_executor_type get_inner_executor() const BOOST_ASIO_NOEXCEPT
151 /// Obtain the underlying execution context.
152 execution_context& context() const BOOST_ASIO_NOEXCEPT
154 return executor_.context();
157 /// Inform the strand that it has some outstanding work to do.
159 * The strand delegates this call to its underlying executor.
161 void on_work_started() const BOOST_ASIO_NOEXCEPT
163 executor_.on_work_started();
166 /// Inform the strand that some work is no longer outstanding.
168 * The strand delegates this call to its underlying executor.
170 void on_work_finished() const BOOST_ASIO_NOEXCEPT
172 executor_.on_work_finished();
175 /// Request the strand to invoke the given function object.
177 * This function is used to ask the strand to execute the given function
178 * object on its underlying executor. The function object will be executed
179 * inside this function if the strand is not otherwise busy and if the
180 * underlying executor's @c dispatch() function is also able to execute the
181 * function before returning.
183 * @param f The function object to be called. The executor will make
184 * a copy of the handler object as required. The function signature of the
185 * function object must be: @code void function(); @endcode
187 * @param a An allocator that may be used by the executor to allocate the
188 * internal storage needed for function invocation.
190 template <typename Function, typename Allocator>
191 void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
193 detail::strand_executor_service::dispatch(impl_,
194 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
197 /// Request the strand to invoke the given function object.
199 * This function is used to ask the executor to execute the given function
200 * object. The function object will never be executed inside this function.
201 * Instead, it will be scheduled by the underlying executor's defer function.
203 * @param f The function object to be called. The executor will make
204 * a copy of the handler object as required. The function signature of the
205 * function object must be: @code void function(); @endcode
207 * @param a An allocator that may be used by the executor to allocate the
208 * internal storage needed for function invocation.
210 template <typename Function, typename Allocator>
211 void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
213 detail::strand_executor_service::post(impl_,
214 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
217 /// Request the strand to invoke the given function object.
219 * This function is used to ask the executor to execute the given function
220 * object. The function object will never be executed inside this function.
221 * Instead, it will be scheduled by the underlying executor's defer function.
223 * @param f The function object to be called. The executor will make
224 * a copy of the handler object as required. The function signature of the
225 * function object must be: @code void function(); @endcode
227 * @param a An allocator that may be used by the executor to allocate the
228 * internal storage needed for function invocation.
230 template <typename Function, typename Allocator>
231 void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
233 detail::strand_executor_service::defer(impl_,
234 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
237 /// Determine whether the strand is running in the current thread.
239 * @return @c true if the current thread is executing a function that was
240 * submitted to the strand using post(), dispatch() or defer(). Otherwise
243 bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT
245 return detail::strand_executor_service::running_in_this_thread(impl_);
248 /// Compare two strands for equality.
250 * Two strands are equal if they refer to the same ordered, non-concurrent
253 friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
255 return a.impl_ == b.impl_;
258 /// Compare two strands for inequality.
260 * Two strands are equal if they refer to the same ordered, non-concurrent
263 friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
265 return a.impl_ != b.impl_;
270 typedef detail::strand_executor_service::implementation_type
272 implementation_type impl_;
278 #include <boost/asio/detail/pop_options.hpp>
280 // If both io_context.hpp and strand.hpp have been included, automatically
281 // include the header file needed for the io_context::strand class.
282 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
283 # if defined(BOOST_ASIO_IO_CONTEXT_HPP)
284 # include <boost/asio/io_context_strand.hpp>
285 # endif // defined(BOOST_ASIO_IO_CONTEXT_HPP)
286 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
288 #endif // BOOST_ASIO_STRAND_HPP