strand_executor_service.ipp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // detail/impl/strand_executor_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_EXECUTOR_SERVICE_IPP
  11. #define ASIO_DETAIL_IMPL_STRAND_EXECUTOR_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/strand_executor_service.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. namespace detail {
  20. strand_executor_service::strand_executor_service(execution_context& ctx)
  21. : execution_context_service_base<strand_executor_service>(ctx),
  22. mutex_(),
  23. salt_(0),
  24. impl_list_(0)
  25. {
  26. }
  27. void strand_executor_service::shutdown()
  28. {
  29. op_queue<scheduler_operation> ops;
  30. asio::detail::mutex::scoped_lock lock(mutex_);
  31. strand_impl* impl = impl_list_;
  32. while (impl)
  33. {
  34. impl->mutex_->lock();
  35. impl->shutdown_ = true;
  36. ops.push(impl->waiting_queue_);
  37. ops.push(impl->ready_queue_);
  38. impl->mutex_->unlock();
  39. impl = impl->next_;
  40. }
  41. }
  42. strand_executor_service::implementation_type
  43. strand_executor_service::create_implementation()
  44. {
  45. implementation_type new_impl(new strand_impl);
  46. new_impl->locked_ = false;
  47. new_impl->shutdown_ = false;
  48. asio::detail::mutex::scoped_lock lock(mutex_);
  49. // Select a mutex from the pool of shared mutexes.
  50. std::size_t salt = salt_++;
  51. std::size_t mutex_index = reinterpret_cast<std::size_t>(new_impl.get());
  52. mutex_index += (reinterpret_cast<std::size_t>(new_impl.get()) >> 3);
  53. mutex_index ^= salt + 0x9e3779b9 + (mutex_index << 6) + (mutex_index >> 2);
  54. mutex_index = mutex_index % num_mutexes;
  55. if (!mutexes_[mutex_index].get())
  56. mutexes_[mutex_index].reset(new mutex);
  57. new_impl->mutex_ = mutexes_[mutex_index].get();
  58. // Insert implementation into linked list of all implementations.
  59. new_impl->next_ = impl_list_;
  60. new_impl->prev_ = 0;
  61. if (impl_list_)
  62. impl_list_->prev_ = new_impl.get();
  63. impl_list_ = new_impl.get();
  64. new_impl->service_ = this;
  65. return new_impl;
  66. }
  67. strand_executor_service::strand_impl::~strand_impl()
  68. {
  69. asio::detail::mutex::scoped_lock lock(service_->mutex_);
  70. // Remove implementation from linked list of all implementations.
  71. if (service_->impl_list_ == this)
  72. service_->impl_list_ = next_;
  73. if (prev_)
  74. prev_->next_ = next_;
  75. if (next_)
  76. next_->prev_= prev_;
  77. }
  78. bool strand_executor_service::enqueue(const implementation_type& impl,
  79. scheduler_operation* op)
  80. {
  81. impl->mutex_->lock();
  82. if (impl->shutdown_)
  83. {
  84. impl->mutex_->unlock();
  85. op->destroy();
  86. return false;
  87. }
  88. else if (impl->locked_)
  89. {
  90. // Some other function already holds the strand lock. Enqueue for later.
  91. impl->waiting_queue_.push(op);
  92. impl->mutex_->unlock();
  93. return false;
  94. }
  95. else
  96. {
  97. // The function is acquiring the strand lock and so is responsible for
  98. // scheduling the strand.
  99. impl->locked_ = true;
  100. impl->mutex_->unlock();
  101. impl->ready_queue_.push(op);
  102. return true;
  103. }
  104. }
  105. bool strand_executor_service::running_in_this_thread(
  106. const implementation_type& impl)
  107. {
  108. return !!call_stack<strand_impl>::contains(impl.get());
  109. }
  110. bool strand_executor_service::push_waiting_to_ready(implementation_type& impl)
  111. {
  112. impl->mutex_->lock();
  113. impl->ready_queue_.push(impl->waiting_queue_);
  114. bool more_handlers = impl->locked_ = !impl->ready_queue_.empty();
  115. impl->mutex_->unlock();
  116. return more_handlers;
  117. }
  118. void strand_executor_service::run_ready_handlers(implementation_type& impl)
  119. {
  120. // Indicate that this strand is executing on the current thread.
  121. call_stack<strand_impl>::context ctx(impl.get());
  122. // Run all ready handlers. No lock is required since the ready queue is
  123. // accessed only within the strand.
  124. asio::error_code ec;
  125. while (scheduler_operation* o = impl->ready_queue_.front())
  126. {
  127. impl->ready_queue_.pop();
  128. o->complete(impl.get(), ec, 0);
  129. }
  130. }
  131. } // namespace detail
  132. } // namespace asio
  133. #include "asio/detail/pop_options.hpp"
  134. #endif // ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_IPP