strand_service.ipp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // detail/impl/strand_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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])
  70. {
  71. execution_context::allocator<void> alloc(context());
  72. implementations_[index] = allocate_shared<strand_impl>(alloc);
  73. }
  74. impl = implementations_[index].get();
  75. }
  76. bool strand_service::running_in_this_thread(
  77. const implementation_type& impl) const
  78. {
  79. return call_stack<strand_impl>::contains(impl) != 0;
  80. }
  81. struct strand_service::on_dispatch_exit
  82. {
  83. io_context_impl* io_context_impl_;
  84. strand_impl* impl_;
  85. ~on_dispatch_exit()
  86. {
  87. impl_->mutex_.lock();
  88. impl_->ready_queue_.push(impl_->waiting_queue_);
  89. bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
  90. impl_->mutex_.unlock();
  91. if (more_handlers)
  92. io_context_impl_->post_immediate_completion(impl_, false);
  93. }
  94. };
  95. void strand_service::do_dispatch(implementation_type& impl, operation* op)
  96. {
  97. // If we are running inside the io_context, and no other handler already
  98. // holds the strand lock, then the handler can run immediately.
  99. bool can_dispatch = io_context_impl_.can_dispatch();
  100. impl->mutex_.lock();
  101. if (can_dispatch && !impl->locked_)
  102. {
  103. // Immediate invocation is allowed.
  104. impl->locked_ = true;
  105. impl->mutex_.unlock();
  106. // Indicate that this strand is executing on the current thread.
  107. call_stack<strand_impl>::context ctx(impl);
  108. // Ensure the next handler, if any, is scheduled on block exit.
  109. on_dispatch_exit on_exit = { &io_context_impl_, impl };
  110. (void)on_exit;
  111. op->complete(&io_context_impl_, boost::system::error_code(), 0);
  112. return;
  113. }
  114. if (impl->locked_)
  115. {
  116. // Some other handler already holds the strand lock. Enqueue for later.
  117. impl->waiting_queue_.push(op);
  118. impl->mutex_.unlock();
  119. }
  120. else
  121. {
  122. // The handler is acquiring the strand lock and so is responsible for
  123. // scheduling the strand.
  124. impl->locked_ = true;
  125. impl->mutex_.unlock();
  126. impl->ready_queue_.push(op);
  127. io_context_impl_.post_immediate_completion(impl, false);
  128. }
  129. }
  130. void strand_service::do_post(implementation_type& impl,
  131. operation* op, bool is_continuation)
  132. {
  133. impl->mutex_.lock();
  134. if (impl->locked_)
  135. {
  136. // Some other handler already holds the strand lock. Enqueue for later.
  137. impl->waiting_queue_.push(op);
  138. impl->mutex_.unlock();
  139. }
  140. else
  141. {
  142. // The handler is acquiring the strand lock and so is responsible for
  143. // scheduling the strand.
  144. impl->locked_ = true;
  145. impl->mutex_.unlock();
  146. impl->ready_queue_.push(op);
  147. io_context_impl_.post_immediate_completion(impl, is_continuation);
  148. }
  149. }
  150. void strand_service::do_complete(void* owner, operation* base,
  151. const boost::system::error_code& ec, std::size_t /*bytes_transferred*/)
  152. {
  153. if (owner)
  154. {
  155. strand_impl* impl = static_cast<strand_impl*>(base);
  156. // Indicate that this strand is executing on the current thread.
  157. 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;
  160. on_exit.owner_ = static_cast<io_context_impl*>(owner);
  161. on_exit.impl_ = 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())
  165. {
  166. impl->ready_queue_.pop();
  167. o->complete(owner, ec, 0);
  168. }
  169. }
  170. }
  171. } // namespace detail
  172. } // namespace asio
  173. } // namespace boost
  174. #include <boost/asio/detail/pop_options.hpp>
  175. #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP