strand_service.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // detail/impl/strand_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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/completion_handler.hpp"
  16. #include "asio/detail/fenced_block.hpp"
  17. #include "asio/detail/handler_alloc_helpers.hpp"
  18. #include "asio/detail/handler_invoke_helpers.hpp"
  19. #include "asio/detail/memory.hpp"
  20. #include "asio/detail/push_options.hpp"
  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. asio_handler_invoke_helpers::invoke(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 = { asio::detail::addressof(handler),
  42. op::ptr::allocate(handler), 0 };
  43. p.p = new (p.v) op(handler, io_context_.get_executor());
  44. 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. 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 = { asio::detail::addressof(handler),
  60. op::ptr::allocate(handler), 0 };
  61. p.p = new (p.v) op(handler, io_context_.get_executor());
  62. 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. #include "asio/detail/pop_options.hpp"
  70. #endif // ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP