2 // detail/impl/strand_service.ipp
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_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/call_stack.hpp>
20 #include <boost/asio/detail/strand_service.hpp>
22 #include <boost/asio/detail/push_options.hpp>
28 struct strand_service::on_do_complete_exit
30 io_service_impl* owner_;
33 ~on_do_complete_exit()
36 impl_->ready_queue_.push(impl_->waiting_queue_);
37 bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
38 impl_->mutex_.unlock();
41 owner_->post_immediate_completion(impl_, true);
45 strand_service::strand_service(boost::asio::io_service& io_service)
46 : boost::asio::detail::service_base<strand_service>(io_service),
47 io_service_(boost::asio::use_service<io_service_impl>(io_service)),
53 void strand_service::shutdown_service()
55 op_queue<operation> ops;
57 boost::asio::detail::mutex::scoped_lock lock(mutex_);
59 for (std::size_t i = 0; i < num_implementations; ++i)
61 if (strand_impl* impl = implementations_[i].get())
63 ops.push(impl->waiting_queue_);
64 ops.push(impl->ready_queue_);
69 void strand_service::construct(strand_service::implementation_type& impl)
71 boost::asio::detail::mutex::scoped_lock lock(mutex_);
73 std::size_t salt = salt_++;
74 #if defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
75 std::size_t index = salt;
76 #else // defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
77 std::size_t index = reinterpret_cast<std::size_t>(&impl);
78 index += (reinterpret_cast<std::size_t>(&impl) >> 3);
79 index ^= salt + 0x9e3779b9 + (index << 6) + (index >> 2);
80 #endif // defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
81 index = index % num_implementations;
83 if (!implementations_[index].get())
84 implementations_[index].reset(new strand_impl);
85 impl = implementations_[index].get();
88 bool strand_service::running_in_this_thread(
89 const implementation_type& impl) const
91 return call_stack<strand_impl>::contains(impl) != 0;
94 bool strand_service::do_dispatch(implementation_type& impl, operation* op)
96 // If we are running inside the io_service, and no other handler already
97 // holds the strand lock, then the handler can run immediately.
98 bool can_dispatch = io_service_.can_dispatch();
100 if (can_dispatch && !impl->locked_)
102 // Immediate invocation is allowed.
103 impl->locked_ = true;
104 impl->mutex_.unlock();
110 // Some other handler already holds the strand lock. Enqueue for later.
111 impl->waiting_queue_.push(op);
112 impl->mutex_.unlock();
116 // The handler is acquiring the strand lock and so is responsible for
117 // scheduling the strand.
118 impl->locked_ = true;
119 impl->mutex_.unlock();
120 impl->ready_queue_.push(op);
121 io_service_.post_immediate_completion(impl, false);
127 void strand_service::do_post(implementation_type& impl,
128 operation* op, bool is_continuation)
133 // Some other handler already holds the strand lock. Enqueue for later.
134 impl->waiting_queue_.push(op);
135 impl->mutex_.unlock();
139 // The handler is acquiring the strand lock and so is responsible for
140 // scheduling the strand.
141 impl->locked_ = true;
142 impl->mutex_.unlock();
143 impl->ready_queue_.push(op);
144 io_service_.post_immediate_completion(impl, is_continuation);
148 void strand_service::do_complete(io_service_impl* owner, operation* base,
149 const boost::system::error_code& ec, std::size_t /*bytes_transferred*/)
153 strand_impl* impl = static_cast<strand_impl*>(base);
155 // Indicate that this strand is executing on the current thread.
156 call_stack<strand_impl>::context ctx(impl);
158 // Ensure the next handler, if any, is scheduled on block exit.
159 on_do_complete_exit on_exit = { owner, impl };
162 // Run all ready handlers. No lock is required since the ready queue is
163 // accessed only within the strand.
164 while (operation* o = impl->ready_queue_.front())
166 impl->ready_queue_.pop();
167 o->complete(*owner, ec, 0);
172 } // namespace detail
176 #include <boost/asio/detail/pop_options.hpp>
178 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP