strand_service.ipp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // detail/impl/strand_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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 BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/call_stack.hpp>
  17. #include <boost/asio/detail/strand_service.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. struct strand_service::on_do_complete_exit
  23. {
  24. io_context_impl* owner_;
  25. strand_impl* impl_;
  26. ~on_do_complete_exit()
  27. {
  28. impl_->mutex_.lock();
  29. impl_->ready_queue_.push(impl_->waiting_queue_);
  30. bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
  31. impl_->mutex_.unlock();
  32. if (more_handlers)
  33. owner_->post_immediate_completion(impl_, true);
  34. }
  35. };
  36. strand_service::strand_service(boost::asio::io_context& io_context)
  37. : boost::asio::detail::service_base<strand_service>(io_context),
  38. io_context_(boost::asio::use_service<io_context_impl>(io_context)),
  39. mutex_(),
  40. salt_(0)
  41. {
  42. }
  43. void strand_service::shutdown()
  44. {
  45. op_queue<operation> ops;
  46. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  47. for (std::size_t i = 0; i < num_implementations; ++i)
  48. {
  49. if (strand_impl* impl = implementations_[i].get())
  50. {
  51. ops.push(impl->waiting_queue_);
  52. ops.push(impl->ready_queue_);
  53. }
  54. }
  55. }
  56. void strand_service::construct(strand_service::implementation_type& impl)
  57. {
  58. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  59. std::size_t salt = salt_++;
  60. #if defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  61. std::size_t index = salt;
  62. #else // defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  63. std::size_t index = reinterpret_cast<std::size_t>(&impl);
  64. index += (reinterpret_cast<std::size_t>(&impl) >> 3);
  65. index ^= salt + 0x9e3779b9 + (index << 6) + (index >> 2);
  66. #endif // defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  67. index = index % num_implementations;
  68. if (!implementations_[index].get())
  69. implementations_[index].reset(new strand_impl);
  70. impl = implementations_[index].get();
  71. }
  72. bool strand_service::running_in_this_thread(
  73. const implementation_type& impl) const
  74. {
  75. return call_stack<strand_impl>::contains(impl) != 0;
  76. }
  77. bool strand_service::do_dispatch(implementation_type& impl, operation* op)
  78. {
  79. // If we are running inside the io_context, and no other handler already
  80. // holds the strand lock, then the handler can run immediately.
  81. bool can_dispatch = io_context_.can_dispatch();
  82. impl->mutex_.lock();
  83. if (can_dispatch && !impl->locked_)
  84. {
  85. // Immediate invocation is allowed.
  86. impl->locked_ = true;
  87. impl->mutex_.unlock();
  88. return true;
  89. }
  90. if (impl->locked_)
  91. {
  92. // Some other handler already holds the strand lock. Enqueue for later.
  93. impl->waiting_queue_.push(op);
  94. impl->mutex_.unlock();
  95. }
  96. else
  97. {
  98. // The handler is acquiring the strand lock and so is responsible for
  99. // scheduling the strand.
  100. impl->locked_ = true;
  101. impl->mutex_.unlock();
  102. impl->ready_queue_.push(op);
  103. io_context_.post_immediate_completion(impl, false);
  104. }
  105. return false;
  106. }
  107. void strand_service::do_post(implementation_type& impl,
  108. operation* op, bool is_continuation)
  109. {
  110. impl->mutex_.lock();
  111. if (impl->locked_)
  112. {
  113. // Some other handler already holds the strand lock. Enqueue for later.
  114. impl->waiting_queue_.push(op);
  115. impl->mutex_.unlock();
  116. }
  117. else
  118. {
  119. // The handler is acquiring the strand lock and so is responsible for
  120. // scheduling the strand.
  121. impl->locked_ = true;
  122. impl->mutex_.unlock();
  123. impl->ready_queue_.push(op);
  124. io_context_.post_immediate_completion(impl, is_continuation);
  125. }
  126. }
  127. void strand_service::do_complete(void* owner, operation* base,
  128. const boost::system::error_code& ec, std::size_t /*bytes_transferred*/)
  129. {
  130. if (owner)
  131. {
  132. strand_impl* impl = static_cast<strand_impl*>(base);
  133. // Indicate that this strand is executing on the current thread.
  134. call_stack<strand_impl>::context ctx(impl);
  135. // Ensure the next handler, if any, is scheduled on block exit.
  136. on_do_complete_exit on_exit;
  137. on_exit.owner_ = static_cast<io_context_impl*>(owner);
  138. on_exit.impl_ = impl;
  139. // Run all ready handlers. No lock is required since the ready queue is
  140. // accessed only within the strand.
  141. while (operation* o = impl->ready_queue_.front())
  142. {
  143. impl->ready_queue_.pop();
  144. o->complete(owner, ec, 0);
  145. }
  146. }
  147. }
  148. } // namespace detail
  149. } // namespace asio
  150. } // namespace boost
  151. #include <boost/asio/detail/pop_options.hpp>
  152. #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP