ThreadPool.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Support/ThreadPool.h - A ThreadPool implementation -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines a crude C++11 based thread pool.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_THREADPOOL_H
  18. #define LLVM_SUPPORT_THREADPOOL_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/Config/llvm-config.h"
  21. #include "llvm/Support/RWMutex.h"
  22. #include "llvm/Support/Threading.h"
  23. #include "llvm/Support/thread.h"
  24. #include <future>
  25. #include <condition_variable>
  26. #include <deque>
  27. #include <functional>
  28. #include <memory>
  29. #include <mutex>
  30. #include <utility>
  31. namespace llvm {
  32. class ThreadPoolTaskGroup;
  33. /// A ThreadPool for asynchronous parallel execution on a defined number of
  34. /// threads.
  35. ///
  36. /// The pool keeps a vector of threads alive, waiting on a condition variable
  37. /// for some work to become available.
  38. ///
  39. /// It is possible to reuse one thread pool for different groups of tasks
  40. /// by grouping tasks using ThreadPoolTaskGroup. All tasks are processed using
  41. /// the same queue, but it is possible to wait only for a specific group of
  42. /// tasks to finish.
  43. ///
  44. /// It is also possible for worker threads to submit new tasks and wait for
  45. /// them. Note that this may result in a deadlock in cases such as when a task
  46. /// (directly or indirectly) tries to wait for its own completion, or when all
  47. /// available threads are used up by tasks waiting for a task that has no thread
  48. /// left to run on (this includes waiting on the returned future). It should be
  49. /// generally safe to wait() for a group as long as groups do not form a cycle.
  50. class ThreadPool {
  51. public:
  52. /// Construct a pool using the hardware strategy \p S for mapping hardware
  53. /// execution resources (threads, cores, CPUs)
  54. /// Defaults to using the maximum execution resources in the system, but
  55. /// accounting for the affinity mask.
  56. ThreadPool(ThreadPoolStrategy S = hardware_concurrency());
  57. /// Blocking destructor: the pool will wait for all the threads to complete.
  58. ~ThreadPool();
  59. /// Asynchronous submission of a task to the pool. The returned future can be
  60. /// used to wait for the task to finish and is *non-blocking* on destruction.
  61. template <typename Function, typename... Args>
  62. auto async(Function &&F, Args &&...ArgList) {
  63. auto Task =
  64. std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...);
  65. return async(std::move(Task));
  66. }
  67. /// Overload, task will be in the given task group.
  68. template <typename Function, typename... Args>
  69. auto async(ThreadPoolTaskGroup &Group, Function &&F, Args &&...ArgList) {
  70. auto Task =
  71. std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...);
  72. return async(Group, std::move(Task));
  73. }
  74. /// Asynchronous submission of a task to the pool. The returned future can be
  75. /// used to wait for the task to finish and is *non-blocking* on destruction.
  76. template <typename Func>
  77. auto async(Func &&F) -> std::shared_future<decltype(F())> {
  78. return asyncImpl(std::function<decltype(F())()>(std::forward<Func>(F)),
  79. nullptr);
  80. }
  81. template <typename Func>
  82. auto async(ThreadPoolTaskGroup &Group, Func &&F)
  83. -> std::shared_future<decltype(F())> {
  84. return asyncImpl(std::function<decltype(F())()>(std::forward<Func>(F)),
  85. &Group);
  86. }
  87. /// Blocking wait for all the threads to complete and the queue to be empty.
  88. /// It is an error to try to add new tasks while blocking on this call.
  89. /// Calling wait() from a task would deadlock waiting for itself.
  90. void wait();
  91. /// Blocking wait for only all the threads in the given group to complete.
  92. /// It is possible to wait even inside a task, but waiting (directly or
  93. /// indirectly) on itself will deadlock. If called from a task running on a
  94. /// worker thread, the call may process pending tasks while waiting in order
  95. /// not to waste the thread.
  96. void wait(ThreadPoolTaskGroup &Group);
  97. // TODO: misleading legacy name warning!
  98. // Returns the maximum number of worker threads in the pool, not the current
  99. // number of threads!
  100. unsigned getThreadCount() const { return MaxThreadCount; }
  101. /// Returns true if the current thread is a worker thread of this thread pool.
  102. bool isWorkerThread() const;
  103. private:
  104. /// Helpers to create a promise and a callable wrapper of \p Task that sets
  105. /// the result of the promise. Returns the callable and a future to access the
  106. /// result.
  107. template <typename ResTy>
  108. static std::pair<std::function<void()>, std::future<ResTy>>
  109. createTaskAndFuture(std::function<ResTy()> Task) {
  110. std::shared_ptr<std::promise<ResTy>> Promise =
  111. std::make_shared<std::promise<ResTy>>();
  112. auto F = Promise->get_future();
  113. return {
  114. [Promise = std::move(Promise), Task]() { Promise->set_value(Task()); },
  115. std::move(F)};
  116. }
  117. static std::pair<std::function<void()>, std::future<void>>
  118. createTaskAndFuture(std::function<void()> Task) {
  119. std::shared_ptr<std::promise<void>> Promise =
  120. std::make_shared<std::promise<void>>();
  121. auto F = Promise->get_future();
  122. return {[Promise = std::move(Promise), Task]() {
  123. Task();
  124. Promise->set_value();
  125. },
  126. std::move(F)};
  127. }
  128. /// Returns true if all tasks in the given group have finished (nullptr means
  129. /// all tasks regardless of their group). QueueLock must be locked.
  130. bool workCompletedUnlocked(ThreadPoolTaskGroup *Group) const;
  131. /// Asynchronous submission of a task to the pool. The returned future can be
  132. /// used to wait for the task to finish and is *non-blocking* on destruction.
  133. template <typename ResTy>
  134. std::shared_future<ResTy> asyncImpl(std::function<ResTy()> Task,
  135. ThreadPoolTaskGroup *Group) {
  136. #if LLVM_ENABLE_THREADS
  137. /// Wrap the Task in a std::function<void()> that sets the result of the
  138. /// corresponding future.
  139. auto R = createTaskAndFuture(Task);
  140. int requestedThreads;
  141. {
  142. // Lock the queue and push the new task
  143. std::unique_lock<std::mutex> LockGuard(QueueLock);
  144. // Don't allow enqueueing after disabling the pool
  145. assert(EnableFlag && "Queuing a thread during ThreadPool destruction");
  146. Tasks.emplace_back(std::make_pair(std::move(R.first), Group));
  147. requestedThreads = ActiveThreads + Tasks.size();
  148. }
  149. QueueCondition.notify_one();
  150. grow(requestedThreads);
  151. return R.second.share();
  152. #else // LLVM_ENABLE_THREADS Disabled
  153. // Get a Future with launch::deferred execution using std::async
  154. auto Future = std::async(std::launch::deferred, std::move(Task)).share();
  155. // Wrap the future so that both ThreadPool::wait() can operate and the
  156. // returned future can be sync'ed on.
  157. Tasks.emplace_back(std::make_pair([Future]() { Future.get(); }, Group));
  158. return Future;
  159. #endif
  160. }
  161. #if LLVM_ENABLE_THREADS
  162. // Grow to ensure that we have at least `requested` Threads, but do not go
  163. // over MaxThreadCount.
  164. void grow(int requested);
  165. void processTasks(ThreadPoolTaskGroup *WaitingForGroup);
  166. #endif
  167. /// Threads in flight
  168. std::vector<llvm::thread> Threads;
  169. /// Lock protecting access to the Threads vector.
  170. mutable llvm::sys::RWMutex ThreadsLock;
  171. /// Tasks waiting for execution in the pool.
  172. std::deque<std::pair<std::function<void()>, ThreadPoolTaskGroup *>> Tasks;
  173. /// Locking and signaling for accessing the Tasks queue.
  174. std::mutex QueueLock;
  175. std::condition_variable QueueCondition;
  176. /// Signaling for job completion (all tasks or all tasks in a group).
  177. std::condition_variable CompletionCondition;
  178. /// Keep track of the number of thread actually busy
  179. unsigned ActiveThreads = 0;
  180. /// Number of threads active for tasks in the given group (only non-zero).
  181. DenseMap<ThreadPoolTaskGroup *, unsigned> ActiveGroups;
  182. #if LLVM_ENABLE_THREADS // avoids warning for unused variable
  183. /// Signal for the destruction of the pool, asking thread to exit.
  184. bool EnableFlag = true;
  185. #endif
  186. const ThreadPoolStrategy Strategy;
  187. /// Maximum number of threads to potentially grow this pool to.
  188. const unsigned MaxThreadCount;
  189. };
  190. /// A group of tasks to be run on a thread pool. Thread pool tasks in different
  191. /// groups can run on the same threadpool but can be waited for separately.
  192. /// It is even possible for tasks of one group to submit and wait for tasks
  193. /// of another group, as long as this does not form a loop.
  194. class ThreadPoolTaskGroup {
  195. public:
  196. /// The ThreadPool argument is the thread pool to forward calls to.
  197. ThreadPoolTaskGroup(ThreadPool &Pool) : Pool(Pool) {}
  198. /// Blocking destructor: will wait for all the tasks in the group to complete
  199. /// by calling ThreadPool::wait().
  200. ~ThreadPoolTaskGroup() { wait(); }
  201. /// Calls ThreadPool::async() for this group.
  202. template <typename Function, typename... Args>
  203. inline auto async(Function &&F, Args &&...ArgList) {
  204. return Pool.async(*this, std::forward<Function>(F),
  205. std::forward<Args>(ArgList)...);
  206. }
  207. /// Calls ThreadPool::wait() for this group.
  208. void wait() { Pool.wait(*this); }
  209. private:
  210. ThreadPool &Pool;
  211. };
  212. } // namespace llvm
  213. #endif // LLVM_SUPPORT_THREADPOOL_H
  214. #ifdef __GNUC__
  215. #pragma GCC diagnostic pop
  216. #endif