strand_service.hpp 3.4 KB

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