1//==-- llvm/Support/ThreadPool.cpp - A ThreadPool implementation -*- C++ -*-==//
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
7//===----------------------------------------------------------------------===//
10// This file implements a crude C++11 based thread pool.
12//===----------------------------------------------------------------------===//
16#include "llvm/Config/llvm-config.h"
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
36#if LLVM_ENABLE_THREADS
39 : Strategy(S), MaxThreadCount(S.compute_thread_count()) {
40 if (Strategy.UseJobserver)
44void StdThreadPool::grow(
int requested) {
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] {
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.
60 processTasksWithJobserver();
62 processTasks(
nullptr);
68// The group of the tasks run by the current thread.
70 *CurrentThreadTaskGroups =
nullptr;
73// WaitingForGroup == nullptr means all tasks regardless of their group.
76 std::function<void()> Task;
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)));
89 if (!EnableFlag && Tasks.empty())
91 if (WaitingForGroup !=
nullptr && workCompletedForGroup)
93 // Yeah, we have a task, grab it and release the lock on the queue
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.
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
108 if (CurrentThreadTaskGroups ==
nullptr)
109 CurrentThreadTaskGroups =
new std::vector<ThreadPoolTaskGroup *>;
110 CurrentThreadTaskGroups->push_back(GroupOfTask);
113 // Run the task we just grabbed
117 CurrentThreadTaskGroups->pop_back();
118 if (CurrentThreadTaskGroups->empty()) {
119 delete CurrentThreadTaskGroups;
120 CurrentThreadTaskGroups =
nullptr;
127 // Adjust `ActiveThreads`, in case someone waits on StdThreadPool::wait()
128 std::lock_guard<std::mutex> LockGuard(QueueLock);
130 if (GroupOfTask !=
nullptr) {
131 auto A = ActiveGroups.find(GroupOfTask);
132 if (--(
A->second) == 0)
133 ActiveGroups.erase(
A);
135 Notify = workCompletedUnlocked(GroupOfTask);
136 NotifyGroup = GroupOfTask !=
nullptr && Notify;
138 // Notify task completion if this is the last active thread, in case
139 // someone waits on StdThreadPool::wait().
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.
146 QueueCondition.notify_all();
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() {
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.
163 bool AcquiredToken =
false;
165 // Return if the thread pool is shutting down.
167 std::unique_lock<std::mutex> LockGuard(QueueLock);
172 Slot = TheJobserver->tryAcquire();
173 if (
Slot.isValid()) {
174 AcquiredToken =
true;
177 }
while (Backoff.waitForNextAttempt());
179 if (!AcquiredToken) {
180 // This is practically unreachable with a 24h timeout and indicates a
181 // deeper problem if hit.
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.
190 // While we hold a job slot, process tasks from the internal queue.
192 std::function<void()> Task;
196 std::unique_lock<std::mutex> LockGuard(QueueLock);
198 // Wait until a task is available or the pool is shutting down.
199 QueueCondition.wait(LockGuard,
200 [&] {
return !EnableFlag || !Tasks.empty(); });
202 // If shutting down and the queue is empty, the thread can terminate.
203 if (!EnableFlag && Tasks.empty())
206 // If the queue is empty, we're done processing tasks for now.
207 // Break the inner loop to release the job slot.
211 // A task is available. Mark it as active before releasing the lock
212 // to prevent race conditions with `wait()`.
214 Task = std::move(Tasks.front().first);
215 GroupOfTask = Tasks.front().second;
216 if (GroupOfTask !=
nullptr)
217 ++ActiveGroups[GroupOfTask];
219 }
// The queue lock is released.
221 // Run the task. The job slot remains acquired during execution.
224 // The task has finished. Update the active count and notify any waiters.
226 std::lock_guard<std::mutex> LockGuard(QueueLock);
228 if (GroupOfTask !=
nullptr) {
229 auto A = ActiveGroups.find(GroupOfTask);
230 if (--(
A->second) == 0)
231 ActiveGroups.erase(
A);
233 // If all tasks are complete, notify any waiting threads.
234 if (workCompletedUnlocked(
nullptr))
235 CompletionCondition.notify_all();
241 if (Group ==
nullptr)
242 return !ActiveThreads && Tasks.empty();
243 return ActiveGroups.count(Group) == 0 &&
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); });
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); });
263 // Make sure to not deadlock waiting for oneself.
264 assert(CurrentThreadTaskGroups ==
nullptr ||
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);
272bool StdThreadPool::isWorkerThread()
const {
274 llvm::thread::id CurrentThreadId = llvm::this_thread::get_id();
276 if (CurrentThreadId ==
Thread.get_id())
281// The destructor joins all threads, waiting for completion.
282StdThreadPool::~StdThreadPool() {
284 std::unique_lock<std::mutex> LockGuard(QueueLock);
287 QueueCondition.notify_all();
289 for (
auto &Worker : Threads)
293#endif // LLVM_ENABLE_THREADS Disabled
295// No threads are launched, issue a warning if ThreadCount is not 0
300 <<
" threads, but LLVM_ENABLE_THREADS has been turned off\n";
305 // Sequential implementation running the tasks
306 while (!Tasks.empty()) {
307 auto Task = std::move(Tasks.front().first);
314 // Simply wait for all, this works even if recursive (the running task
315 // is already removed from the queue).
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,...
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.
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.
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.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
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.