PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/detail/impl/strand_service.ipp

http://hadesmem.googlecode.com/
C++ Header | 147 lines | 98 code | 29 blank | 20 comment | 12 complexity | 20777d92954edefe9b86d8b6bec0bee4 MD5 | raw file
  1//
  2// detail/impl/strand_service.ipp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6//
  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)
  9//
 10
 11#ifndef BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP
 12#define BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP
 13
 14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 15# pragma once
 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 17
 18#include <boost/asio/detail/config.hpp>
 19#include <boost/asio/detail/call_stack.hpp>
 20#include <boost/asio/detail/strand_service.hpp>
 21
 22#include <boost/asio/detail/push_options.hpp>
 23
 24namespace boost {
 25namespace asio {
 26namespace detail {
 27
 28struct strand_service::on_do_complete_exit
 29{
 30  io_service_impl* owner_;
 31  strand_impl* impl_;
 32
 33  ~on_do_complete_exit()
 34  {
 35    impl_->mutex_.lock();
 36    bool more_handlers = (--impl_->count_ > 0);
 37    impl_->mutex_.unlock();
 38
 39    if (more_handlers)
 40      owner_->post_immediate_completion(impl_);
 41  }
 42};
 43
 44strand_service::strand_service(boost::asio::io_service& io_service)
 45  : boost::asio::detail::service_base<strand_service>(io_service),
 46    io_service_(boost::asio::use_service<io_service_impl>(io_service)),
 47    mutex_(),
 48    salt_(0)
 49{
 50}
 51
 52void strand_service::shutdown_service()
 53{
 54  op_queue<operation> ops;
 55
 56  boost::asio::detail::mutex::scoped_lock lock(mutex_);
 57
 58  for (std::size_t i = 0; i < num_implementations; ++i)
 59    if (strand_impl* impl = implementations_[i].get())
 60      ops.push(impl->queue_);
 61}
 62
 63void strand_service::construct(strand_service::implementation_type& impl)
 64{
 65  std::size_t salt = salt_++;
 66  std::size_t index = reinterpret_cast<std::size_t>(&impl);
 67  index += (reinterpret_cast<std::size_t>(&impl) >> 3);
 68  index ^= salt + 0x9e3779b9 + (index << 6) + (index >> 2);
 69  index = index % num_implementations;
 70
 71  boost::asio::detail::mutex::scoped_lock lock(mutex_);
 72
 73  if (!implementations_[index].get())
 74    implementations_[index].reset(new strand_impl);
 75  impl = implementations_[index].get();
 76}
 77
 78bool strand_service::do_dispatch(implementation_type& impl, operation* op)
 79{
 80  // If we are running inside the io_service, and no other handler is queued
 81  // or running, then the handler can run immediately.
 82  bool can_dispatch = call_stack<io_service_impl>::contains(&io_service_);
 83  impl->mutex_.lock();
 84  bool first = (++impl->count_ == 1);
 85  if (can_dispatch && first)
 86  {
 87    // Immediate invocation is allowed.
 88    impl->mutex_.unlock();
 89    return true;
 90  }
 91
 92  // Immediate invocation is not allowed, so enqueue for later.
 93  impl->queue_.push(op);
 94  impl->mutex_.unlock();
 95
 96  // The first handler to be enqueued is responsible for scheduling the
 97  // strand.
 98  if (first)
 99    io_service_.post_immediate_completion(impl);
100
101  return false;
102}
103
104void strand_service::do_post(implementation_type& impl, operation* op)
105{
106  // Add the handler to the queue.
107  impl->mutex_.lock();
108  bool first = (++impl->count_ == 1);
109  impl->queue_.push(op);
110  impl->mutex_.unlock();
111
112  // The first handler to be enqueue is responsible for scheduling the strand.
113  if (first)
114    io_service_.post_immediate_completion(impl);
115}
116
117void strand_service::do_complete(io_service_impl* owner, operation* base,
118    boost::system::error_code /*ec*/, std::size_t /*bytes_transferred*/)
119{
120  if (owner)
121  {
122    strand_impl* impl = static_cast<strand_impl*>(base);
123
124    // Get the next handler to be executed.
125    impl->mutex_.lock();
126    operation* o = impl->queue_.front();
127    impl->queue_.pop();
128    impl->mutex_.unlock();
129
130    // Indicate that this strand is executing on the current thread.
131    call_stack<strand_impl>::context ctx(impl);
132
133    // Ensure the next handler, if any, is scheduled on block exit.
134    on_do_complete_exit on_exit = { owner, impl };
135    (void)on_exit;
136
137    o->complete(*owner);
138  }
139}
140
141} // namespace detail
142} // namespace asio
143} // namespace boost
144
145#include <boost/asio/detail/pop_options.hpp>
146
147#endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP