strand_service.hpp 2.4 KB

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