2 // io_context_strand.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_IO_CONTEXT_STRAND_HPP
12 #define BOOST_ASIO_IO_CONTEXT_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>
20 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
22 #include <boost/asio/async_result.hpp>
23 #include <boost/asio/detail/handler_type_requirements.hpp>
24 #include <boost/asio/detail/strand_service.hpp>
25 #include <boost/asio/detail/wrapped_handler.hpp>
26 #include <boost/asio/io_context.hpp>
28 #include <boost/asio/detail/push_options.hpp>
33 /// Provides serialised handler execution.
35 * The io_context::strand class provides the ability to post and dispatch
36 * handlers with the guarantee that none of those handlers will execute
39 * @par Order of handler invocation
42 * @li a strand object @c s
44 * @li an object @c a meeting completion handler requirements
46 * @li an object @c a1 which is an arbitrary copy of @c a made by the
49 * @li an object @c b meeting completion handler requirements
51 * @li an object @c b1 which is an arbitrary copy of @c b made by the
54 * if any of the following conditions are true:
56 * @li @c s.post(a) happens-before @c s.post(b)
58 * @li @c s.post(a) happens-before @c s.dispatch(b), where the latter is
59 * performed outside the strand
61 * @li @c s.dispatch(a) happens-before @c s.post(b), where the former is
62 * performed outside the strand
64 * @li @c s.dispatch(a) happens-before @c s.dispatch(b), where both are
65 * performed outside the strand
67 * then @c asio_handler_invoke(a1, &a1) happens-before
68 * @c asio_handler_invoke(b1, &b1).
70 * Note that in the following case:
71 * @code async_op_1(..., s.wrap(a));
72 * async_op_2(..., s.wrap(b)); @endcode
73 * the completion of the first async operation will perform @c s.dispatch(a),
74 * and the second will perform @c s.dispatch(b), but the order in which those
75 * are performed is unspecified. That is, you cannot state whether one
76 * happens-before the other. Therefore none of the above conditions are met and
77 * no ordering guarantee is made.
79 * @note The implementation makes no guarantee that handlers posted or
80 * dispatched through different @c strand objects will be invoked concurrently.
83 * @e Distinct @e objects: Safe.@n
84 * @e Shared @e objects: Safe.
89 class io_context::strand
94 * Constructs the strand.
96 * @param io_context The io_context object that the strand will use to
97 * dispatch handlers that are ready to be run.
99 explicit strand(boost::asio::io_context& io_context)
100 : service_(boost::asio::use_service<
101 boost::asio::detail::strand_service>(io_context))
103 service_.construct(impl_);
110 * Handlers posted through the strand that have not yet been invoked will
111 * still be dispatched in a way that meets the guarantee of non-concurrency.
117 /// Obtain the underlying execution context.
118 boost::asio::io_context& context() const BOOST_ASIO_NOEXCEPT
120 return service_.get_io_context();
123 /// Inform the strand that it has some outstanding work to do.
125 * The strand delegates this call to its underlying io_context.
127 void on_work_started() const BOOST_ASIO_NOEXCEPT
129 context().get_executor().on_work_started();
132 /// Inform the strand that some work is no longer outstanding.
134 * The strand delegates this call to its underlying io_context.
136 void on_work_finished() const BOOST_ASIO_NOEXCEPT
138 context().get_executor().on_work_finished();
141 /// Request the strand to invoke the given function object.
143 * This function is used to ask the strand to execute the given function
144 * object on its underlying io_context. The function object will be executed
145 * inside this function if the strand is not otherwise busy and if the
146 * underlying io_context's executor's @c dispatch() function is also able to
147 * execute the function before returning.
149 * @param f The function object to be called. The executor will make
150 * a copy of the handler object as required. The function signature of the
151 * function object must be: @code void function(); @endcode
153 * @param a An allocator that may be used by the executor to allocate the
154 * internal storage needed for function invocation.
156 template <typename Function, typename Allocator>
157 void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
159 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
160 service_.dispatch(impl_, tmp);
164 #if !defined(BOOST_ASIO_NO_DEPRECATED)
165 /// (Deprecated: Use boost::asio::dispatch().) Request the strand to invoke
166 /// the given handler.
168 * This function is used to ask the strand to execute the given handler.
170 * The strand object guarantees that handlers posted or dispatched through
171 * the strand will not be executed concurrently. The handler may be executed
172 * inside this function if the guarantee can be met. If this function is
173 * called from within a handler that was posted or dispatched through the same
174 * strand, then the new handler will be executed immediately.
176 * The strand's guarantee is in addition to the guarantee provided by the
177 * underlying io_context. The io_context guarantees that the handler will only
178 * be called in a thread in which the io_context's run member function is
179 * currently being invoked.
181 * @param handler The handler to be called. The strand will make a copy of the
182 * handler object as required. The function signature of the handler must be:
183 * @code void handler(); @endcode
185 template <typename LegacyCompletionHandler>
186 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
187 dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
189 return async_initiate<LegacyCompletionHandler, void ()>(
190 initiate_dispatch(), handler, this);
192 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
194 /// Request the strand to invoke the given function object.
196 * This function is used to ask the executor to execute the given function
197 * object. The function object will never be executed inside this function.
198 * Instead, it will be scheduled to run by the underlying io_context.
200 * @param f The function object to be called. The executor will make
201 * a copy of the handler object as required. The function signature of the
202 * function object must be: @code void function(); @endcode
204 * @param a An allocator that may be used by the executor to allocate the
205 * internal storage needed for function invocation.
207 template <typename Function, typename Allocator>
208 void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
210 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
211 service_.post(impl_, tmp);
215 #if !defined(BOOST_ASIO_NO_DEPRECATED)
216 /// (Deprecated: Use boost::asio::post().) Request the strand to invoke the
217 /// given handler and return immediately.
219 * This function is used to ask the strand to execute the given handler, but
220 * without allowing the strand to call the handler from inside this function.
222 * The strand object guarantees that handlers posted or dispatched through
223 * the strand will not be executed concurrently. The strand's guarantee is in
224 * addition to the guarantee provided by the underlying io_context. The
225 * io_context guarantees that the handler will only be called in a thread in
226 * which the io_context's run member function is currently being invoked.
228 * @param handler The handler to be called. The strand will make a copy of the
229 * handler object as required. The function signature of the handler must be:
230 * @code void handler(); @endcode
232 template <typename LegacyCompletionHandler>
233 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
234 post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
236 return async_initiate<LegacyCompletionHandler, void ()>(
237 initiate_post(), handler, this);
239 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
241 /// Request the strand to invoke the given function object.
243 * This function is used to ask the executor to execute the given function
244 * object. The function object will never be executed inside this function.
245 * Instead, it will be scheduled to run by the underlying io_context.
247 * @param f The function object to be called. The executor will make
248 * a copy of the handler object as required. The function signature of the
249 * function object must be: @code void function(); @endcode
251 * @param a An allocator that may be used by the executor to allocate the
252 * internal storage needed for function invocation.
254 template <typename Function, typename Allocator>
255 void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
257 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
258 service_.post(impl_, tmp);
262 #if !defined(BOOST_ASIO_NO_DEPRECATED)
263 /// (Deprecated: Use boost::asio::bind_executor().) Create a new handler that
264 /// automatically dispatches the wrapped handler on the strand.
266 * This function is used to create a new handler function object that, when
267 * invoked, will automatically pass the wrapped handler to the strand's
270 * @param handler The handler to be wrapped. The strand will make a copy of
271 * the handler object as required. The function signature of the handler must
272 * be: @code void handler(A1 a1, ... An an); @endcode
274 * @return A function object that, when invoked, passes the wrapped handler to
275 * the strand's dispatch function. Given a function object with the signature:
276 * @code R f(A1 a1, ... An an); @endcode
277 * If this function object is passed to the wrap function like so:
278 * @code strand.wrap(f); @endcode
279 * then the return value is a function object with the signature
280 * @code void g(A1 a1, ... An an); @endcode
281 * that, when invoked, executes code equivalent to:
282 * @code strand.dispatch(boost::bind(f, a1, ... an)); @endcode
284 template <typename Handler>
285 #if defined(GENERATING_DOCUMENTATION)
288 detail::wrapped_handler<strand, Handler, detail::is_continuation_if_running>
290 wrap(Handler handler)
292 return detail::wrapped_handler<io_context::strand, Handler,
293 detail::is_continuation_if_running>(*this, handler);
295 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
297 /// Determine whether the strand is running in the current thread.
299 * @return @c true if the current thread is executing a handler that was
300 * submitted to the strand using post(), dispatch() or wrap(). Otherwise
303 bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT
305 return service_.running_in_this_thread(impl_);
308 /// Compare two strands for equality.
310 * Two strands are equal if they refer to the same ordered, non-concurrent
313 friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
315 return a.impl_ == b.impl_;
318 /// Compare two strands for inequality.
320 * Two strands are equal if they refer to the same ordered, non-concurrent
323 friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
325 return a.impl_ != b.impl_;
329 #if !defined(BOOST_ASIO_NO_DEPRECATED)
330 struct initiate_dispatch
332 template <typename LegacyCompletionHandler>
333 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
336 // If you get an error on the following line it means that your
337 // handler does not meet the documented type requirements for a
338 // LegacyCompletionHandler.
339 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
340 LegacyCompletionHandler, handler) type_check;
342 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
343 self->service_.dispatch(self->impl_, handler2.value);
349 template <typename LegacyCompletionHandler>
350 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
353 // If you get an error on the following line it means that your
354 // handler does not meet the documented type requirements for a
355 // LegacyCompletionHandler.
356 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
357 LegacyCompletionHandler, handler) type_check;
359 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
360 self->service_.post(self->impl_, handler2.value);
363 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
365 boost::asio::detail::strand_service& service_;
366 mutable boost::asio::detail::strand_service::implementation_type impl_;
372 #include <boost/asio/detail/pop_options.hpp>
374 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
376 #endif // BOOST_ASIO_IO_CONTEXT_STRAND_HPP