
The cleanup was manual, but assisted by "include-what-you-use". It consists in 1. Removing unused forward declaration. No impact expected. 2. Removing unused headers in .cpp files. No impact expected. 3. Removing unused headers in .h files. This removes implicit dependencies and is generally considered a good thing, but this may break downstream builds. I've updated llvm, clang, lld, lldb and mlir deps, and included a list of the modification in the second part of the commit. 4. Replacing header inclusion by forward declaration. This has the same impact as 3. Notable changes: - llvm/Support/TargetParser.h no longer includes llvm/Support/AArch64TargetParser.h nor llvm/Support/ARMTargetParser.h - llvm/Support/TypeSize.h no longer includes llvm/Support/WithColor.h - llvm/Support/YAMLTraits.h no longer includes llvm/Support/Regex.h - llvm/ADT/SmallVector.h no longer includes llvm/Support/MemAlloc.h nor llvm/Support/ErrorHandling.h You may need to add some of these headers in your compilation units, if needs be. As an hint to the impact of the cleanup, running clang++ -E -Iinclude -I../llvm/include ../llvm/lib/Support/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l before: 8000919 lines after: 7917500 lines Reduced dependencies also helps incremental rebuilds and is more ccache friendly, something not shown by the above metric :-) Discourse thread on the topic: https://llvm.discourse.group/t/include-what-you-use-include-cleanup/5831
126 lines
4.0 KiB
C++
126 lines
4.0 KiB
C++
//==-- 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/Support/Threading.h"
|
|
|
|
using namespace llvm;
|
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
ThreadPool::ThreadPool(ThreadPoolStrategy S)
|
|
: Strategy(S), MaxThreadCount(S.compute_thread_count()) {}
|
|
|
|
void ThreadPool::grow(int requested) {
|
|
std::unique_lock<std::mutex> 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] {
|
|
Strategy.apply_thread_strategy(ThreadID);
|
|
while (true) {
|
|
std::function<void()> Task;
|
|
{
|
|
std::unique_lock<std::mutex> LockGuard(QueueLock);
|
|
// Wait for tasks to be pushed in the queue
|
|
QueueCondition.wait(LockGuard,
|
|
[&] { return !EnableFlag || !Tasks.empty(); });
|
|
// Exit condition
|
|
if (!EnableFlag && Tasks.empty())
|
|
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());
|
|
Tasks.pop();
|
|
}
|
|
// Run the task we just grabbed
|
|
Task();
|
|
|
|
bool Notify;
|
|
{
|
|
// Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait()
|
|
std::lock_guard<std::mutex> LockGuard(QueueLock);
|
|
--ActiveThreads;
|
|
Notify = workCompletedUnlocked();
|
|
}
|
|
// Notify task completion if this is the last active thread, in case
|
|
// someone waits on ThreadPool::wait().
|
|
if (Notify)
|
|
CompletionCondition.notify_all();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
void ThreadPool::wait() {
|
|
// Wait for all threads to complete and the queue to be empty
|
|
std::unique_lock<std::mutex> LockGuard(QueueLock);
|
|
CompletionCondition.wait(LockGuard, [&] { return workCompletedUnlocked(); });
|
|
}
|
|
|
|
bool ThreadPool::isWorkerThread() const {
|
|
std::unique_lock<std::mutex> 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.
|
|
ThreadPool::~ThreadPool() {
|
|
{
|
|
std::unique_lock<std::mutex> LockGuard(QueueLock);
|
|
EnableFlag = false;
|
|
}
|
|
QueueCondition.notify_all();
|
|
std::unique_lock<std::mutex> LockGuard(ThreadsLock);
|
|
for (auto &Worker : Threads)
|
|
Worker.join();
|
|
}
|
|
|
|
#else // LLVM_ENABLE_THREADS Disabled
|
|
|
|
// No threads are launched, issue a warning if ThreadCount is not 0
|
|
ThreadPool::ThreadPool(ThreadPoolStrategy S) : MaxThreadCount(1) {
|
|
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 ThreadPool::wait() {
|
|
// Sequential implementation running the tasks
|
|
while (!Tasks.empty()) {
|
|
auto Task = std::move(Tasks.front());
|
|
Tasks.pop();
|
|
Task();
|
|
}
|
|
}
|
|
|
|
bool ThreadPool::isWorkerThread() const {
|
|
report_fatal_error("LLVM compiled without multithreading");
|
|
}
|
|
|
|
ThreadPool::~ThreadPool() { wait(); }
|
|
|
|
#endif
|