Make log(), error() and fatal() thread-safe.

llvm-svn: 287794
This commit is contained in:
Rui Ueyama 2016-11-23 18:34:28 +00:00
parent 98c80572f2
commit 0ede7f281e
1 changed files with 14 additions and 3 deletions

View File

@ -14,6 +14,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
#include <mutex>
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
@ -28,19 +29,28 @@ uint64_t elf::ErrorCount;
raw_ostream *elf::ErrorOS;
StringRef elf::Argv0;
// The functions defined in this file can be called from multiple threads,
// but outs() or errs() are not thread-safe. We protect them using a mutex.
static std::mutex Mu;
void elf::log(const Twine &Msg) {
std::lock_guard<std::mutex> Lock(Mu);
if (Config->Verbose)
outs() << Argv0 << ": " << Msg << "\n";
}
void elf::warn(const Twine &Msg) {
if (Config->FatalWarnings)
if (Config->FatalWarnings) {
error(Msg);
else
*ErrorOS << Argv0 << ": warning: " << Msg << "\n";
return;
}
std::lock_guard<std::mutex> Lock(Mu);
*ErrorOS << Argv0 << ": warning: " << Msg << "\n";
}
void elf::error(const Twine &Msg) {
std::lock_guard<std::mutex> Lock(Mu);
if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
*ErrorOS << Argv0 << ": error: " << Msg << "\n";
} else if (ErrorCount == Config->ErrorLimit) {
@ -69,6 +79,7 @@ void elf::exitLld(int Val) {
}
void elf::fatal(const Twine &Msg) {
std::lock_guard<std::mutex> Lock(Mu);
*ErrorOS << Argv0 << ": error: " << Msg << "\n";
exitLld(1);
}