15#include "llvm/Config/llvm-config.h"
33#if LLVM_ENABLE_THREADS
36 : Strategy(S), MaxThreadCount(S.compute_thread_count()) {}
38void StdThreadPool::grow(
int requested) {
40 if (Threads.size() >= MaxThreadCount)
42 int newThreadCount = std::min<int>(requested, MaxThreadCount);
43 while (
static_cast<int>(Threads.size()) < newThreadCount) {
44 int ThreadID = Threads.size();
45 Threads.emplace_back([
this, ThreadID] {
47 Strategy.apply_thread_strategy(ThreadID);
48 processTasks(
nullptr);
56 *CurrentThreadTaskGroups =
nullptr;
62 std::function<void()> Task;
65 std::unique_lock<std::mutex> LockGuard(QueueLock);
66 bool workCompletedForGroup =
false;
68 QueueCondition.wait(LockGuard, [&] {
69 return !EnableFlag || !Tasks.empty() ||
70 (WaitingForGroup !=
nullptr &&
71 (workCompletedForGroup =
72 workCompletedUnlocked(WaitingForGroup)));
75 if (!EnableFlag && Tasks.empty())
77 if (WaitingForGroup !=
nullptr && workCompletedForGroup)
85 Task = std::move(Tasks.front().first);
86 GroupOfTask = Tasks.front().second;
89 if (GroupOfTask !=
nullptr)
90 ++ActiveGroups[GroupOfTask];
94 if (CurrentThreadTaskGroups ==
nullptr)
95 CurrentThreadTaskGroups =
new std::vector<ThreadPoolTaskGroup *>;
96 CurrentThreadTaskGroups->push_back(GroupOfTask);
103 CurrentThreadTaskGroups->pop_back();
104 if (CurrentThreadTaskGroups->empty()) {
105 delete CurrentThreadTaskGroups;
106 CurrentThreadTaskGroups =
nullptr;
114 std::lock_guard<std::mutex> LockGuard(QueueLock);
116 if (GroupOfTask !=
nullptr) {
117 auto A = ActiveGroups.find(GroupOfTask);
118 if (--(
A->second) == 0)
119 ActiveGroups.erase(
A);
121 Notify = workCompletedUnlocked(GroupOfTask);
122 NotifyGroup = GroupOfTask !=
nullptr && Notify;
127 CompletionCondition.notify_all();
132 QueueCondition.notify_all();
137 if (Group ==
nullptr)
138 return !ActiveThreads && Tasks.empty();
139 return ActiveGroups.count(Group) == 0 &&
143void StdThreadPool::wait() {
144 assert(!isWorkerThread());
146 std::unique_lock<std::mutex> LockGuard(QueueLock);
147 CompletionCondition.wait(LockGuard,
148 [&] {
return workCompletedUnlocked(
nullptr); });
153 if (!isWorkerThread()) {
154 std::unique_lock<std::mutex> LockGuard(QueueLock);
155 CompletionCondition.wait(LockGuard,
156 [&] {
return workCompletedUnlocked(&Group); });
160 assert(CurrentThreadTaskGroups ==
nullptr ||
165 processTasks(&Group);
168bool StdThreadPool::isWorkerThread()
const {
170 llvm::thread::id CurrentThreadId = llvm::this_thread::get_id();
172 if (CurrentThreadId ==
Thread.get_id())
178StdThreadPool::~StdThreadPool() {
180 std::unique_lock<std::mutex> LockGuard(QueueLock);
183 QueueCondition.notify_all();
185 for (
auto &Worker : Threads)
196 <<
" threads, but LLVM_ENABLE_THREADS has been turned off\n";
202 while (!Tasks.empty()) {
203 auto Task = std::move(Tasks.front().first);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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,...
static cl::opt< int > ThreadCount("threads", cl::init(0))
SingleThreadExecutor(ThreadPoolStrategy ignored={})
Construct a non-threaded pool, ignoring using the hardware strategy.
void wait() override
Blocking wait for all the tasks to execute first.
~SingleThreadExecutor() override
Blocking destructor: the pool will first execute the pending tasks.
bool isWorkerThread() const
Returns true if the current thread is a worker thread of this thread pool.
virtual ~ThreadPoolInterface()
Destroying the pool will drain the pending tasks and wait.
This tells how a thread pool will be used.
LLVM_ABI 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.
SmartScopedReader< false > ScopedReader
SmartScopedWriter< false > ScopedWriter
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI void set_thread_name(const Twine &Name)
Set the name of the current thread.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.