16 #include "llvm/Config/llvm-config.h" 22 #if LLVM_ENABLE_THREADS 28 : ActiveThreads(0), EnableFlag(
true) {
31 Threads.reserve(ThreadCount);
32 for (
unsigned ThreadID = 0; ThreadID < ThreadCount; ++ThreadID) {
33 Threads.emplace_back([&] {
37 std::unique_lock<std::mutex> LockGuard(QueueLock);
39 QueueCondition.wait(LockGuard,
40 [&] {
return !EnableFlag || !Tasks.empty(); });
42 if (!EnableFlag && Tasks.empty())
50 std::unique_lock<std::mutex> LockGuard(CompletionLock);
53 Task = std::move(Tasks.front());
61 std::unique_lock<std::mutex> LockGuard(CompletionLock);
66 CompletionCondition.notify_all();
74 std::unique_lock<std::mutex> LockGuard(CompletionLock);
78 CompletionCondition.wait(LockGuard,
79 [&] {
return !ActiveThreads && Tasks.empty(); });
82 std::shared_future<void> ThreadPool::asyncImpl(
TaskTy Task) {
85 auto Future = PackagedTask.get_future();
88 std::unique_lock<std::mutex> LockGuard(QueueLock);
91 assert(EnableFlag &&
"Queuing a thread during ThreadPool destruction");
93 Tasks.push(std::move(PackagedTask));
95 QueueCondition.notify_one();
96 return Future.share();
102 std::unique_lock<std::mutex> LockGuard(QueueLock);
105 QueueCondition.notify_all();
106 for (
auto &Worker : Threads)
110 #else // LLVM_ENABLE_THREADS Disabled 118 errs() <<
"Warning: request a ThreadPool with " << ThreadCount
119 <<
" threads, but LLVM_ENABLE_THREADS has been turned off\n";
125 while (!Tasks.empty()) {
126 auto Task = std::move(Tasks.front());
132 std::shared_future<void> ThreadPool::asyncImpl(
TaskTy Task) {
134 auto Future = std::async(std::launch::deferred, std::move(Task)).share();
138 Tasks.push(std::move(PackagedTask));
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
std::packaged_task< void()> PackagedTaskTy
std::function< void()> TaskTy
block Block Frequency true
~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 threads found by hardware_concurrency().
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned hardware_concurrency()
Get the number of threads that the current program can execute concurrently.
void wait()
Blocking wait for all the threads to complete and the queue to be empty.