Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.7KB

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