2 // io_context_strand.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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 #if !defined(BOOST_ASIO_NO_DEPRECATED)
118 /// (Deprecated: Use context().) Get the io_context associated with the
121 * This function may be used to obtain the io_context object that the strand
122 * uses to dispatch handlers for asynchronous operations.
124 * @return A reference to the io_context object that the strand will use to
125 * dispatch handlers. Ownership is not transferred to the caller.
127 boost::asio::io_context& get_io_context()
129 return service_.get_io_context();
132 /// (Deprecated: Use context().) Get the io_context associated with the
135 * This function may be used to obtain the io_context object that the strand
136 * uses to dispatch handlers for asynchronous operations.
138 * @return A reference to the io_context object that the strand will use to
139 * dispatch handlers. Ownership is not transferred to the caller.
141 boost::asio::io_context& get_io_service()
143 return service_.get_io_context();
145 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
147 /// Obtain the underlying execution context.
148 boost::asio::io_context& context() const BOOST_ASIO_NOEXCEPT
150 return service_.get_io_context();
153 /// Inform the strand that it has some outstanding work to do.
155 * The strand delegates this call to its underlying io_context.
157 void on_work_started() const BOOST_ASIO_NOEXCEPT
159 context().get_executor().on_work_started();
162 /// Inform the strand that some work is no longer outstanding.
164 * The strand delegates this call to its underlying io_context.
166 void on_work_finished() const BOOST_ASIO_NOEXCEPT
168 context().get_executor().on_work_finished();
171 /// Request the strand to invoke the given function object.
173 * This function is used to ask the strand to execute the given function
174 * object on its underlying io_context. The function object will be executed
175 * inside this function if the strand is not otherwise busy and if the
176 * underlying io_context's executor's @c dispatch() function is also able to
177 * execute the function before returning.
179 * @param f The function object to be called. The executor will make
180 * a copy of the handler object as required. The function signature of the
181 * function object must be: @code void function(); @endcode
183 * @param a An allocator that may be used by the executor to allocate the
184 * internal storage needed for function invocation.
186 template <typename Function, typename Allocator>
187 void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
189 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
190 service_.dispatch(impl_, tmp);
194 #if !defined(BOOST_ASIO_NO_DEPRECATED)
195 /// (Deprecated: Use boost::asio::dispatch().) Request the strand to invoke
196 /// the given handler.
198 * This function is used to ask the strand to execute the given handler.
200 * The strand object guarantees that handlers posted or dispatched through
201 * the strand will not be executed concurrently. The handler may be executed
202 * inside this function if the guarantee can be met. If this function is
203 * called from within a handler that was posted or dispatched through the same
204 * strand, then the new handler will be executed immediately.
206 * The strand's guarantee is in addition to the guarantee provided by the
207 * underlying io_context. The io_context guarantees that the handler will only
208 * be called in a thread in which the io_context's run member function is
209 * currently being invoked.
211 * @param handler The handler to be called. The strand will make a copy of the
212 * handler object as required. The function signature of the handler must be:
213 * @code void handler(); @endcode
215 template <typename CompletionHandler>
216 BOOST_ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
217 dispatch(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler)
219 // If you get an error on the following line it means that your handler does
220 // not meet the documented type requirements for a CompletionHandler.
221 BOOST_ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
223 async_completion<CompletionHandler, void ()> init(handler);
225 service_.dispatch(impl_, init.completion_handler);
227 return init.result.get();
229 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
231 /// Request the strand to invoke the given function object.
233 * This function is used to ask the executor to execute the given function
234 * object. The function object will never be executed inside this function.
235 * Instead, it will be scheduled to run by the underlying io_context.
237 * @param f The function object to be called. The executor will make
238 * a copy of the handler object as required. The function signature of the
239 * function object must be: @code void function(); @endcode
241 * @param a An allocator that may be used by the executor to allocate the
242 * internal storage needed for function invocation.
244 template <typename Function, typename Allocator>
245 void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
247 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
248 service_.post(impl_, tmp);
252 #if !defined(BOOST_ASIO_NO_DEPRECATED)
253 /// (Deprecated: Use boost::asio::post().) Request the strand to invoke the
254 /// given handler and return immediately.
256 * This function is used to ask the strand to execute the given handler, but
257 * without allowing the strand to call the handler from inside this function.
259 * The strand object guarantees that handlers posted or dispatched through
260 * the strand will not be executed concurrently. The strand's guarantee is in
261 * addition to the guarantee provided by the underlying io_context. The
262 * io_context guarantees that the handler will only be called in a thread in
263 * which the io_context's run member function is currently being invoked.
265 * @param handler The handler to be called. The strand will make a copy of the
266 * handler object as required. The function signature of the handler must be:
267 * @code void handler(); @endcode
269 template <typename CompletionHandler>
270 BOOST_ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
271 post(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler)
273 // If you get an error on the following line it means that your handler does
274 // not meet the documented type requirements for a CompletionHandler.
275 BOOST_ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
277 async_completion<CompletionHandler, void ()> init(handler);
279 service_.post(impl_, init.completion_handler);
281 return init.result.get();
283 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
285 /// Request the strand to invoke the given function object.
287 * This function is used to ask the executor to execute the given function
288 * object. The function object will never be executed inside this function.
289 * Instead, it will be scheduled to run by the underlying io_context.
291 * @param f The function object to be called. The executor will make
292 * a copy of the handler object as required. The function signature of the
293 * function object must be: @code void function(); @endcode
295 * @param a An allocator that may be used by the executor to allocate the
296 * internal storage needed for function invocation.
298 template <typename Function, typename Allocator>
299 void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
301 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
302 service_.post(impl_, tmp);
306 #if !defined(BOOST_ASIO_NO_DEPRECATED)
307 /// (Deprecated: Use boost::asio::bind_executor().) Create a new handler that
308 /// automatically dispatches the wrapped handler on the strand.
310 * This function is used to create a new handler function object that, when
311 * invoked, will automatically pass the wrapped handler to the strand's
314 * @param handler The handler to be wrapped. The strand will make a copy of
315 * the handler object as required. The function signature of the handler must
316 * be: @code void handler(A1 a1, ... An an); @endcode
318 * @return A function object that, when invoked, passes the wrapped handler to
319 * the strand's dispatch function. Given a function object with the signature:
320 * @code R f(A1 a1, ... An an); @endcode
321 * If this function object is passed to the wrap function like so:
322 * @code strand.wrap(f); @endcode
323 * then the return value is a function object with the signature
324 * @code void g(A1 a1, ... An an); @endcode
325 * that, when invoked, executes code equivalent to:
326 * @code strand.dispatch(boost::bind(f, a1, ... an)); @endcode
328 template <typename Handler>
329 #if defined(GENERATING_DOCUMENTATION)
332 detail::wrapped_handler<strand, Handler, detail::is_continuation_if_running>
334 wrap(Handler handler)
336 return detail::wrapped_handler<io_context::strand, Handler,
337 detail::is_continuation_if_running>(*this, handler);
339 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
341 /// Determine whether the strand is running in the current thread.
343 * @return @c true if the current thread is executing a handler that was
344 * submitted to the strand using post(), dispatch() or wrap(). Otherwise
347 bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT
349 return service_.running_in_this_thread(impl_);
352 /// Compare two strands for equality.
354 * Two strands are equal if they refer to the same ordered, non-concurrent
357 friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
359 return a.impl_ == b.impl_;
362 /// Compare two strands for inequality.
364 * Two strands are equal if they refer to the same ordered, non-concurrent
367 friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
369 return a.impl_ != b.impl_;
373 boost::asio::detail::strand_service& service_;
374 mutable boost::asio::detail::strand_service::implementation_type impl_;
380 #include <boost/asio/detail/pop_options.hpp>
382 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
384 #endif // BOOST_ASIO_IO_CONTEXT_STRAND_HPP