strand_service.hpp 2.6 KB

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