LLVM 22.0.0git
ThreadPool.cpp
Go to the documentation of this file.
1//==-- llvm/Support/ThreadPool.cpp - A ThreadPool implementation -*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//
10// This file implements a crude C++11 based thread pool.
11//
12//===----------------------------------------------------------------------===//
13
15
16#include "llvm/Config/llvm-config.h"
17
18#include "llvm/ADT/ScopeExit.h"
23
24using namespace llvm;
25
27
28// A note on thread groups: Tasks are by default in no group (represented
29// by nullptr ThreadPoolTaskGroup pointer in the Tasks queue) and functionality
30// here normally works on all tasks regardless of their group (functions
31// in that case receive nullptr ThreadPoolTaskGroup pointer as argument).
32// A task in a group has a pointer to that ThreadPoolTaskGroup in the Tasks
33// queue, and functions called to work only on tasks from one group take that
34// pointer.
35
36#if LLVM_ENABLE_THREADS
37
38StdThreadPool::StdThreadPool(ThreadPoolStrategy S)
39 : Strategy(S), MaxThreadCount(S.compute_thread_count()) {
40 if (Strategy.UseJobserver)
41 TheJobserver = JobserverClient::getInstance();
42}
43
44void StdThreadPool::grow(int requested) {
45 llvm::sys::ScopedWriter LockGuard(ThreadsLock);
46 if (Threads.size() >= MaxThreadCount)
47 return; // Already hit the max thread pool size.
48 int newThreadCount = std::min<int>(requested, MaxThreadCount);
49 while (static_cast<int>(Threads.size()) < newThreadCount) {
50 int ThreadID = Threads.size();
51 Threads.emplace_back([this, ThreadID] {
52 set_thread_name(formatv("llvm-worker-{0}", ThreadID));
53 Strategy.apply_thread_strategy(ThreadID);
54 // Note on jobserver deadlock avoidance:
55 // GNU Make grants each invoked process one implicit job slot.
56 // JobserverClient::tryAcquire() returns that implicit slot on the first
57 // successful call in a process, ensuring forward progress without a
58 // dedicated "always-on" thread.
59 if (TheJobserver)
60 processTasksWithJobserver();
61 else
62 processTasks(nullptr);
63 });
64 }
65}
66
67#ifndef NDEBUG
68// The group of the tasks run by the current thread.
69static LLVM_THREAD_LOCAL std::vector<ThreadPoolTaskGroup *>
70 *CurrentThreadTaskGroups = nullptr;
71#endif
72
73// WaitingForGroup == nullptr means all tasks regardless of their group.
74void StdThreadPool::processTasks(ThreadPoolTaskGroup *WaitingForGroup) {
75 while (true) {
76 std::function<void()> Task;
77 ThreadPoolTaskGroup *GroupOfTask;
78 {
79 std::unique_lock<std::mutex> LockGuard(QueueLock);
80 bool workCompletedForGroup = false; // Result of workCompletedUnlocked()
81 // Wait for tasks to be pushed in the queue
82 QueueCondition.wait(LockGuard, [&] {
83 return !EnableFlag || !Tasks.empty() ||
84 (WaitingForGroup != nullptr &&
85 (workCompletedForGroup =
86 workCompletedUnlocked(WaitingForGroup)));
87 });
88 // Exit condition
89 if (!EnableFlag && Tasks.empty())
90 return;
91 if (WaitingForGroup != nullptr && workCompletedForGroup)
92 return;
93 // Yeah, we have a task, grab it and release the lock on the queue
94
95 // We first need to signal that we are active before popping the queue
96 // in order for wait() to properly detect that even if the queue is
97 // empty, there is still a task in flight.
98 ++ActiveThreads;
99 Task = std::move(Tasks.front().first);
100 GroupOfTask = Tasks.front().second;
101 // Need to count active threads in each group separately, ActiveThreads
102 // would never be 0 if waiting for another group inside a wait.
103 if (GroupOfTask != nullptr)
104 ++ActiveGroups[GroupOfTask]; // Increment or set to 1 if new item
105 Tasks.pop_front();
106 }
107#ifndef NDEBUG
108 if (CurrentThreadTaskGroups == nullptr)
109 CurrentThreadTaskGroups = new std::vector<ThreadPoolTaskGroup *>;
110 CurrentThreadTaskGroups->push_back(GroupOfTask);
111#endif
112
113 // Run the task we just grabbed
114 Task();
115
116#ifndef NDEBUG
117 CurrentThreadTaskGroups->pop_back();
118 if (CurrentThreadTaskGroups->empty()) {
119 delete CurrentThreadTaskGroups;
120 CurrentThreadTaskGroups = nullptr;
121 }
122#endif
123
124 bool Notify;
125 bool NotifyGroup;
126 {
127 // Adjust `ActiveThreads`, in case someone waits on StdThreadPool::wait()
128 std::lock_guard<std::mutex> LockGuard(QueueLock);
129 --ActiveThreads;
130 if (GroupOfTask != nullptr) {
131 auto A = ActiveGroups.find(GroupOfTask);
132 if (--(A->second) == 0)
133 ActiveGroups.erase(A);
134 }
135 Notify = workCompletedUnlocked(GroupOfTask);
136 NotifyGroup = GroupOfTask != nullptr && Notify;
137 }
138 // Notify task completion if this is the last active thread, in case
139 // someone waits on StdThreadPool::wait().
140 if (Notify)
141 CompletionCondition.notify_all();
142 // If this was a task in a group, notify also threads waiting for tasks
143 // in this function on QueueCondition, to make a recursive wait() return
144 // after the group it's been waiting for has finished.
145 if (NotifyGroup)
146 QueueCondition.notify_all();
147 }
148}
149
150/// Main loop for worker threads when using a jobserver.
151/// This function uses a two-level queue; it first acquires a job slot from the
152/// external jobserver, then retrieves a task from the internal queue.
153/// This allows the thread pool to cooperate with build systems like `make -j`.
154void StdThreadPool::processTasksWithJobserver() {
155 while (true) {
156 // Acquire a job slot from the external jobserver.
157 // This polls for a slot and yields the thread to avoid a high-CPU wait.
159 // The timeout for the backoff can be very long, as the shutdown
160 // is checked on each iteration. The sleep duration is capped by MaxWait
161 // in ExponentialBackoff, so shutdown latency is not a problem.
162 ExponentialBackoff Backoff(std::chrono::hours(24));
163 bool AcquiredToken = false;
164 do {
165 // Return if the thread pool is shutting down.
166 {
167 std::unique_lock<std::mutex> LockGuard(QueueLock);
168 if (!EnableFlag)
169 return;
170 }
171
172 Slot = TheJobserver->tryAcquire();
173 if (Slot.isValid()) {
174 AcquiredToken = true;
175 break;
176 }
177 } while (Backoff.waitForNextAttempt());
178
179 if (!AcquiredToken) {
180 // This is practically unreachable with a 24h timeout and indicates a
181 // deeper problem if hit.
182 report_fatal_error("Timed out waiting for jobserver token.");
183 }
184
185 // `make_scope_exit` guarantees the job slot is released, even if the
186 // task throws or we exit early. This prevents deadlocking the build.
187 auto SlotReleaser =
188 make_scope_exit([&] { TheJobserver->release(std::move(Slot)); });
189
190 // While we hold a job slot, process tasks from the internal queue.
191 while (true) {
192 std::function<void()> Task;
193 ThreadPoolTaskGroup *GroupOfTask = nullptr;
194
195 {
196 std::unique_lock<std::mutex> LockGuard(QueueLock);
197
198 // Wait until a task is available or the pool is shutting down.
199 QueueCondition.wait(LockGuard,
200 [&] { return !EnableFlag || !Tasks.empty(); });
201
202 // If shutting down and the queue is empty, the thread can terminate.
203 if (!EnableFlag && Tasks.empty())
204 return;
205
206 // If the queue is empty, we're done processing tasks for now.
207 // Break the inner loop to release the job slot.
208 if (Tasks.empty())
209 break;
210
211 // A task is available. Mark it as active before releasing the lock
212 // to prevent race conditions with `wait()`.
213 ++ActiveThreads;
214 Task = std::move(Tasks.front().first);
215 GroupOfTask = Tasks.front().second;
216 if (GroupOfTask != nullptr)
217 ++ActiveGroups[GroupOfTask];
218 Tasks.pop_front();
219 } // The queue lock is released.
220
221 // Run the task. The job slot remains acquired during execution.
222 Task();
223
224 // The task has finished. Update the active count and notify any waiters.
225 {
226 std::lock_guard<std::mutex> LockGuard(QueueLock);
227 --ActiveThreads;
228 if (GroupOfTask != nullptr) {
229 auto A = ActiveGroups.find(GroupOfTask);
230 if (--(A->second) == 0)
231 ActiveGroups.erase(A);
232 }
233 // If all tasks are complete, notify any waiting threads.
234 if (workCompletedUnlocked(nullptr))
235 CompletionCondition.notify_all();
236 }
237 }
238 }
239}
240bool StdThreadPool::workCompletedUnlocked(ThreadPoolTaskGroup *Group) const {
241 if (Group == nullptr)
242 return !ActiveThreads && Tasks.empty();
243 return ActiveGroups.count(Group) == 0 &&
245}
246
247void StdThreadPool::wait() {
248 assert(!isWorkerThread()); // Would deadlock waiting for itself.
249 // Wait for all threads to complete and the queue to be empty
250 std::unique_lock<std::mutex> LockGuard(QueueLock);
251 CompletionCondition.wait(LockGuard,
252 [&] { return workCompletedUnlocked(nullptr); });
253}
254
255void StdThreadPool::wait(ThreadPoolTaskGroup &Group) {
256 // Wait for all threads in the group to complete.
257 if (!isWorkerThread()) {
258 std::unique_lock<std::mutex> LockGuard(QueueLock);
259 CompletionCondition.wait(LockGuard,
260 [&] { return workCompletedUnlocked(&Group); });
261 return;
262 }
263 // Make sure to not deadlock waiting for oneself.
264 assert(CurrentThreadTaskGroups == nullptr ||
265 !llvm::is_contained(*CurrentThreadTaskGroups, &Group));
266 // Handle the case of recursive call from another task in a different group,
267 // in which case process tasks while waiting to keep the thread busy and avoid
268 // possible deadlock.
269 processTasks(&Group);
270}
271
272bool StdThreadPool::isWorkerThread() const {
273 llvm::sys::ScopedReader LockGuard(ThreadsLock);
274 llvm::thread::id CurrentThreadId = llvm::this_thread::get_id();
275 for (const llvm::thread &Thread : Threads)
276 if (CurrentThreadId == Thread.get_id())
277 return true;
278 return false;
279}
280
281// The destructor joins all threads, waiting for completion.
282StdThreadPool::~StdThreadPool() {
283 {
284 std::unique_lock<std::mutex> LockGuard(QueueLock);
285 EnableFlag = false;
286 }
287 QueueCondition.notify_all();
288 llvm::sys::ScopedReader LockGuard(ThreadsLock);
289 for (auto &Worker : Threads)
290 Worker.join();
291}
292
293#endif // LLVM_ENABLE_THREADS Disabled
294
295// No threads are launched, issue a warning if ThreadCount is not 0
298 if (ThreadCount != 1) {
299 errs() << "Warning: request a ThreadPool with " << ThreadCount
300 << " threads, but LLVM_ENABLE_THREADS has been turned off\n";
301 }
302}
303
305 // Sequential implementation running the tasks
306 while (!Tasks.empty()) {
307 auto Task = std::move(Tasks.front().first);
308 Tasks.pop_front();
309 Task();
310 }
311}
312
314 // Simply wait for all, this works even if recursive (the running task
315 // is already removed from the queue).
316 wait();
317}
318
320 report_fatal_error("LLVM compiled without multithreading");
321}
322
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,...
Definition Compiler.h:679
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
static cl::opt< int > ThreadCount("threads", cl::init(0))
A class to help implement exponential backoff.
A JobSlot represents a single job slot that can be acquired from or released to a jobserver pool.
Definition Jobserver.h:77
static JobserverClient * getInstance()
Returns the singleton instance of the JobserverClient.
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.
Definition Threading.h:115
LLVM_ABI unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
Definition Threading.cpp:42
A group of tasks to be run on a thread pool.
Definition ThreadPool.h:261
SmartScopedReader< false > ScopedReader
Definition RWMutex.h:182
SmartScopedWriter< false > ScopedWriter
Definition RWMutex.h:199
This is an optimization pass for GlobalISel generic memory operations.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
LLVM_ABI void set_thread_name(const Twine &Name)
Set the name of the current thread.
Definition Threading.cpp:36
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.
Definition STLExtras.h:1409
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1899