thread_pool.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // impl/thread_pool.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_IMPL_THREAD_POOL_HPP
  11. #define ASIO_IMPL_THREAD_POOL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/blocking_executor_op.hpp"
  16. #include "asio/detail/executor_op.hpp"
  17. #include "asio/detail/fenced_block.hpp"
  18. #include "asio/detail/non_const_lvalue.hpp"
  19. #include "asio/detail/type_traits.hpp"
  20. #include "asio/execution_context.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. inline thread_pool::executor_type
  24. thread_pool::get_executor() noexcept
  25. {
  26. return executor_type(*this);
  27. }
  28. inline thread_pool::executor_type
  29. thread_pool::executor() noexcept
  30. {
  31. return executor_type(*this);
  32. }
  33. template <typename Allocator, unsigned int Bits>
  34. thread_pool::basic_executor_type<Allocator, Bits>&
  35. thread_pool::basic_executor_type<Allocator, Bits>::operator=(
  36. const basic_executor_type& other) noexcept
  37. {
  38. if (this != &other)
  39. {
  40. thread_pool* old_thread_pool = pool_;
  41. pool_ = other.pool_;
  42. allocator_ = other.allocator_;
  43. bits_ = other.bits_;
  44. if (Bits & outstanding_work_tracked)
  45. {
  46. if (pool_)
  47. pool_->scheduler_.work_started();
  48. if (old_thread_pool)
  49. old_thread_pool->scheduler_.work_finished();
  50. }
  51. }
  52. return *this;
  53. }
  54. template <typename Allocator, unsigned int Bits>
  55. thread_pool::basic_executor_type<Allocator, Bits>&
  56. thread_pool::basic_executor_type<Allocator, Bits>::operator=(
  57. basic_executor_type&& other) noexcept
  58. {
  59. if (this != &other)
  60. {
  61. thread_pool* old_thread_pool = pool_;
  62. pool_ = other.pool_;
  63. allocator_ = std::move(other.allocator_);
  64. bits_ = other.bits_;
  65. if (Bits & outstanding_work_tracked)
  66. {
  67. other.pool_ = 0;
  68. if (old_thread_pool)
  69. old_thread_pool->scheduler_.work_finished();
  70. }
  71. }
  72. return *this;
  73. }
  74. template <typename Allocator, unsigned int Bits>
  75. inline bool thread_pool::basic_executor_type<Allocator,
  76. Bits>::running_in_this_thread() const noexcept
  77. {
  78. return pool_->scheduler_.can_dispatch();
  79. }
  80. template <typename Allocator, unsigned int Bits>
  81. template <typename Function>
  82. void thread_pool::basic_executor_type<Allocator,
  83. Bits>::do_execute(Function&& f, false_type) const
  84. {
  85. typedef decay_t<Function> function_type;
  86. // Invoke immediately if the blocking.possibly property is enabled and we are
  87. // already inside the thread pool.
  88. if ((bits_ & blocking_never) == 0 && pool_->scheduler_.can_dispatch())
  89. {
  90. // Make a local, non-const copy of the function.
  91. function_type tmp(static_cast<Function&&>(f));
  92. #if !defined(ASIO_NO_EXCEPTIONS)
  93. try
  94. {
  95. #endif // !defined(ASIO_NO_EXCEPTIONS)
  96. detail::fenced_block b(detail::fenced_block::full);
  97. static_cast<function_type&&>(tmp)();
  98. return;
  99. #if !defined(ASIO_NO_EXCEPTIONS)
  100. }
  101. catch (...)
  102. {
  103. pool_->scheduler_.capture_current_exception();
  104. return;
  105. }
  106. #endif // !defined(ASIO_NO_EXCEPTIONS)
  107. }
  108. // Allocate and construct an operation to wrap the function.
  109. typedef detail::executor_op<function_type, Allocator> op;
  110. typename op::ptr p = { detail::addressof(allocator_),
  111. op::ptr::allocate(allocator_), 0 };
  112. p.p = new (p.v) op(static_cast<Function&&>(f), allocator_);
  113. if ((bits_ & relationship_continuation) != 0)
  114. {
  115. ASIO_HANDLER_CREATION((*pool_, *p.p,
  116. "thread_pool", pool_, 0, "execute(blk=never,rel=cont)"));
  117. }
  118. else
  119. {
  120. ASIO_HANDLER_CREATION((*pool_, *p.p,
  121. "thread_pool", pool_, 0, "execute(blk=never,rel=fork)"));
  122. }
  123. pool_->scheduler_.post_immediate_completion(p.p,
  124. (bits_ & relationship_continuation) != 0);
  125. p.v = p.p = 0;
  126. }
  127. template <typename Allocator, unsigned int Bits>
  128. template <typename Function>
  129. void thread_pool::basic_executor_type<Allocator,
  130. Bits>::do_execute(Function&& f, true_type) const
  131. {
  132. // Obtain a non-const instance of the function.
  133. detail::non_const_lvalue<Function> f2(f);
  134. // Invoke immediately if we are already inside the thread pool.
  135. if (pool_->scheduler_.can_dispatch())
  136. {
  137. #if !defined(ASIO_NO_EXCEPTIONS)
  138. try
  139. {
  140. #endif // !defined(ASIO_NO_EXCEPTIONS)
  141. detail::fenced_block b(detail::fenced_block::full);
  142. static_cast<decay_t<Function>&&>(f2.value)();
  143. return;
  144. #if !defined(ASIO_NO_EXCEPTIONS)
  145. }
  146. catch (...)
  147. {
  148. std::terminate();
  149. }
  150. #endif // !defined(ASIO_NO_EXCEPTIONS)
  151. }
  152. // Construct an operation to wrap the function.
  153. typedef decay_t<Function> function_type;
  154. detail::blocking_executor_op<function_type> op(f2.value);
  155. ASIO_HANDLER_CREATION((*pool_, op,
  156. "thread_pool", pool_, 0, "execute(blk=always)"));
  157. pool_->scheduler_.post_immediate_completion(&op, false);
  158. op.wait();
  159. }
  160. #if !defined(ASIO_NO_TS_EXECUTORS)
  161. template <typename Allocator, unsigned int Bits>
  162. inline thread_pool& thread_pool::basic_executor_type<
  163. Allocator, Bits>::context() const noexcept
  164. {
  165. return *pool_;
  166. }
  167. template <typename Allocator, unsigned int Bits>
  168. inline void thread_pool::basic_executor_type<Allocator,
  169. Bits>::on_work_started() const noexcept
  170. {
  171. pool_->scheduler_.work_started();
  172. }
  173. template <typename Allocator, unsigned int Bits>
  174. inline void thread_pool::basic_executor_type<Allocator,
  175. Bits>::on_work_finished() const noexcept
  176. {
  177. pool_->scheduler_.work_finished();
  178. }
  179. template <typename Allocator, unsigned int Bits>
  180. template <typename Function, typename OtherAllocator>
  181. void thread_pool::basic_executor_type<Allocator, Bits>::dispatch(
  182. Function&& f, const OtherAllocator& a) const
  183. {
  184. typedef decay_t<Function> function_type;
  185. // Invoke immediately if we are already inside the thread pool.
  186. if (pool_->scheduler_.can_dispatch())
  187. {
  188. // Make a local, non-const copy of the function.
  189. function_type tmp(static_cast<Function&&>(f));
  190. detail::fenced_block b(detail::fenced_block::full);
  191. static_cast<function_type&&>(tmp)();
  192. return;
  193. }
  194. // Allocate and construct an operation to wrap the function.
  195. typedef detail::executor_op<function_type, OtherAllocator> op;
  196. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  197. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  198. ASIO_HANDLER_CREATION((*pool_, *p.p,
  199. "thread_pool", pool_, 0, "dispatch"));
  200. pool_->scheduler_.post_immediate_completion(p.p, false);
  201. p.v = p.p = 0;
  202. }
  203. template <typename Allocator, unsigned int Bits>
  204. template <typename Function, typename OtherAllocator>
  205. void thread_pool::basic_executor_type<Allocator, Bits>::post(
  206. Function&& f, const OtherAllocator& a) const
  207. {
  208. typedef decay_t<Function> function_type;
  209. // Allocate and construct an operation to wrap the function.
  210. typedef detail::executor_op<function_type, OtherAllocator> op;
  211. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  212. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  213. ASIO_HANDLER_CREATION((*pool_, *p.p,
  214. "thread_pool", pool_, 0, "post"));
  215. pool_->scheduler_.post_immediate_completion(p.p, false);
  216. p.v = p.p = 0;
  217. }
  218. template <typename Allocator, unsigned int Bits>
  219. template <typename Function, typename OtherAllocator>
  220. void thread_pool::basic_executor_type<Allocator, Bits>::defer(
  221. Function&& f, const OtherAllocator& a) const
  222. {
  223. typedef decay_t<Function> function_type;
  224. // Allocate and construct an operation to wrap the function.
  225. typedef detail::executor_op<function_type, OtherAllocator> op;
  226. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  227. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  228. ASIO_HANDLER_CREATION((*pool_, *p.p,
  229. "thread_pool", pool_, 0, "defer"));
  230. pool_->scheduler_.post_immediate_completion(p.p, true);
  231. p.v = p.p = 0;
  232. }
  233. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  234. } // namespace asio
  235. #include "asio/detail/pop_options.hpp"
  236. #endif // ASIO_IMPL_THREAD_POOL_HPP