Skip to content

Commit d9049e8

Browse files
committed
[clang-scan-deps] Switch to using a ThreadPool
Use a ThreadPool instead of plain std::threads in clang-scan-deps. This is needed to further support https://reviews.llvm.org/D71775. Differential Revision: https://reviews.llvm.org/D74569
1 parent d110c3a commit d9049e8

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

clang/tools/clang-scan-deps/ClangScanDeps.cpp

+6-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/JSON.h"
1919
#include "llvm/Support/Program.h"
2020
#include "llvm/Support/Signals.h"
21+
#include "llvm/Support/ThreadPool.h"
2122
#include "llvm/Support/Threading.h"
2223
#include <mutex>
2324
#include <thread>
@@ -490,6 +491,7 @@ int main(int argc, const char **argv) {
490491
#else
491492
unsigned NumWorkers = 1;
492493
#endif
494+
llvm::ThreadPool Pool(NumWorkers);
493495
std::vector<std::unique_ptr<DependencyScanningTool>> WorkerTools;
494496
for (unsigned I = 0; I < NumWorkers; ++I)
495497
WorkerTools.push_back(std::make_unique<DependencyScanningTool>(Service));
@@ -499,7 +501,6 @@ int main(int argc, const char **argv) {
499501
AdjustingCompilations->getAllCompileCommands())
500502
Inputs.emplace_back(Cmd);
501503

502-
std::vector<std::thread> WorkerThreads;
503504
std::atomic<bool> HadErrors(false);
504505
FullDeps FD;
505506
std::mutex Lock;
@@ -510,8 +511,8 @@ int main(int argc, const char **argv) {
510511
<< " files using " << NumWorkers << " workers\n";
511512
}
512513
for (unsigned I = 0; I < NumWorkers; ++I) {
513-
auto Worker = [I, &Lock, &Index, &Inputs, &HadErrors, &FD, &WorkerTools,
514-
&DependencyOS, &Errs]() {
514+
Pool.async([I, &Lock, &Index, &Inputs, &HadErrors, &FD, &WorkerTools,
515+
&DependencyOS, &Errs]() {
515516
llvm::StringSet<> AlreadySeenModules;
516517
while (true) {
517518
const SingleCommandCompilationDatabase *Input;
@@ -543,16 +544,9 @@ int main(int argc, const char **argv) {
543544
HadErrors = true;
544545
}
545546
}
546-
};
547-
#if LLVM_ENABLE_THREADS
548-
WorkerThreads.emplace_back(std::move(Worker));
549-
#else
550-
// Run the worker without spawning a thread when threads are disabled.
551-
Worker();
552-
#endif
547+
});
553548
}
554-
for (auto &W : WorkerThreads)
555-
W.join();
549+
Pool.wait();
556550

557551
if (Format == ScanningOutputFormat::Full)
558552
FD.printFullOutput(llvm::outs());

0 commit comments

Comments
 (0)