2 // detail/impl/strand_executor_service.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_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/call_stack.hpp>
19 #include <boost/asio/detail/fenced_block.hpp>
20 #include <boost/asio/detail/handler_invoke_helpers.hpp>
21 #include <boost/asio/detail/recycling_allocator.hpp>
22 #include <boost/asio/executor_work_guard.hpp>
23 #include <boost/asio/defer.hpp>
24 #include <boost/asio/dispatch.hpp>
25 #include <boost/asio/post.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 template <typename F, typename Allocator>
34 class strand_executor_service::allocator_binder
37 typedef Allocator allocator_type;
39 allocator_binder(BOOST_ASIO_MOVE_ARG(F) f, const Allocator& a)
40 : f_(BOOST_ASIO_MOVE_CAST(F)(f)),
45 allocator_binder(const allocator_binder& other)
47 allocator_(other.allocator_)
51 #if defined(BOOST_ASIO_HAS_MOVE)
52 allocator_binder(allocator_binder&& other)
53 : f_(BOOST_ASIO_MOVE_CAST(F)(other.f_)),
54 allocator_(BOOST_ASIO_MOVE_CAST(allocator_type)(other.allocator_))
57 #endif // defined(BOOST_ASIO_HAS_MOVE)
59 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
71 allocator_type allocator_;
74 template <typename Executor>
75 class strand_executor_service::invoker<Executor,
77 execution::is_executor<Executor>::value
81 invoker(const implementation_type& impl, Executor& ex)
83 executor_(boost::asio::prefer(ex, execution::outstanding_work.tracked))
87 invoker(const invoker& other)
89 executor_(other.executor_)
93 #if defined(BOOST_ASIO_HAS_MOVE)
94 invoker(invoker&& other)
95 : impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_)),
96 executor_(BOOST_ASIO_MOVE_CAST(executor_type)(other.executor_))
99 #endif // defined(BOOST_ASIO_HAS_MOVE)
101 struct on_invoker_exit
107 this_->impl_->mutex_->lock();
108 this_->impl_->ready_queue_.push(this_->impl_->waiting_queue_);
109 bool more_handlers = this_->impl_->locked_ =
110 !this_->impl_->ready_queue_.empty();
111 this_->impl_->mutex_->unlock();
115 recycling_allocator<void> allocator;
118 boost::asio::require(this_->executor_,
119 execution::blocking.never),
120 execution::allocator(allocator)),
121 BOOST_ASIO_MOVE_CAST(invoker)(*this_));
128 // Indicate that this strand is executing on the current thread.
129 call_stack<strand_impl>::context ctx(impl_.get());
131 // Ensure the next handler, if any, is scheduled on block exit.
132 on_invoker_exit on_exit = { this };
135 // Run all ready handlers. No lock is required since the ready queue is
136 // accessed only within the strand.
137 boost::system::error_code ec;
138 while (scheduler_operation* o = impl_->ready_queue_.front())
140 impl_->ready_queue_.pop();
141 o->complete(impl_.get(), ec, 0);
146 typedef typename decay<
147 typename prefer_result<
149 execution::outstanding_work_t::tracked_t
151 >::type executor_type;
153 implementation_type impl_;
154 executor_type executor_;
157 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
159 template <typename Executor>
160 class strand_executor_service::invoker<Executor,
162 !execution::is_executor<Executor>::value
166 invoker(const implementation_type& impl, Executor& ex)
172 invoker(const invoker& other)
173 : impl_(other.impl_),
178 #if defined(BOOST_ASIO_HAS_MOVE)
179 invoker(invoker&& other)
180 : impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_)),
181 work_(BOOST_ASIO_MOVE_CAST(executor_work_guard<Executor>)(other.work_))
184 #endif // defined(BOOST_ASIO_HAS_MOVE)
186 struct on_invoker_exit
192 this_->impl_->mutex_->lock();
193 this_->impl_->ready_queue_.push(this_->impl_->waiting_queue_);
194 bool more_handlers = this_->impl_->locked_ =
195 !this_->impl_->ready_queue_.empty();
196 this_->impl_->mutex_->unlock();
200 Executor ex(this_->work_.get_executor());
201 recycling_allocator<void> allocator;
202 ex.post(BOOST_ASIO_MOVE_CAST(invoker)(*this_), allocator);
209 // Indicate that this strand is executing on the current thread.
210 call_stack<strand_impl>::context ctx(impl_.get());
212 // Ensure the next handler, if any, is scheduled on block exit.
213 on_invoker_exit on_exit = { this };
216 // Run all ready handlers. No lock is required since the ready queue is
217 // accessed only within the strand.
218 boost::system::error_code ec;
219 while (scheduler_operation* o = impl_->ready_queue_.front())
221 impl_->ready_queue_.pop();
222 o->complete(impl_.get(), ec, 0);
227 implementation_type impl_;
228 executor_work_guard<Executor> work_;
231 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
233 template <typename Executor, typename Function>
234 inline void strand_executor_service::execute(const implementation_type& impl,
235 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function,
237 can_query<Executor, execution::allocator_t<void> >::value
240 return strand_executor_service::do_execute(impl, ex,
241 BOOST_ASIO_MOVE_CAST(Function)(function),
242 boost::asio::query(ex, execution::allocator));
245 template <typename Executor, typename Function>
246 inline void strand_executor_service::execute(const implementation_type& impl,
247 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function,
249 !can_query<Executor, execution::allocator_t<void> >::value
252 return strand_executor_service::do_execute(impl, ex,
253 BOOST_ASIO_MOVE_CAST(Function)(function),
254 std::allocator<void>());
257 template <typename Executor, typename Function, typename Allocator>
258 void strand_executor_service::do_execute(const implementation_type& impl,
259 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
261 typedef typename decay<Function>::type function_type;
263 // If the executor is not never-blocking, and we are already in the strand,
264 // then the function can run immediately.
265 if (boost::asio::query(ex, execution::blocking) != execution::blocking.never
266 && call_stack<strand_impl>::contains(impl.get()))
268 // Make a local, non-const copy of the function.
269 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(function));
271 fenced_block b(fenced_block::full);
272 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
276 // Allocate and construct an operation to wrap the function.
277 typedef executor_op<function_type, Allocator> op;
278 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
279 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
281 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
282 "strand_executor", impl.get(), 0, "execute"));
284 // Add the function to the strand and schedule the strand if required.
285 bool first = enqueue(impl, p.p);
289 execution::execute(ex, invoker<Executor>(impl, ex));
293 template <typename Executor, typename Function, typename Allocator>
294 void strand_executor_service::dispatch(const implementation_type& impl,
295 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
297 typedef typename decay<Function>::type function_type;
299 // If we are already in the strand then the function can run immediately.
300 if (call_stack<strand_impl>::contains(impl.get()))
302 // Make a local, non-const copy of the function.
303 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(function));
305 fenced_block b(fenced_block::full);
306 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
310 // Allocate and construct an operation to wrap the function.
311 typedef executor_op<function_type, Allocator> op;
312 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
313 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
315 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
316 "strand_executor", impl.get(), 0, "dispatch"));
318 // Add the function to the strand and schedule the strand if required.
319 bool first = enqueue(impl, p.p);
323 boost::asio::dispatch(ex,
324 allocator_binder<invoker<Executor>, Allocator>(
325 invoker<Executor>(impl, ex), a));
329 // Request invocation of the given function and return immediately.
330 template <typename Executor, typename Function, typename Allocator>
331 void strand_executor_service::post(const implementation_type& impl,
332 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
334 typedef typename decay<Function>::type function_type;
336 // Allocate and construct an operation to wrap the function.
337 typedef executor_op<function_type, Allocator> op;
338 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
339 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
341 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
342 "strand_executor", impl.get(), 0, "post"));
344 // Add the function to the strand and schedule the strand if required.
345 bool first = enqueue(impl, p.p);
349 boost::asio::post(ex,
350 allocator_binder<invoker<Executor>, Allocator>(
351 invoker<Executor>(impl, ex), a));
355 // Request invocation of the given function and return immediately.
356 template <typename Executor, typename Function, typename Allocator>
357 void strand_executor_service::defer(const implementation_type& impl,
358 Executor& ex, BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a)
360 typedef typename decay<Function>::type function_type;
362 // Allocate and construct an operation to wrap the function.
363 typedef executor_op<function_type, Allocator> op;
364 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
365 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(function), a);
367 BOOST_ASIO_HANDLER_CREATION((impl->service_->context(), *p.p,
368 "strand_executor", impl.get(), 0, "defer"));
370 // Add the function to the strand and schedule the strand if required.
371 bool first = enqueue(impl, p.p);
375 boost::asio::defer(ex,
376 allocator_binder<invoker<Executor>, Allocator>(
377 invoker<Executor>(impl, ex), a));
381 } // namespace detail
385 #include <boost/asio/detail/pop_options.hpp>
387 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_HPP