Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- llvm/Support/ThreadPool.h - A ThreadPool implementation -*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines a crude C++11 based thread pool. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_THREAD_POOL_H |
| 14 | #define LLVM_SUPPORT_THREAD_POOL_H |
| 15 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 16 | #include "llvm/Config/llvm-config.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 17 | #include "llvm/Support/Threading.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | #include "llvm/Support/thread.h" |
| 19 | |
| 20 | #include <future> |
| 21 | |
| 22 | #include <atomic> |
| 23 | #include <condition_variable> |
| 24 | #include <functional> |
| 25 | #include <memory> |
| 26 | #include <mutex> |
| 27 | #include <queue> |
| 28 | #include <utility> |
| 29 | |
| 30 | namespace llvm { |
| 31 | |
| 32 | /// A ThreadPool for asynchronous parallel execution on a defined number of |
| 33 | /// threads. |
| 34 | /// |
| 35 | /// The pool keeps a vector of threads alive, waiting on a condition variable |
| 36 | /// for some work to become available. |
| 37 | class ThreadPool { |
| 38 | public: |
| 39 | using TaskTy = std::function<void()>; |
| 40 | using PackagedTaskTy = std::packaged_task<void()>; |
| 41 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 42 | /// Construct a pool using the hardware strategy \p S for mapping hardware |
| 43 | /// execution resources (threads, cores, CPUs) |
| 44 | /// Defaults to using the maximum execution resources in the system, but |
| 45 | /// accounting for the affinity mask. |
| 46 | ThreadPool(ThreadPoolStrategy S = hardware_concurrency()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | |
| 48 | /// Blocking destructor: the pool will wait for all the threads to complete. |
| 49 | ~ThreadPool(); |
| 50 | |
| 51 | /// Asynchronous submission of a task to the pool. The returned future can be |
| 52 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
| 53 | template <typename Function, typename... Args> |
| 54 | inline std::shared_future<void> async(Function &&F, Args &&... ArgList) { |
| 55 | auto Task = |
| 56 | std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...); |
| 57 | return asyncImpl(std::move(Task)); |
| 58 | } |
| 59 | |
| 60 | /// Asynchronous submission of a task to the pool. The returned future can be |
| 61 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
| 62 | template <typename Function> |
| 63 | inline std::shared_future<void> async(Function &&F) { |
| 64 | return asyncImpl(std::forward<Function>(F)); |
| 65 | } |
| 66 | |
| 67 | /// Blocking wait for all the threads to complete and the queue to be empty. |
| 68 | /// It is an error to try to add new tasks while blocking on this call. |
| 69 | void wait(); |
| 70 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 71 | unsigned getThreadCount() const { return ThreadCount; } |
| 72 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 74 | bool workCompletedUnlocked() { return !ActiveThreads && Tasks.empty(); } |
| 75 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | /// Asynchronous submission of a task to the pool. The returned future can be |
| 77 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
| 78 | std::shared_future<void> asyncImpl(TaskTy F); |
| 79 | |
| 80 | /// Threads in flight |
| 81 | std::vector<llvm::thread> Threads; |
| 82 | |
| 83 | /// Tasks waiting for execution in the pool. |
| 84 | std::queue<PackagedTaskTy> Tasks; |
| 85 | |
| 86 | /// Locking and signaling for accessing the Tasks queue. |
| 87 | std::mutex QueueLock; |
| 88 | std::condition_variable QueueCondition; |
| 89 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 90 | /// Signaling for job completion |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 91 | std::condition_variable CompletionCondition; |
| 92 | |
| 93 | /// Keep track of the number of thread actually busy |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 94 | unsigned ActiveThreads = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 95 | |
| 96 | #if LLVM_ENABLE_THREADS // avoids warning for unused variable |
| 97 | /// Signal for the destruction of the pool, asking thread to exit. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 98 | bool EnableFlag = true; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | #endif |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 100 | |
| 101 | unsigned ThreadCount; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | }; |
| 103 | } |
| 104 | |
| 105 | #endif // LLVM_SUPPORT_THREAD_POOL_H |