strand_service.ipp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // detail/impl/strand_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_(io_context),
  39. io_context_impl_(boost::asio::use_service<io_context_impl>(io_context)),
  40. mutex_(),
  41. salt_(0)
  42. {
  43. }
  44. void strand_service::shutdown()
  45. {
  46. op_queue<operation> ops;
  47. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  48. for (std::size_t i = 0; i < num_implementations; ++i)
  49. {
  50. if (strand_impl* impl = implementations_[i].get())
  51. {
  52. ops.push(impl->waiting_queue_);
  53. ops.push(impl->ready_queue_);
  54. }
  55. }
  56. }
  57. void strand_service::construct(strand_service::implementation_type& impl)
  58. {
  59. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  60. std::size_t salt = salt_++;
  61. #if defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  62. std::size_t index = salt;
  63. #else // defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  64. std::size_t index = reinterpret_cast<std::size_t>(&impl);
  65. index += (reinterpret_cast<std::size_t>(&impl) >> 3);
  66. index ^= salt + 0x9e3779b9 + (index << 6) + (index >> 2);
  67. #endif // defined(BOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  68. index = index % num_implementations;
  69. if (!implementations_[index].get())
  70. implementations_[index].reset(new strand_impl);
  71. impl = implementations_[index].get();
  72. }
  73. bool strand_service::running_in_this_thread(
  74. const implementation_type& impl) const
  75. {
  76. return call_stack<strand_impl>::contains(impl) != 0;
  77. }
  78. struct strand_service::on_dispatch_exit
  79. {
  80. io_context_impl* io_context_impl_;
  81. strand_impl* impl_;
  82. ~on_dispatch_exit()
  83. {
  84. impl_->mutex_.lock();
  85. impl_->ready_queue_.push(impl_->waiting_queue_);
  86. bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
  87. impl_->mutex_.unlock();
  88. if (more_handlers)
  89. io_context_impl_->post_immediate_completion(impl_, false);
  90. }
  91. };
  92. void strand_service::do_dispatch(implementation_type& impl, operation* op)
  93. {
  94. // If we are running inside the io_context, and no other handler already
  95. // holds the strand lock, then the handler can run immediately.
  96. bool can_dispatch = io_context_impl_.can_dispatch();
  97. impl->mutex_.lock();
  98. if (can_dispatch && !impl->locked_)
  99. {
  100. // Immediate invocation is allowed.
  101. impl->locked_ = true;
  102. impl->mutex_.unlock();
  103. // Indicate that this strand is executing on the current thread.
  104. call_stack<strand_impl>::context ctx(impl);
  105. // Ensure the next handler, if any, is scheduled on block exit.
  106. on_dispatch_exit on_exit = { &io_context_impl_, impl };
  107. (void)on_exit;
  108. op->complete(&io_context_impl_, boost::system::error_code(), 0);
  109. return;
  110. }
  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_impl_.post_immediate_completion(impl, false);
  125. }
  126. }
  127. void strand_service::do_post(implementation_type& impl,
  128. operation* op, bool is_continuation)
  129. {
  130. impl->mutex_.lock();
  131. if (impl->locked_)
  132. {
  133. // Some other handler already holds the strand lock. Enqueue for later.
  134. impl->waiting_queue_.push(op);
  135. impl->mutex_.unlock();
  136. }
  137. else
  138. {
  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_context_impl_.post_immediate_completion(impl, is_continuation);
  145. }
  146. }
  147. void strand_service::do_complete(void* owner, operation* base,
  148. const boost::system::error_code& ec, std::size_t /*bytes_transferred*/)
  149. {
  150. if (owner)
  151. {
  152. strand_impl* impl = static_cast<strand_impl*>(base);
  153. // Indicate that this strand is executing on the current thread.
  154. call_stack<strand_impl>::context ctx(impl);
  155. // Ensure the next handler, if any, is scheduled on block exit.
  156. on_do_complete_exit on_exit;
  157. on_exit.owner_ = static_cast<io_context_impl*>(owner);
  158. on_exit.impl_ = impl;
  159. // Run all ready handlers. No lock is required since the ready queue is
  160. // accessed only within the strand.
  161. while (operation* o = impl->ready_queue_.front())
  162. {
  163. impl->ready_queue_.pop();
  164. o->complete(owner, ec, 0);
  165. }
  166. }
  167. }
  168. } // namespace detail
  169. } // namespace asio
  170. } // namespace boost
  171. #include <boost/asio/detail/pop_options.hpp>
  172. #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP