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