aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/ThreadPool.cpp
diff options
context:
space:
mode:
authorFangrui Song <maskray@google.com>2020-04-24 20:50:06 -0700
committerFangrui Song <maskray@google.com>2020-04-28 12:20:42 -0700
commit6f230491197253be7c19b53c80be3e4a5835bc72 (patch)
treee2f74e060cc83e8baa27c6ffa5be1f6d1cc3c1f9 /llvm/lib/Support/ThreadPool.cpp
parent03ffe5860549e97a0f2d8262b100e8910f61c528 (diff)
downloadllvm-6f230491197253be7c19b53c80be3e4a5835bc72.zip
llvm-6f230491197253be7c19b53c80be3e4a5835bc72.tar.gz
llvm-6f230491197253be7c19b53c80be3e4a5835bc72.tar.bz2
[Support] Simplify and optimize ThreadPool
* Merge QueueLock and CompletionLock. * Avoid spurious CompletionCondition.notify_all() when ActiveThreads is greater than 0. * Use default member initializers. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D78856
Diffstat (limited to 'llvm/lib/Support/ThreadPool.cpp')
-rw-r--r--llvm/lib/Support/ThreadPool.cpp29
1 files changed, 12 insertions, 17 deletions
diff --git a/llvm/lib/Support/ThreadPool.cpp b/llvm/lib/Support/ThreadPool.cpp
index 5aa5815..46a1990 100644
--- a/llvm/lib/Support/ThreadPool.cpp
+++ b/llvm/lib/Support/ThreadPool.cpp
@@ -21,8 +21,7 @@ using namespace llvm;
#if LLVM_ENABLE_THREADS
ThreadPool::ThreadPool(ThreadPoolStrategy S)
- : ActiveThreads(0), EnableFlag(true),
- ThreadCount(S.compute_thread_count()) {
+ : ThreadCount(S.compute_thread_count()) {
// Create ThreadCount threads that will loop forever, wait on QueueCondition
// for tasks to be queued or the Pool to be destroyed.
Threads.reserve(ThreadCount);
@@ -44,24 +43,24 @@ ThreadPool::ThreadPool(ThreadPoolStrategy S)
// We first need to signal that we are active before popping the queue
// in order for wait() to properly detect that even if the queue is
// empty, there is still a task in flight.
- {
- std::unique_lock<std::mutex> LockGuard(CompletionLock);
- ++ActiveThreads;
- }
+ ++ActiveThreads;
Task = std::move(Tasks.front());
Tasks.pop();
}
// Run the task we just grabbed
Task();
+ bool Notify;
{
// Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait()
- std::unique_lock<std::mutex> LockGuard(CompletionLock);
+ std::lock_guard<std::mutex> LockGuard(QueueLock);
--ActiveThreads;
+ Notify = workCompletedUnlocked();
}
-
- // Notify task completion, in case someone waits on ThreadPool::wait()
- CompletionCondition.notify_all();
+ // Notify task completion if this is the last active thread, in case
+ // someone waits on ThreadPool::wait().
+ if (Notify)
+ CompletionCondition.notify_all();
}
});
}
@@ -69,12 +68,8 @@ ThreadPool::ThreadPool(ThreadPoolStrategy S)
void ThreadPool::wait() {
// Wait for all threads to complete and the queue to be empty
- std::unique_lock<std::mutex> LockGuard(CompletionLock);
- // The order of the checks for ActiveThreads and Tasks.empty() matters because
- // any active threads might be modifying the Tasks queue, and this would be a
- // race.
- CompletionCondition.wait(LockGuard,
- [&] { return !ActiveThreads && Tasks.empty(); });
+ std::unique_lock<std::mutex> LockGuard(QueueLock);
+ CompletionCondition.wait(LockGuard, [&] { return workCompletedUnlocked(); });
}
std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
@@ -109,7 +104,7 @@ ThreadPool::~ThreadPool() {
// No threads are launched, issue a warning if ThreadCount is not 0
ThreadPool::ThreadPool(ThreadPoolStrategy S)
- : ActiveThreads(0), ThreadCount(S.compute_thread_count()) {
+ : ThreadCount(S.compute_thread_count()) {
if (ThreadCount != 1) {
errs() << "Warning: request a ThreadPool with " << ThreadCount
<< " threads, but LLVM_ENABLE_THREADS has been turned off\n";