15#include "llvm/Config/llvm-config.h"
17#if LLVM_ENABLE_THREADS
26#if LLVM_ENABLE_THREADS
37 : Strategy(S), MaxThreadCount(S.compute_thread_count()) {}
39void ThreadPool::grow(
int requested) {
41 if (Threads.size() >= MaxThreadCount)
43 int newThreadCount = std::min<int>(requested, MaxThreadCount);
44 while (
static_cast<int>(Threads.size()) < newThreadCount) {
45 int ThreadID = Threads.size();
46 Threads.emplace_back([
this, ThreadID] {
49 processTasks(
nullptr);
57 *CurrentThreadTaskGroups =
nullptr;
63 std::function<void()> Task;
66 std::unique_lock<std::mutex> LockGuard(QueueLock);
67 bool workCompletedForGroup =
false;
69 QueueCondition.wait(LockGuard, [&] {
70 return !EnableFlag || !Tasks.empty() ||
71 (WaitingForGroup !=
nullptr &&
72 (workCompletedForGroup =
73 workCompletedUnlocked(WaitingForGroup)));
76 if (!EnableFlag && Tasks.empty())
78 if (WaitingForGroup !=
nullptr && workCompletedForGroup)
86 Task = std::move(Tasks.front().first);
87 GroupOfTask = Tasks.front().second;
90 if (GroupOfTask !=
nullptr)
91 ++ActiveGroups[GroupOfTask];
95 if (CurrentThreadTaskGroups ==
nullptr)
96 CurrentThreadTaskGroups =
new std::vector<ThreadPoolTaskGroup *>;
97 CurrentThreadTaskGroups->push_back(GroupOfTask);
104 CurrentThreadTaskGroups->pop_back();
105 if (CurrentThreadTaskGroups->empty()) {
106 delete CurrentThreadTaskGroups;
107 CurrentThreadTaskGroups =
nullptr;
115 std::lock_guard<std::mutex> LockGuard(QueueLock);
117 if (GroupOfTask !=
nullptr) {
118 auto A = ActiveGroups.find(GroupOfTask);
119 if (--(
A->second) == 0)
120 ActiveGroups.erase(
A);
122 Notify = workCompletedUnlocked(GroupOfTask);
123 NotifyGroup = GroupOfTask !=
nullptr && Notify;
128 CompletionCondition.notify_all();
133 QueueCondition.notify_all();
138 if (Group ==
nullptr)
139 return !ActiveThreads && Tasks.empty();
140 return ActiveGroups.count(Group) == 0 &&
142 [Group](
const auto &
T) {
return T.second == Group; });
148 std::unique_lock<std::mutex> LockGuard(QueueLock);
149 CompletionCondition.wait(LockGuard,
150 [&] {
return workCompletedUnlocked(
nullptr); });
156 std::unique_lock<std::mutex> LockGuard(QueueLock);
157 CompletionCondition.wait(LockGuard,
158 [&] {
return workCompletedUnlocked(&Group); });
162 assert(CurrentThreadTaskGroups ==
nullptr ||
167 processTasks(&Group);
172 llvm::thread::id CurrentThreadId = llvm::this_thread::get_id();
174 if (CurrentThreadId ==
Thread.get_id())
182 std::unique_lock<std::mutex> LockGuard(QueueLock);
185 QueueCondition.notify_all();
187 for (
auto &Worker : Threads)
196 if (ThreadCount != 1) {
197 errs() <<
"Warning: request a ThreadPool with " << ThreadCount
198 <<
" threads, but LLVM_ENABLE_THREADS has been turned off\n";
204 while (!Tasks.empty()) {
205 auto Task = std::move(Tasks.front().first);
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_THREAD_LOCAL
\macro LLVM_THREAD_LOCAL A thread-local storage specifier which can be used with globals,...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This tells how a thread pool will be used.
void apply_thread_strategy(unsigned ThreadPoolNum) const
Assign the current thread to an ideal hardware CPU or NUMA node.
unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
A group of tasks to be run on a thread pool.
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
~ThreadPool()
Blocking destructor: the pool will wait for all the threads to complete.
bool isWorkerThread() const
Returns true if the current thread is a worker thread of this thread pool.
ThreadPool(ThreadPoolStrategy S=hardware_concurrency())
Construct a pool using the hardware strategy S for mapping hardware execution resources (threads,...
SmartScopedReader< false > ScopedReader
SmartScopedWriter< false > ScopedWriter
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
void set_thread_name(const Twine &Name)
Set the name of the current thread.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.