[lld] r287794 - Make log(), error() and fatal() thread-safe.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 23 10:34:28 PST 2016
Author: ruiu
Date: Wed Nov 23 12:34:28 2016
New Revision: 287794
URL: http://llvm.org/viewvc/llvm-project?rev=287794&view=rev
Log:
Make log(), error() and fatal() thread-safe.
Modified:
lld/trunk/ELF/Error.cpp
Modified: lld/trunk/ELF/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Error.cpp?rev=287794&r1=287793&r2=287794&view=diff
==============================================================================
--- lld/trunk/ELF/Error.cpp (original)
+++ lld/trunk/ELF/Error.cpp Wed Nov 23 12:34:28 2016
@@ -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);
}
More information about the llvm-commits
mailing list