executor_work_guard.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // executor_work_guard.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_EXECUTOR_WORK_GUARD_HPP
  11. #define BOOST_ASIO_EXECUTOR_WORK_GUARD_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/associated_executor.hpp>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/is_executor.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. /// An object of type @c executor_work_guard controls ownership of executor work
  23. /// within a scope.
  24. template <typename Executor>
  25. class executor_work_guard
  26. {
  27. public:
  28. /// The underlying executor type.
  29. typedef Executor executor_type;
  30. /// Constructs a @c executor_work_guard object for the specified executor.
  31. /**
  32. * Stores a copy of @c e and calls <tt>on_work_started()</tt> on it.
  33. */
  34. explicit executor_work_guard(const executor_type& e) BOOST_ASIO_NOEXCEPT
  35. : executor_(e),
  36. owns_(true)
  37. {
  38. executor_.on_work_started();
  39. }
  40. /// Copy constructor.
  41. executor_work_guard(const executor_work_guard& other) BOOST_ASIO_NOEXCEPT
  42. : executor_(other.executor_),
  43. owns_(other.owns_)
  44. {
  45. if (owns_)
  46. executor_.on_work_started();
  47. }
  48. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  49. /// Move constructor.
  50. executor_work_guard(executor_work_guard&& other)
  51. : executor_(BOOST_ASIO_MOVE_CAST(Executor)(other.executor_)),
  52. owns_(other.owns_)
  53. {
  54. other.owns_ = false;
  55. }
  56. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  57. /// Destructor.
  58. /**
  59. * Unless the object has already been reset, or is in a moved-from state,
  60. * calls <tt>on_work_finished()</tt> on the stored executor.
  61. */
  62. ~executor_work_guard()
  63. {
  64. if (owns_)
  65. executor_.on_work_finished();
  66. }
  67. /// Obtain the associated executor.
  68. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  69. {
  70. return executor_;
  71. }
  72. /// Whether the executor_work_guard object owns some outstanding work.
  73. bool owns_work() const BOOST_ASIO_NOEXCEPT
  74. {
  75. return owns_;
  76. }
  77. /// Indicate that the work is no longer outstanding.
  78. /*
  79. * Unless the object has already been reset, or is in a moved-from state,
  80. * calls <tt>on_work_finished()</tt> on the stored executor.
  81. */
  82. void reset() BOOST_ASIO_NOEXCEPT
  83. {
  84. if (owns_)
  85. {
  86. executor_.on_work_finished();
  87. owns_ = false;
  88. }
  89. }
  90. private:
  91. // Disallow assignment.
  92. executor_work_guard& operator=(const executor_work_guard&);
  93. executor_type executor_;
  94. bool owns_;
  95. };
  96. /// Create an @ref executor_work_guard object.
  97. template <typename Executor>
  98. inline executor_work_guard<Executor> make_work_guard(const Executor& ex,
  99. typename enable_if<is_executor<Executor>::value>::type* = 0)
  100. {
  101. return executor_work_guard<Executor>(ex);
  102. }
  103. /// Create an @ref executor_work_guard object.
  104. template <typename ExecutionContext>
  105. inline executor_work_guard<typename ExecutionContext::executor_type>
  106. make_work_guard(ExecutionContext& ctx,
  107. typename enable_if<
  108. is_convertible<ExecutionContext&, execution_context&>::value>::type* = 0)
  109. {
  110. return executor_work_guard<typename ExecutionContext::executor_type>(
  111. ctx.get_executor());
  112. }
  113. /// Create an @ref executor_work_guard object.
  114. template <typename T>
  115. inline executor_work_guard<typename associated_executor<T>::type>
  116. make_work_guard(const T& t,
  117. typename enable_if<!is_executor<T>::value &&
  118. !is_convertible<T&, execution_context&>::value>::type* = 0)
  119. {
  120. return executor_work_guard<typename associated_executor<T>::type>(
  121. associated_executor<T>::get(t));
  122. }
  123. /// Create an @ref executor_work_guard object.
  124. template <typename T, typename Executor>
  125. inline executor_work_guard<typename associated_executor<T, Executor>::type>
  126. make_work_guard(const T& t, const Executor& ex,
  127. typename enable_if<is_executor<Executor>::value>::type* = 0)
  128. {
  129. return executor_work_guard<typename associated_executor<T, Executor>::type>(
  130. associated_executor<T, Executor>::get(t, ex));
  131. }
  132. /// Create an @ref executor_work_guard object.
  133. template <typename T, typename ExecutionContext>
  134. inline executor_work_guard<typename associated_executor<T,
  135. typename ExecutionContext::executor_type>::type>
  136. make_work_guard(const T& t, ExecutionContext& ctx,
  137. typename enable_if<!is_executor<T>::value &&
  138. !is_convertible<T&, execution_context&>::value &&
  139. is_convertible<ExecutionContext&, execution_context&>::value>::type* = 0)
  140. {
  141. return executor_work_guard<typename associated_executor<T,
  142. typename ExecutionContext::executor_type>::type>(
  143. associated_executor<T, typename ExecutionContext::executor_type>::get(
  144. t, ctx.get_executor()));
  145. }
  146. } // namespace asio
  147. } // namespace boost
  148. #include <boost/asio/detail/pop_options.hpp>
  149. #endif // BOOST_ASIO_EXECUTOR_WORK_GUARD_HPP