diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2020-07-26 19:36:28 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2020-07-26 19:36:28 +0000 |
commit | cfca06d7963fa0909f90483b42a6d7d194d01e08 (patch) | |
tree | 209fb2a2d68f8f277793fc8df46c753d31bc853b /llvm/lib/Support/ThreadPool.cpp | |
parent | 706b4fc47bbc608932d3b491ae19a3b9cde9497b (diff) |
Notes
Diffstat (limited to 'llvm/lib/Support/ThreadPool.cpp')
-rw-r--r-- | llvm/lib/Support/ThreadPool.cpp | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/llvm/lib/Support/ThreadPool.cpp b/llvm/lib/Support/ThreadPool.cpp index 40982d777914..46a1990cd719 100644 --- a/llvm/lib/Support/ThreadPool.cpp +++ b/llvm/lib/Support/ThreadPool.cpp @@ -20,16 +20,14 @@ using namespace llvm; #if LLVM_ENABLE_THREADS -// Default to hardware_concurrency -ThreadPool::ThreadPool() : ThreadPool(hardware_concurrency()) {} - -ThreadPool::ThreadPool(unsigned ThreadCount) - : ActiveThreads(0), EnableFlag(true) { +ThreadPool::ThreadPool(ThreadPoolStrategy S) + : 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); for (unsigned ThreadID = 0; ThreadID < ThreadCount; ++ThreadID) { - Threads.emplace_back([&] { + Threads.emplace_back([S, ThreadID, this] { + S.apply_thread_strategy(ThreadID); while (true) { PackagedTaskTy Task; { @@ -45,24 +43,24 @@ ThreadPool::ThreadPool(unsigned ThreadCount) // 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(); } }); } @@ -70,12 +68,8 @@ ThreadPool::ThreadPool(unsigned ThreadCount) 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) { @@ -108,12 +102,10 @@ ThreadPool::~ThreadPool() { #else // LLVM_ENABLE_THREADS Disabled -ThreadPool::ThreadPool() : ThreadPool(0) {} - // No threads are launched, issue a warning if ThreadCount is not 0 -ThreadPool::ThreadPool(unsigned ThreadCount) - : ActiveThreads(0) { - if (ThreadCount) { +ThreadPool::ThreadPool(ThreadPoolStrategy S) + : 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"; } @@ -138,8 +130,6 @@ std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) { return Future; } -ThreadPool::~ThreadPool() { - wait(); -} +ThreadPool::~ThreadPool() { wait(); } #endif |