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.

strand_service.hpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // detail/impl/strand_service.hpp
  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_HPP
  11. #define ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/call_stack.hpp"
  16. #include "asio/detail/completion_handler.hpp"
  17. #include "asio/detail/fenced_block.hpp"
  18. #include "asio/detail/handler_alloc_helpers.hpp"
  19. #include "asio/detail/handler_invoke_helpers.hpp"
  20. #include "asio/detail/memory.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. inline strand_service::strand_impl::strand_impl()
  25. : operation(&strand_service::do_complete),
  26. locked_(false)
  27. {
  28. }
  29. struct strand_service::on_dispatch_exit
  30. {
  31. io_context_impl* io_context_;
  32. strand_impl* impl_;
  33. ~on_dispatch_exit()
  34. {
  35. impl_->mutex_.lock();
  36. impl_->ready_queue_.push(impl_->waiting_queue_);
  37. bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
  38. impl_->mutex_.unlock();
  39. if (more_handlers)
  40. io_context_->post_immediate_completion(impl_, false);
  41. }
  42. };
  43. template <typename Handler>
  44. void strand_service::dispatch(strand_service::implementation_type& impl,
  45. Handler& handler)
  46. {
  47. // If we are already in the strand then the handler can run immediately.
  48. if (call_stack<strand_impl>::contains(impl))
  49. {
  50. fenced_block b(fenced_block::full);
  51. asio_handler_invoke_helpers::invoke(handler, handler);
  52. return;
  53. }
  54. // Allocate and construct an operation to wrap the handler.
  55. typedef completion_handler<Handler> op;
  56. typename op::ptr p = { asio::detail::addressof(handler),
  57. op::ptr::allocate(handler), 0 };
  58. p.p = new (p.v) op(handler);
  59. ASIO_HANDLER_CREATION((this->context(),
  60. *p.p, "strand", impl, 0, "dispatch"));
  61. bool dispatch_immediately = do_dispatch(impl, p.p);
  62. operation* o = p.p;
  63. p.v = p.p = 0;
  64. if (dispatch_immediately)
  65. {
  66. // Indicate that this strand is executing on the current thread.
  67. call_stack<strand_impl>::context ctx(impl);
  68. // Ensure the next handler, if any, is scheduled on block exit.
  69. on_dispatch_exit on_exit = { &io_context_, impl };
  70. (void)on_exit;
  71. completion_handler<Handler>::do_complete(
  72. &io_context_, o, asio::error_code(), 0);
  73. }
  74. }
  75. // Request the io_context to invoke the given handler and return immediately.
  76. template <typename Handler>
  77. void strand_service::post(strand_service::implementation_type& impl,
  78. Handler& handler)
  79. {
  80. bool is_continuation =
  81. asio_handler_cont_helpers::is_continuation(handler);
  82. // Allocate and construct an operation to wrap the handler.
  83. typedef completion_handler<Handler> op;
  84. typename op::ptr p = { asio::detail::addressof(handler),
  85. op::ptr::allocate(handler), 0 };
  86. p.p = new (p.v) op(handler);
  87. ASIO_HANDLER_CREATION((this->context(),
  88. *p.p, "strand", impl, 0, "post"));
  89. do_post(impl, p.p, is_continuation);
  90. p.v = p.p = 0;
  91. }
  92. } // namespace detail
  93. } // namespace asio
  94. #include "asio/detail/pop_options.hpp"
  95. #endif // ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP