strand_service.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // detail/impl/strand_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/completion_handler.hpp>
  16. #include <boost/asio/detail/fenced_block.hpp>
  17. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  18. #include <boost/asio/detail/memory.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. inline strand_service::strand_impl::strand_impl()
  24. : operation(&strand_service::do_complete),
  25. locked_(false)
  26. {
  27. }
  28. template <typename Handler>
  29. void strand_service::dispatch(strand_service::implementation_type& impl,
  30. Handler& handler)
  31. {
  32. // If we are already in the strand then the handler can run immediately.
  33. if (running_in_this_thread(impl))
  34. {
  35. fenced_block b(fenced_block::full);
  36. static_cast<Handler&&>(handler)();
  37. return;
  38. }
  39. // Allocate and construct an operation to wrap the handler.
  40. typedef completion_handler<Handler, io_context::executor_type> op;
  41. typename op::ptr p = { boost::asio::detail::addressof(handler),
  42. op::ptr::allocate(handler), 0 };
  43. p.p = new (p.v) op(handler, io_context_.get_executor());
  44. BOOST_ASIO_HANDLER_CREATION((this->context(),
  45. *p.p, "strand", impl, 0, "dispatch"));
  46. operation* o = p.p;
  47. p.v = p.p = 0;
  48. do_dispatch(impl, o);
  49. }
  50. // Request the io_context to invoke the given handler and return immediately.
  51. template <typename Handler>
  52. void strand_service::post(strand_service::implementation_type& impl,
  53. Handler& handler)
  54. {
  55. bool is_continuation =
  56. boost_asio_handler_cont_helpers::is_continuation(handler);
  57. // Allocate and construct an operation to wrap the handler.
  58. typedef completion_handler<Handler, io_context::executor_type> op;
  59. typename op::ptr p = { boost::asio::detail::addressof(handler),
  60. op::ptr::allocate(handler), 0 };
  61. p.p = new (p.v) op(handler, io_context_.get_executor());
  62. BOOST_ASIO_HANDLER_CREATION((this->context(),
  63. *p.p, "strand", impl, 0, "post"));
  64. do_post(impl, p.p, is_continuation);
  65. p.v = p.p = 0;
  66. }
  67. } // namespace detail
  68. } // namespace asio
  69. } // namespace boost
  70. #include <boost/asio/detail/pop_options.hpp>
  71. #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP