diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2024-07-27 23:34:35 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-10-23 18:26:01 +0000 |
commit | 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583 (patch) | |
tree | 6cf5ab1f05330c6773b1f3f64799d56a9c7a1faa /contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp | |
parent | 6b9f7133aba44189d9625c352bc2c2a59baf18ef (diff) | |
parent | ac9a064cb179f3425b310fa2847f8764ac970a4d (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp b/contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp index 4eef339000e1..27e0f220ac4e 100644 --- a/contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp +++ b/contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp @@ -14,16 +14,13 @@ #include "llvm/Config/llvm-config.h" -#if LLVM_ENABLE_THREADS #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Threading.h" -#else #include "llvm/Support/raw_ostream.h" -#endif using namespace llvm; -#if LLVM_ENABLE_THREADS +ThreadPoolInterface::~ThreadPoolInterface() = default; // A note on thread groups: Tasks are by default in no group (represented // by nullptr ThreadPoolTaskGroup pointer in the Tasks queue) and functionality @@ -33,10 +30,12 @@ using namespace llvm; // queue, and functions called to work only on tasks from one group take that // pointer. -ThreadPool::ThreadPool(ThreadPoolStrategy S) +#if LLVM_ENABLE_THREADS + +StdThreadPool::StdThreadPool(ThreadPoolStrategy S) : Strategy(S), MaxThreadCount(S.compute_thread_count()) {} -void ThreadPool::grow(int requested) { +void StdThreadPool::grow(int requested) { llvm::sys::ScopedWriter LockGuard(ThreadsLock); if (Threads.size() >= MaxThreadCount) return; // Already hit the max thread pool size. @@ -58,7 +57,7 @@ static LLVM_THREAD_LOCAL std::vector<ThreadPoolTaskGroup *> #endif // WaitingForGroup == nullptr means all tasks regardless of their group. -void ThreadPool::processTasks(ThreadPoolTaskGroup *WaitingForGroup) { +void StdThreadPool::processTasks(ThreadPoolTaskGroup *WaitingForGroup) { while (true) { std::function<void()> Task; ThreadPoolTaskGroup *GroupOfTask; @@ -111,7 +110,7 @@ void ThreadPool::processTasks(ThreadPoolTaskGroup *WaitingForGroup) { bool Notify; bool NotifyGroup; { - // Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait() + // Adjust `ActiveThreads`, in case someone waits on StdThreadPool::wait() std::lock_guard<std::mutex> LockGuard(QueueLock); --ActiveThreads; if (GroupOfTask != nullptr) { @@ -123,7 +122,7 @@ void ThreadPool::processTasks(ThreadPoolTaskGroup *WaitingForGroup) { NotifyGroup = GroupOfTask != nullptr && Notify; } // Notify task completion if this is the last active thread, in case - // someone waits on ThreadPool::wait(). + // someone waits on StdThreadPool::wait(). if (Notify) CompletionCondition.notify_all(); // If this was a task in a group, notify also threads waiting for tasks @@ -134,7 +133,7 @@ void ThreadPool::processTasks(ThreadPoolTaskGroup *WaitingForGroup) { } } -bool ThreadPool::workCompletedUnlocked(ThreadPoolTaskGroup *Group) const { +bool StdThreadPool::workCompletedUnlocked(ThreadPoolTaskGroup *Group) const { if (Group == nullptr) return !ActiveThreads && Tasks.empty(); return ActiveGroups.count(Group) == 0 && @@ -142,7 +141,7 @@ bool ThreadPool::workCompletedUnlocked(ThreadPoolTaskGroup *Group) const { [Group](const auto &T) { return T.second == Group; }); } -void ThreadPool::wait() { +void StdThreadPool::wait() { assert(!isWorkerThread()); // Would deadlock waiting for itself. // Wait for all threads to complete and the queue to be empty std::unique_lock<std::mutex> LockGuard(QueueLock); @@ -150,7 +149,7 @@ void ThreadPool::wait() { [&] { return workCompletedUnlocked(nullptr); }); } -void ThreadPool::wait(ThreadPoolTaskGroup &Group) { +void StdThreadPool::wait(ThreadPoolTaskGroup &Group) { // Wait for all threads in the group to complete. if (!isWorkerThread()) { std::unique_lock<std::mutex> LockGuard(QueueLock); @@ -167,7 +166,7 @@ void ThreadPool::wait(ThreadPoolTaskGroup &Group) { processTasks(&Group); } -bool ThreadPool::isWorkerThread() const { +bool StdThreadPool::isWorkerThread() const { llvm::sys::ScopedReader LockGuard(ThreadsLock); llvm::thread::id CurrentThreadId = llvm::this_thread::get_id(); for (const llvm::thread &Thread : Threads) @@ -177,7 +176,7 @@ bool ThreadPool::isWorkerThread() const { } // The destructor joins all threads, waiting for completion. -ThreadPool::~ThreadPool() { +StdThreadPool::~StdThreadPool() { { std::unique_lock<std::mutex> LockGuard(QueueLock); EnableFlag = false; @@ -188,10 +187,10 @@ ThreadPool::~ThreadPool() { Worker.join(); } -#else // LLVM_ENABLE_THREADS Disabled +#endif // LLVM_ENABLE_THREADS Disabled // No threads are launched, issue a warning if ThreadCount is not 0 -ThreadPool::ThreadPool(ThreadPoolStrategy S) : MaxThreadCount(1) { +SingleThreadExecutor::SingleThreadExecutor(ThreadPoolStrategy S) { int ThreadCount = S.compute_thread_count(); if (ThreadCount != 1) { errs() << "Warning: request a ThreadPool with " << ThreadCount @@ -199,7 +198,7 @@ ThreadPool::ThreadPool(ThreadPoolStrategy S) : MaxThreadCount(1) { } } -void ThreadPool::wait() { +void SingleThreadExecutor::wait() { // Sequential implementation running the tasks while (!Tasks.empty()) { auto Task = std::move(Tasks.front().first); @@ -208,16 +207,14 @@ void ThreadPool::wait() { } } -void ThreadPool::wait(ThreadPoolTaskGroup &) { +void SingleThreadExecutor::wait(ThreadPoolTaskGroup &) { // Simply wait for all, this works even if recursive (the running task // is already removed from the queue). wait(); } -bool ThreadPool::isWorkerThread() const { +bool SingleThreadExecutor::isWorkerThread() const { report_fatal_error("LLVM compiled without multithreading"); } -ThreadPool::~ThreadPool() { wait(); } - -#endif +SingleThreadExecutor::~SingleThreadExecutor() { wait(); } |