strand_executor_service.ipp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // detail/impl/strand_executor_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_EXECUTOR_SERVICE_IPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/strand_executor_service.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. strand_executor_service::strand_executor_service(execution_context& ctx)
  22. : execution_context_service_base<strand_executor_service>(ctx),
  23. mutex_(),
  24. salt_(0),
  25. impl_list_(0)
  26. {
  27. }
  28. void strand_executor_service::shutdown()
  29. {
  30. op_queue<scheduler_operation> ops;
  31. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  32. strand_impl* impl = impl_list_;
  33. while (impl)
  34. {
  35. impl->mutex_->lock();
  36. impl->shutdown_ = true;
  37. ops.push(impl->waiting_queue_);
  38. ops.push(impl->ready_queue_);
  39. impl->mutex_->unlock();
  40. impl = impl->next_;
  41. }
  42. }
  43. strand_executor_service::implementation_type
  44. strand_executor_service::create_implementation()
  45. {
  46. implementation_type new_impl(new strand_impl);
  47. new_impl->locked_ = false;
  48. new_impl->shutdown_ = false;
  49. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  50. // Select a mutex from the pool of shared mutexes.
  51. std::size_t salt = salt_++;
  52. std::size_t mutex_index = reinterpret_cast<std::size_t>(new_impl.get());
  53. mutex_index += (reinterpret_cast<std::size_t>(new_impl.get()) >> 3);
  54. mutex_index ^= salt + 0x9e3779b9 + (mutex_index << 6) + (mutex_index >> 2);
  55. mutex_index = mutex_index % num_mutexes;
  56. if (!mutexes_[mutex_index].get())
  57. mutexes_[mutex_index].reset(new mutex);
  58. new_impl->mutex_ = mutexes_[mutex_index].get();
  59. // Insert implementation into linked list of all implementations.
  60. new_impl->next_ = impl_list_;
  61. new_impl->prev_ = 0;
  62. if (impl_list_)
  63. impl_list_->prev_ = new_impl.get();
  64. impl_list_ = new_impl.get();
  65. new_impl->service_ = this;
  66. return new_impl;
  67. }
  68. strand_executor_service::strand_impl::~strand_impl()
  69. {
  70. boost::asio::detail::mutex::scoped_lock lock(service_->mutex_);
  71. // Remove implementation from linked list of all implementations.
  72. if (service_->impl_list_ == this)
  73. service_->impl_list_ = next_;
  74. if (prev_)
  75. prev_->next_ = next_;
  76. if (next_)
  77. next_->prev_= prev_;
  78. }
  79. bool strand_executor_service::enqueue(const implementation_type& impl,
  80. scheduler_operation* op)
  81. {
  82. impl->mutex_->lock();
  83. if (impl->shutdown_)
  84. {
  85. impl->mutex_->unlock();
  86. op->destroy();
  87. return false;
  88. }
  89. else if (impl->locked_)
  90. {
  91. // Some other function already holds the strand lock. Enqueue for later.
  92. impl->waiting_queue_.push(op);
  93. impl->mutex_->unlock();
  94. return false;
  95. }
  96. else
  97. {
  98. // The function 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. return true;
  104. }
  105. }
  106. bool strand_executor_service::running_in_this_thread(
  107. const implementation_type& impl)
  108. {
  109. return !!call_stack<strand_impl>::contains(impl.get());
  110. }
  111. } // namespace detail
  112. } // namespace asio
  113. } // namespace boost
  114. #include <boost/asio/detail/pop_options.hpp>
  115. #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_IPP