<div dir="ltr">Even with locking, the output will not be deterministic. Is that something we are okay with? I think we should avoid it.<div><br></div><div><br></div><div>-- Sean Silva</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 23, 2016 at 10:34 AM, Rui Ueyama via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: ruiu<br>
Date: Wed Nov 23 12:34:28 2016<br>
New Revision: 287794<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=287794&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=287794&view=rev</a><br>
Log:<br>
Make log(), error() and fatal() thread-safe.<br>
<br>
Modified:<br>
lld/trunk/ELF/Error.cpp<br>
<br>
Modified: lld/trunk/ELF/Error.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Error.cpp?rev=287794&r1=287793&r2=287794&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Error.<wbr>cpp?rev=287794&r1=287793&r2=<wbr>287794&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/Error.cpp (original)<br>
+++ lld/trunk/ELF/Error.cpp Wed Nov 23 12:34:28 2016<br>
@@ -14,6 +14,7 @@<br>
#include "llvm/Support/Error.h"<br>
#include "llvm/Support/ManagedStatic.h"<br>
#include "llvm/Support/raw_ostream.h"<br>
+#include <mutex><br>
<br>
#if !defined(_MSC_VER) && !defined(__MINGW32__)<br>
#include <unistd.h><br>
@@ -28,19 +29,28 @@ uint64_t elf::ErrorCount;<br>
raw_ostream *elf::ErrorOS;<br>
StringRef elf::Argv0;<br>
<br>
+// The functions defined in this file can be called from multiple threads,<br>
+// but outs() or errs() are not thread-safe. We protect them using a mutex.<br>
+static std::mutex Mu;<br>
+<br>
void elf::log(const Twine &Msg) {<br>
+ std::lock_guard<std::mutex> Lock(Mu);<br>
if (Config->Verbose)<br>
outs() << Argv0 << ": " << Msg << "\n";<br>
}<br>
<br>
void elf::warn(const Twine &Msg) {<br>
- if (Config->FatalWarnings)<br>
+ if (Config->FatalWarnings) {<br>
error(Msg);<br>
- else<br>
- *ErrorOS << Argv0 << ": warning: " << Msg << "\n";<br>
+ return;<br>
+ }<br>
+ std::lock_guard<std::mutex> Lock(Mu);<br>
+ *ErrorOS << Argv0 << ": warning: " << Msg << "\n";<br>
}<br>
<br>
void elf::error(const Twine &Msg) {<br>
+ std::lock_guard<std::mutex> Lock(Mu);<br>
+<br>
if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {<br>
*ErrorOS << Argv0 << ": error: " << Msg << "\n";<br>
} else if (ErrorCount == Config->ErrorLimit) {<br>
@@ -69,6 +79,7 @@ void elf::exitLld(int Val) {<br>
}<br>
<br>
void elf::fatal(const Twine &Msg) {<br>
+ std::lock_guard<std::mutex> Lock(Mu);<br>
*ErrorOS << Argv0 << ": error: " << Msg << "\n";<br>
exitLld(1);<br>
}<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>