2 // detail/impl/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_IMPL_STRAND_EXECUTOR_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_IMPL_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/fenced_block.hpp>
19 #include <boost/asio/detail/handler_invoke_helpers.hpp>
20 #include <boost/asio/detail/recycling_allocator.hpp>
21 #include <boost/asio/executor_work_guard.hpp>
22 #include <boost/asio/defer.hpp>
23 #include <boost/asio/dispatch.hpp>
24 #include <boost/asio/post.hpp>
26 #include <boost/asio/detail/push_options.hpp>
32 template <typename F, typename Allocator>
33 class strand_executor_service::allocator_binder
36 typedef Allocator allocator_type;
38 allocator_binder(BOOST_ASIO_MOVE_ARG(F) f, const Allocator& a)
39 : f_(BOOST_ASIO_MOVE_CAST(F)(f)),
44 allocator_binder(const allocator_binder& other)
46 allocator_(other.allocator_)
50 #if defined(BOOST_ASIO_HAS_MOVE)
51 allocator_binder(allocator_binder&& other)
52 : f_(BOOST_ASIO_MOVE_CAST(F)(other.f_)),
53 allocator_(BOOST_ASIO_MOVE_CAST(allocator_type)(other.allocator_))
56 #endif // defined(BOOST_ASIO_HAS_MOVE)
58 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
70 allocator_type allocator_;
73 template <typename Executor>
74 class strand_executor_service::invoker<Executor,
76 execution::is_executor<Executor>::value
80 invoker(const implementation_type& impl, Executor& ex)
82 executor_(boost::asio::prefer(ex, execution::outstanding_work.tracked))
86 invoker(const invoker& other)
88 executor_(other.executor_)
92 #if defined(BOOST_ASIO_HAS_MOVE)
93 invoker(invoker&& other)
94 : impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_)),
95 executor_(BOOST_ASIO_MOVE_CAST(executor_type)(other.executor_))
98 #endif // defined(BOOST_ASIO_HAS_MOVE)
100 struct on_invoker_exit
106 if (push_waiting_to_ready(this_->impl_))
108 recycling_allocator<void> allocator;
109 executor_type ex = this_->executor_;
112 boost::asio::require(
113 BOOST_ASIO_MOVE_CAST(executor_type)(ex),
114 execution::blocking.never),
115 execution::allocator(allocator)),
116 BOOST_ASIO_MOVE_CAST(invoker)(*this_));
123 // Ensure the next handler, if any, is scheduled on block exit.
124 on_invoker_exit on_exit = { this };
127 run_ready_handlers(impl_);
131 typedef typename decay<
132 typename prefer_result<
134 execution::outstanding_work_t::tracked_t
136 >::type executor_type;
138 implementation_type impl_;
139 executor_type executor_;
142 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
144 template <typename Executor>
145 class strand_executor_service::invoker<Executor,
147 !execution::is_executor<Executor>::value
151 invoker(const implementation_type& impl, Executor& ex)
157 invoker(const invoker& other)
158 : impl_(other.impl_),
163 #if defined(BOOST_ASIO_HAS_MOVE)
164 invoker(invoker&& other)
165 : impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_)),
166 work_(BOOST_ASIO_MOVE_CAST(executor_work_guard<Executor>)(other.work_))
169 #endif // defined(BOOST_ASIO_HAS_MOVE)
171 struct on_invoker_exit
177 if (push_waiting_to_ready(this_->impl_))
179 Executor ex(this_->work_.get_executor());
180 recycling_allocator<void> allocator;
181 ex.post(BOOST_ASIO_MOVE_CAST(invoker)(*this_), allocator);
188 // Ensure the next handler, if any, is scheduled on block exit.
189 on_invoker_exit on_exit = { this };
192 run_ready_handlers(impl_);
196 implementation_type impl_;
197 executor_work_guard<Executor> work_;
200 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
202 template <typename Executor, typename Function>
203 inline void strand_executor_service::execute(const implementation_type& impl,
204 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function,
206 can_query<Executor, execution::allocator_t<void> >::value
209 return strand_executor_service::do_execute(impl, ex,
210 BOOST_ASIO_MOVE_CAST(Function)(function),
211 boost::asio::query(ex, execution::allocator));
214 template <typename Executor, typename Function>
215 inline void strand_executor_service::execute(const implementation_type& impl,
216 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function,
218 !can_query<Executor, execution::allocator_t<void> >::value
221 return strand_executor_service::do_execute(impl, ex,
222 BOOST_ASIO_MOVE_CAST(Function)(function),
223 std::allocator<void>());
226 template <typename Executor, typename Function, typename Allocator>
227 void strand_executor_service::do_execute(const implementation_type& impl,
228 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
230 typedef typename decay<Function>::type function_type;
232 // If the executor is not never-blocking, and we are already in the strand,
233 // then the function can run immediately.
234 if (boost::asio::query(ex, execution::blocking) != execution::blocking.never
235 && running_in_this_thread(impl))
237 // Make a local, non-const copy of the function.
238 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(function));
240 fenced_block b(fenced_block::full);
241 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
245 // Allocate and construct an operation to wrap the function.
246 typedef executor_op<function_type, Allocator> op;
247 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
248 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
250 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
251 "strand_executor", impl.get(), 0, "execute"));
253 // Add the function to the strand and schedule the strand if required.
254 bool first = enqueue(impl, p.p);
258 execution::execute(ex, invoker<Executor>(impl, ex));
262 template <typename Executor, typename Function, typename Allocator>
263 void strand_executor_service::dispatch(const implementation_type& impl,
264 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
266 typedef typename decay<Function>::type function_type;
268 // If we are already in the strand then the function can run immediately.
269 if (running_in_this_thread(impl))
271 // Make a local, non-const copy of the function.
272 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(function));
274 fenced_block b(fenced_block::full);
275 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
279 // Allocate and construct an operation to wrap the function.
280 typedef executor_op<function_type, Allocator> op;
281 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
282 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
284 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
285 "strand_executor", impl.get(), 0, "dispatch"));
287 // Add the function to the strand and schedule the strand if required.
288 bool first = enqueue(impl, p.p);
292 boost::asio::dispatch(ex,
293 allocator_binder<invoker<Executor>, Allocator>(
294 invoker<Executor>(impl, ex), a));
298 // Request invocation of the given function and return immediately.
299 template <typename Executor, typename Function, typename Allocator>
300 void strand_executor_service::post(const implementation_type& impl,
301 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
303 typedef typename decay<Function>::type function_type;
305 // Allocate and construct an operation to wrap the function.
306 typedef executor_op<function_type, Allocator> op;
307 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
308 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
310 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
311 "strand_executor", impl.get(), 0, "post"));
313 // Add the function to the strand and schedule the strand if required.
314 bool first = enqueue(impl, p.p);
318 boost::asio::post(ex,
319 allocator_binder<invoker<Executor>, Allocator>(
320 invoker<Executor>(impl, ex), a));
324 // Request invocation of the given function and return immediately.
325 template <typename Executor, typename Function, typename Allocator>
326 void strand_executor_service::defer(const implementation_type& impl,
327 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
329 typedef typename decay<Function>::type function_type;
331 // Allocate and construct an operation to wrap the function.
332 typedef executor_op<function_type, Allocator> op;
333 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
334 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
336 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
337 "strand_executor", impl.get(), 0, "defer"));
339 // Add the function to the strand and schedule the strand if required.
340 bool first = enqueue(impl, p.p);
344 boost::asio::defer(ex,
345 allocator_binder<invoker<Executor>, Allocator>(
346 invoker<Executor>(impl, ex), a));
350 } // namespace detail
354 #include <boost/asio/detail/pop_options.hpp>
356 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_HPP