Fix flaky elf/X86_64/dynlib-search.test.
Output to llvm::err() is not guaranteed to be thread-safe, so it needs
to be guarded with a lock.
Differential Revision: http://llvm-reviews.chandlerc.com/D1862
llvm-svn: 192250
diff --git a/lld/lib/Driver/Driver.cpp b/lld/lib/Driver/Driver.cpp
index e0b81ea..8782230 100644
--- a/lld/lib/Driver/Driver.cpp
+++ b/lld/lib/Driver/Driver.cpp
@@ -25,6 +25,8 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
+#include <mutex>
+
namespace lld {
/// This is where the link is actually performed.
@@ -49,13 +51,26 @@
ScopedTask readTask(getDefaultDomain(), "Read Args");
TaskGroup tg;
int index = 0;
+ std::mutex diagnosticsMutex;
for (auto &ie : inputGraph.inputElements()) {
tg.spawn([&, index] {
- if (error_code ec = ie->parse(context, diagnostics)) {
- FileNode *fileNode = (llvm::dyn_cast<FileNode>)(ie.get());
- diagnostics << fileNode->errStr(ec) << "\n";
+ // Writes to the same output stream is not guaranteed to be thread-safe.
+ // We buffer the diagnostics output to a separate string-backed output
+ // stream, acquire the lock, and then print it out.
+ std::string buf;
+ llvm::raw_string_ostream stream(buf);
+
+ if (error_code ec = ie->parse(context, stream)) {
+ FileNode *fileNode = llvm::dyn_cast<FileNode>(ie.get());
+ stream << fileNode->errStr(ec) << "\n";
fail = true;
}
+
+ stream.flush();
+ if (!buf.empty()) {
+ std::lock_guard<std::mutex> lock(diagnosticsMutex);
+ diagnostics << buf;
+ }
});
++index;
}