strand_service.ipp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // detail/impl/strand_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_context_impl* owner_;
  24. strand_impl* impl_;
  25. ~on_do_complete_exit()
  26. {
  27. impl_->mutex_.lock();
  28. impl_->ready_queue_.push(impl_->waiting_queue_);
  29. bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
  30. impl_->mutex_.unlock();
  31. if (more_handlers)
  32. owner_->post_immediate_completion(impl_, true);
  33. }
  34. };
  35. strand_service::strand_service(asio::io_context& io_context)
  36. : asio::detail::service_base<strand_service>(io_context),
  37. io_context_(io_context),
  38. io_context_impl_(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. 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. asio::detail::mutex::scoped_lock lock(mutex_);
  59. std::size_t salt = salt_++;
  60. #if defined(ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION)
  61. std::size_t index = salt;
  62. #else // defined(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(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. struct strand_service::on_dispatch_exit
  78. {
  79. io_context_impl* io_context_impl_;
  80. strand_impl* impl_;
  81. ~on_dispatch_exit()
  82. {
  83. impl_->mutex_.lock();
  84. impl_->ready_queue_.push(impl_->waiting_queue_);
  85. bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
  86. impl_->mutex_.unlock();
  87. if (more_handlers)
  88. io_context_impl_->post_immediate_completion(impl_, false);
  89. }
  90. };
  91. void strand_service::do_dispatch(implementation_type& impl, operation* op)
  92. {
  93. // If we are running inside the io_context, and no other handler already
  94. // holds the strand lock, then the handler can run immediately.
  95. bool can_dispatch = io_context_impl_.can_dispatch();
  96. impl->mutex_.lock();
  97. if (can_dispatch && !impl->locked_)
  98. {
  99. // Immediate invocation is allowed.
  100. impl->locked_ = true;
  101. impl->mutex_.unlock();
  102. // Indicate that this strand is executing on the current thread.
  103. call_stack<strand_impl>::context ctx(impl);
  104. // Ensure the next handler, if any, is scheduled on block exit.
  105. on_dispatch_exit on_exit = { &io_context_impl_, impl };
  106. (void)on_exit;
  107. op->complete(&io_context_impl_, asio::error_code(), 0);
  108. return;
  109. }
  110. if (impl->locked_)
  111. {
  112. // Some other handler already holds the strand lock. Enqueue for later.
  113. impl->waiting_queue_.push(op);
  114. impl->mutex_.unlock();
  115. }
  116. else
  117. {
  118. // The handler is acquiring the strand lock and so is responsible for
  119. // scheduling the strand.
  120. impl->locked_ = true;
  121. impl->mutex_.unlock();
  122. impl->ready_queue_.push(op);
  123. io_context_impl_.post_immediate_completion(impl, false);
  124. }
  125. }
  126. void strand_service::do_post(implementation_type& impl,
  127. operation* op, bool is_continuation)
  128. {
  129. impl->mutex_.lock();
  130. if (impl->locked_)
  131. {
  132. // Some other handler already holds the strand lock. Enqueue for later.
  133. impl->waiting_queue_.push(op);
  134. impl->mutex_.unlock();
  135. }
  136. else
  137. {
  138. // The handler is acquiring the strand lock and so is responsible for
  139. // scheduling the strand.
  140. impl->locked_ = true;
  141. impl->mutex_.unlock();
  142. impl->ready_queue_.push(op);
  143. io_context_impl_.post_immediate_completion(impl, is_continuation);
  144. }
  145. }
  146. void strand_service::do_complete(void* owner, operation* base,
  147. const asio::error_code& ec, std::size_t /*bytes_transferred*/)
  148. {
  149. if (owner)
  150. {
  151. strand_impl* impl = static_cast<strand_impl*>(base);
  152. // Indicate that this strand is executing on the current thread.
  153. call_stack<strand_impl>::context ctx(impl);
  154. // Ensure the next handler, if any, is scheduled on block exit.
  155. on_do_complete_exit on_exit;
  156. on_exit.owner_ = static_cast<io_context_impl*>(owner);
  157. on_exit.impl_ = impl;
  158. // Run all ready handlers. No lock is required since the ready queue is
  159. // accessed only within the strand.
  160. while (operation* o = impl->ready_queue_.front())
  161. {
  162. impl->ready_queue_.pop();
  163. o->complete(owner, ec, 0);
  164. }
  165. }
  166. }
  167. } // namespace detail
  168. } // namespace asio
  169. #include "asio/detail/pop_options.hpp"
  170. #endif // ASIO_DETAIL_IMPL_STRAND_SERVICE_IPP