[PATCH] Fix flaky elf/X86_64/dynlib-search.test.

Rui Ueyama ruiu at google.com
Tue Oct 8 15:50:04 PDT 2013


Output to llvm::err() is not guaranteed to be thread-safe, so it needs
to be guarded with a lock.

http://llvm-reviews.chandlerc.com/D1862

Files:
  lib/Driver/Driver.cpp

Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ 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,22 @@
   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;
       }
+      std::lock_guard<std::mutex> lock(diagnosticsMutex);
+      diagnostics << stream.str();
     });
     ++index;
   }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1862.1.patch
Type: text/x-patch
Size: 1348 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131008/d629cb51/attachment.bin>


More information about the llvm-commits mailing list