[PATCH] D73281: [LLD] Avoid exiting with a locked mutex NFC

Andrew Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 23 10:46:04 PST 2020


andrewng created this revision.
andrewng added reviewers: ruiu, MaskRay.
Herald added a project: LLVM.

In ErrorHandler::error(), rearrange code to avoid calling exitLld with
the mutex locked. Acquire mutex lock when flushing the output streams in
exitLld.


https://reviews.llvm.org/D73281

Files:
  lld/Common/ErrorHandler.cpp


Index: lld/Common/ErrorHandler.cpp
===================================================================
--- lld/Common/ErrorHandler.cpp
+++ lld/Common/ErrorHandler.cpp
@@ -62,8 +62,11 @@
   // avoid intermittent crashes on Windows when exiting.
   llvm_shutdown();
 
-  lld::outs().flush();
-  lld::errs().flush();
+  {
+    std::lock_guard<std::mutex> lock(mu);
+    lld::outs().flush();
+    lld::errs().flush();
+  }
   _exit(val);
 }
 
@@ -191,20 +194,26 @@
     }
   }
 
-  std::lock_guard<std::mutex> lock(mu);
+  bool exit = false;
+  {
+    std::lock_guard<std::mutex> lock(mu);
+
+    if (errorLimit == 0 || errorCount < errorLimit) {
+      lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
+                  << "error: " << Colors::RESET << msg << "\n";
+    } else if (errorCount == errorLimit) {
+      lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
+                  << "error: " << Colors::RESET << errorLimitExceededMsg
+                  << "\n";
+      exit = exitEarly;
+    }
 
-  if (errorLimit == 0 || errorCount < errorLimit) {
-    lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
-                << "error: " << Colors::RESET << msg << "\n";
-  } else if (errorCount == errorLimit) {
-    lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
-                << "error: " << Colors::RESET << errorLimitExceededMsg << "\n";
-    if (exitEarly)
-      exitLld(1);
+    sep = getSeparator(msg);
+    ++errorCount;
   }
 
-  sep = getSeparator(msg);
-  ++errorCount;
+  if (exit)
+    exitLld(1);
 }
 
 void ErrorHandler::fatal(const Twine &msg) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73281.239940.patch
Type: text/x-patch
Size: 1620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200123/6cd3ba11/attachment.bin>


More information about the llvm-commits mailing list