Skip to content

Commit 9679075

Browse files
committed
[clang][deps] NFC: Simplify worker loop
This patch simplifies the loop inside each worker by extracting index retrieval into a lambda function. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D145101
1 parent a183668 commit 9679075

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,15 @@ int main(int argc, const char **argv) {
783783
std::atomic<bool> HadErrors(false);
784784
std::optional<FullDeps> FD;
785785
P1689Deps PD;
786+
786787
std::mutex Lock;
787788
size_t Index = 0;
789+
auto GetNextInputIndex = [&]() -> std::optional<size_t> {
790+
std::unique_lock<std::mutex> LockGuard(Lock);
791+
if (Index < Inputs.size())
792+
return Index++;
793+
return {};
794+
};
788795

789796
if (Format == ScanningOutputFormat::Full)
790797
FD.emplace(ModuleName.empty() ? Inputs.size() : 0);
@@ -794,24 +801,14 @@ int main(int argc, const char **argv) {
794801
<< " files using " << Pool.getThreadCount() << " workers\n";
795802
}
796803
for (unsigned I = 0; I < Pool.getThreadCount(); ++I) {
797-
Pool.async([I, &Lock, &Index, &Inputs, &HadErrors, &FD, &PD, &WorkerTools,
798-
&DependencyOS, &Errs]() {
804+
Pool.async([&, I]() {
799805
llvm::StringSet<> AlreadySeenModules;
800-
while (true) {
801-
const tooling::CompileCommand *Input;
802-
std::string Filename;
803-
std::string CWD;
804-
size_t LocalIndex;
805-
// Take the next input.
806-
{
807-
std::unique_lock<std::mutex> LockGuard(Lock);
808-
if (Index >= Inputs.size())
809-
return;
810-
LocalIndex = Index;
811-
Input = &Inputs[Index++];
812-
Filename = std::move(Input->Filename);
813-
CWD = std::move(Input->Directory);
814-
}
806+
while (auto MaybeInputIndex = GetNextInputIndex()) {
807+
size_t LocalIndex = *MaybeInputIndex;
808+
const tooling::CompileCommand *Input = &Inputs[LocalIndex];
809+
std::string Filename = std::move(Input->Filename);
810+
std::string CWD = std::move(Input->Directory);
811+
815812
std::optional<StringRef> MaybeModuleName;
816813
if (!ModuleName.empty())
817814
MaybeModuleName = ModuleName;

0 commit comments

Comments
 (0)