2 // detail/impl/strand_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/addressof.hpp>
19 #include <boost/asio/detail/call_stack.hpp>
20 #include <boost/asio/detail/completion_handler.hpp>
21 #include <boost/asio/detail/fenced_block.hpp>
22 #include <boost/asio/detail/handler_alloc_helpers.hpp>
23 #include <boost/asio/detail/handler_invoke_helpers.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 inline strand_service::strand_impl::strand_impl()
32 : operation(&strand_service::do_complete),
37 struct strand_service::on_dispatch_exit
39 io_service_impl* io_service_;
45 impl_->ready_queue_.push(impl_->waiting_queue_);
46 bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
47 impl_->mutex_.unlock();
50 io_service_->post_immediate_completion(impl_, false);
54 template <typename Handler>
55 void strand_service::dispatch(strand_service::implementation_type& impl,
58 // If we are already in the strand then the handler can run immediately.
59 if (call_stack<strand_impl>::contains(impl))
61 fenced_block b(fenced_block::full);
62 boost_asio_handler_invoke_helpers::invoke(handler, handler);
66 // Allocate and construct an operation to wrap the handler.
67 typedef completion_handler<Handler> op;
68 typename op::ptr p = { boost::asio::detail::addressof(handler),
69 boost_asio_handler_alloc_helpers::allocate(
70 sizeof(op), handler), 0 };
71 p.p = new (p.v) op(handler);
73 BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "dispatch"));
75 bool dispatch_immediately = do_dispatch(impl, p.p);
79 if (dispatch_immediately)
81 // Indicate that this strand is executing on the current thread.
82 call_stack<strand_impl>::context ctx(impl);
84 // Ensure the next handler, if any, is scheduled on block exit.
85 on_dispatch_exit on_exit = { &io_service_, impl };
88 completion_handler<Handler>::do_complete(
89 &io_service_, o, boost::system::error_code(), 0);
93 // Request the io_service to invoke the given handler and return immediately.
94 template <typename Handler>
95 void strand_service::post(strand_service::implementation_type& impl,
98 bool is_continuation =
99 boost_asio_handler_cont_helpers::is_continuation(handler);
101 // Allocate and construct an operation to wrap the handler.
102 typedef completion_handler<Handler> op;
103 typename op::ptr p = { boost::asio::detail::addressof(handler),
104 boost_asio_handler_alloc_helpers::allocate(
105 sizeof(op), handler), 0 };
106 p.p = new (p.v) op(handler);
108 BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "post"));
110 do_post(impl, p.p, is_continuation);
114 } // namespace detail
118 #include <boost/asio/detail/pop_options.hpp>
120 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP