16 #include "llvm/Config/llvm-config.h"
21 #if LLVM_ENABLE_THREADS
27 : ActiveThreads(0), EnableFlag(
true) {
30 Threads.reserve(ThreadCount);
31 for (
unsigned ThreadID = 0; ThreadID < ThreadCount; ++ThreadID) {
32 Threads.emplace_back([&] {
36 std::unique_lock<std::mutex> LockGuard(QueueLock);
38 QueueCondition.wait(LockGuard,
39 [&] {
return !EnableFlag || !Tasks.empty(); });
41 if (!EnableFlag && Tasks.empty())
50 std::unique_lock<std::mutex> LockGuard(CompletionLock);
52 Task = std::move(Tasks.front());
60 std::unique_lock<std::mutex> LockGuard(CompletionLock);
65 CompletionCondition.notify_all();
73 std::unique_lock<std::mutex> LockGuard(CompletionLock);
77 CompletionCondition.wait(LockGuard,
78 [&] {
return !ActiveThreads && Tasks.empty(); });
81 std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
84 auto Future = PackagedTask.get_future();
87 std::unique_lock<std::mutex> LockGuard(QueueLock);
90 assert(EnableFlag &&
"Queuing a thread during ThreadPool destruction");
92 Tasks.push(std::move(PackagedTask));
94 QueueCondition.notify_one();
95 return Future.share();
101 std::unique_lock<std::mutex> LockGuard(QueueLock);
104 QueueCondition.notify_all();
105 for (
auto &Worker : Threads)
109 #else // LLVM_ENABLE_THREADS Disabled
117 errs() <<
"Warning: request a ThreadPool with " << ThreadCount
118 <<
" threads, but LLVM_ENABLE_THREADS has been turned off\n";
124 while (!Tasks.empty()) {
125 auto Task = std::move(Tasks.front());
131 std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
133 auto Future = std::async(std::launch::deferred, std::move(Task)).share();
137 Tasks.push(std::move(PackagedTask));
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::packaged_task< void()> PackagedTaskTy
~ThreadPool()
Blocking destructor: the pool will wait for all the threads to complete.
A ThreadPool for asynchronous parallel execution on a defined number of threads.
ThreadPool()
Construct a pool with the number of core available on the system (or whatever the value returned by s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void wait()
Blocking wait for all the threads to complete and the queue to be empty.