clang-scan-deps: do not spawn threads when LLVM_ENABLE_THREADS is disabled

llvm-svn: 368640
This commit is contained in:
Alex Lorenz 2019-08-13 00:36:35 +00:00
parent f4446f1775
commit 3ffa688dfc
2 changed files with 28 additions and 20 deletions

View File

@ -1,4 +1,3 @@
// REQUIRES: thread_support
// RUN: rm -rf %t.dir
// RUN: rm -rf %t.cdb
// RUN: mkdir -p %t.dir

View File

@ -154,8 +154,12 @@ int main(int argc, const char **argv) {
SharedStream DependencyOS(llvm::outs());
DependencyScanningService Service(ScanMode);
#if LLVM_ENABLE_THREADS
unsigned NumWorkers =
NumThreads == 0 ? llvm::hardware_concurrency() : NumThreads;
#else
unsigned NumWorkers = 1;
#endif
std::vector<std::unique_ptr<DependencyScanningTool>> WorkerTools;
for (unsigned I = 0; I < NumWorkers; ++I)
WorkerTools.push_back(llvm::make_unique<DependencyScanningTool>(
@ -169,25 +173,30 @@ int main(int argc, const char **argv) {
llvm::outs() << "Running clang-scan-deps on " << Inputs.size()
<< " files using " << NumWorkers << " workers\n";
for (unsigned I = 0; I < NumWorkers; ++I) {
WorkerThreads.emplace_back(
[I, &Lock, &Index, &Inputs, &HadErrors, &WorkerTools]() {
while (true) {
std::string Input;
StringRef CWD;
// Take the next input.
{
std::unique_lock<std::mutex> LockGuard(Lock);
if (Index >= Inputs.size())
return;
const auto &Compilation = Inputs[Index++];
Input = Compilation.first;
CWD = Compilation.second;
}
// Run the tool on it.
if (WorkerTools[I]->runOnFile(Input, CWD))
HadErrors = true;
}
});
auto Worker = [I, &Lock, &Index, &Inputs, &HadErrors, &WorkerTools]() {
while (true) {
std::string Input;
StringRef CWD;
// Take the next input.
{
std::unique_lock<std::mutex> LockGuard(Lock);
if (Index >= Inputs.size())
return;
const auto &Compilation = Inputs[Index++];
Input = Compilation.first;
CWD = Compilation.second;
}
// Run the tool on it.
if (WorkerTools[I]->runOnFile(Input, CWD))
HadErrors = true;
}
};
#if LLVM_ENABLE_THREADS
WorkerThreads.emplace_back(std::move(Worker));
#else
// Run the worker without spawning a thread when threads are disabled.
Worker();
#endif
}
for (auto &W : WorkerThreads)
W.join();