strand_service.ipp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP
  11. #define ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/detail/call_stack.hpp"
  17. #include "asio/detail/strand_service.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace detail {
  21. struct strand_service::on_do_complete_exit
  22. {
  23. io_service_impl* owner_;
  24. strand_impl* impl_;
  25. ~on_do_complete_exit()
  26. {
  27. impl_->mutex_.lock();
  28. bool more_handlers = (--impl_->count_ > 0);
  29. impl_->mutex_.unlock();
  30. if (more_handlers)
  31. owner_->post_immediate_completion(impl_);
  32. }
  33. };
  34. strand_service::strand_service(asio::io_service& io_service)
  35. : asio::detail::service_base<strand_service>(io_service),
  36. io_service_(asio::use_service<io_service_impl>(io_service)),
  37. mutex_(),
  38. salt_(0)
  39. {
  40. }
  41. void strand_service::shutdown_service()
  42. {
  43. op_queue<operation> ops;
  44. asio::detail::mutex::scoped_lock lock(mutex_);
  45. for (std::size_t i = 0; i < num_implementations; ++i)
  46. if (strand_impl* impl = implementations_[i].get())
  47. ops.push(impl->queue_);
  48. }
  49. void strand_service::construct(strand_service::implementation_type& impl)
  50. {
  51. std::size_t salt = salt_++;
  52. std::size_t index = reinterpret_cast<std::size_t>(&impl);
  53. index += (reinterpret_cast<std::size_t>(&impl) >> 3);
  54. index ^= salt + 0x9e3779b9 + (index << 6) + (index >> 2);
  55. index = index % num_implementations;
  56. asio::detail::mutex::scoped_lock lock(mutex_);
  57. if (!implementations_[index])
  58. implementations_[index].reset(new strand_impl);
  59. impl = implementations_[index].get();
  60. }
  61. void strand_service::do_complete(io_service_impl* owner, operation* base,
  62. asio::error_code /*ec*/, std::size_t /*bytes_transferred*/)
  63. {
  64. if (owner)
  65. {
  66. strand_impl* impl = static_cast<strand_impl*>(base);
  67. // Get the next handler to be executed.
  68. impl->mutex_.lock();
  69. operation* o = impl->queue_.front();
  70. impl->queue_.pop();
  71. impl->mutex_.unlock();
  72. // Indicate that this strand is executing on the current thread.
  73. call_stack<strand_impl>::context ctx(impl);
  74. // Ensure the next handler, if any, is scheduled on block exit.
  75. on_do_complete_exit on_exit = { owner, impl };
  76. (void)on_exit;
  77. o->complete(*owner);
  78. }
  79. }
  80. } // namespace detail
  81. } // namespace asio
  82. #include "asio/detail/pop_options.hpp"
  83. #endif // ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP