Skip to content

Commit de2dfc8

Browse files
committed
[LLD] Avoid exiting with a locked mutex NFC
In ErrorHandler::error(), rearrange code to avoid calling exitLld with the mutex locked. Acquire mutex lock when flushing the output streams in exitLld. Differential Revision: https://reviews.llvm.org/D73281
1 parent f117f2c commit de2dfc8

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

lld/Common/ErrorHandler.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ void lld::exitLld(int val) {
6262
// avoid intermittent crashes on Windows when exiting.
6363
llvm_shutdown();
6464

65-
lld::outs().flush();
66-
lld::errs().flush();
65+
{
66+
std::lock_guard<std::mutex> lock(mu);
67+
lld::outs().flush();
68+
lld::errs().flush();
69+
}
6770
_exit(val);
6871
}
6972

@@ -191,20 +194,26 @@ void ErrorHandler::error(const Twine &msg) {
191194
}
192195
}
193196

194-
std::lock_guard<std::mutex> lock(mu);
197+
bool exit = false;
198+
{
199+
std::lock_guard<std::mutex> lock(mu);
200+
201+
if (errorLimit == 0 || errorCount < errorLimit) {
202+
lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
203+
<< "error: " << Colors::RESET << msg << "\n";
204+
} else if (errorCount == errorLimit) {
205+
lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
206+
<< "error: " << Colors::RESET << errorLimitExceededMsg
207+
<< "\n";
208+
exit = exitEarly;
209+
}
195210

196-
if (errorLimit == 0 || errorCount < errorLimit) {
197-
lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
198-
<< "error: " << Colors::RESET << msg << "\n";
199-
} else if (errorCount == errorLimit) {
200-
lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
201-
<< "error: " << Colors::RESET << errorLimitExceededMsg << "\n";
202-
if (exitEarly)
203-
exitLld(1);
211+
sep = getSeparator(msg);
212+
++errorCount;
204213
}
205214

206-
sep = getSeparator(msg);
207-
++errorCount;
215+
if (exit)
216+
exitLld(1);
208217
}
209218

210219
void ErrorHandler::fatal(const Twine &msg) {

0 commit comments

Comments
 (0)