work_dispatcher.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // detail/work_dispatcher.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_WORK_DISPATCHER_HPP
  11. #define BOOST_ASIO_DETAIL_WORK_DISPATCHER_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/config.hpp>
  16. #include <boost/asio/detail/type_traits.hpp>
  17. #include <boost/asio/associated_executor.hpp>
  18. #include <boost/asio/associated_allocator.hpp>
  19. #include <boost/asio/executor_work_guard.hpp>
  20. #include <boost/asio/execution/executor.hpp>
  21. #include <boost/asio/execution/allocator.hpp>
  22. #include <boost/asio/execution/blocking.hpp>
  23. #include <boost/asio/execution/outstanding_work.hpp>
  24. #include <boost/asio/prefer.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. template <typename Handler, typename Executor, typename = void>
  30. struct is_work_dispatcher_required : true_type
  31. {
  32. };
  33. template <typename Handler, typename Executor>
  34. struct is_work_dispatcher_required<Handler, Executor,
  35. typename enable_if<
  36. is_same<
  37. typename associated_executor<Handler,
  38. Executor>::asio_associated_executor_is_unspecialised,
  39. void
  40. >::value
  41. >::type> : false_type
  42. {
  43. };
  44. template <typename Handler, typename Executor, typename = void>
  45. class work_dispatcher
  46. {
  47. public:
  48. template <typename CompletionHandler>
  49. work_dispatcher(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  50. const Executor& handler_ex)
  51. : handler_(BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)),
  52. executor_(boost::asio::prefer(handler_ex,
  53. execution::outstanding_work.tracked))
  54. {
  55. }
  56. #if defined(BOOST_ASIO_HAS_MOVE)
  57. work_dispatcher(const work_dispatcher& other)
  58. : handler_(other.handler_),
  59. executor_(other.executor_)
  60. {
  61. }
  62. work_dispatcher(work_dispatcher&& other)
  63. : handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)),
  64. executor_(BOOST_ASIO_MOVE_CAST(work_executor_type)(other.executor_))
  65. {
  66. }
  67. #endif // defined(BOOST_ASIO_HAS_MOVE)
  68. void operator()()
  69. {
  70. execution::execute(
  71. boost::asio::prefer(executor_,
  72. execution::blocking.possibly,
  73. execution::allocator((get_associated_allocator)(handler_))),
  74. BOOST_ASIO_MOVE_CAST(Handler)(handler_));
  75. }
  76. private:
  77. typedef typename decay<
  78. typename prefer_result<const Executor&,
  79. execution::outstanding_work_t::tracked_t
  80. >::type
  81. >::type work_executor_type;
  82. Handler handler_;
  83. work_executor_type executor_;
  84. };
  85. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  86. template <typename Handler, typename Executor>
  87. class work_dispatcher<Handler, Executor,
  88. typename enable_if<!execution::is_executor<Executor>::value>::type>
  89. {
  90. public:
  91. template <typename CompletionHandler>
  92. work_dispatcher(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  93. const Executor& handler_ex)
  94. : work_(handler_ex),
  95. handler_(BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler))
  96. {
  97. }
  98. #if defined(BOOST_ASIO_HAS_MOVE)
  99. work_dispatcher(const work_dispatcher& other)
  100. : work_(other.work_),
  101. handler_(other.handler_)
  102. {
  103. }
  104. work_dispatcher(work_dispatcher&& other)
  105. : work_(BOOST_ASIO_MOVE_CAST(executor_work_guard<Executor>)(other.work_)),
  106. handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
  107. {
  108. }
  109. #endif // defined(BOOST_ASIO_HAS_MOVE)
  110. void operator()()
  111. {
  112. typename associated_allocator<Handler>::type alloc(
  113. (get_associated_allocator)(handler_));
  114. work_.get_executor().dispatch(
  115. BOOST_ASIO_MOVE_CAST(Handler)(handler_), alloc);
  116. work_.reset();
  117. }
  118. private:
  119. executor_work_guard<Executor> work_;
  120. Handler handler_;
  121. };
  122. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  123. } // namespace detail
  124. } // namespace asio
  125. } // namespace boost
  126. #include <boost/asio/detail/pop_options.hpp>
  127. #endif // BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP