[LLD] Avoid exiting with a locked mutex NFC
authorAndrew Ng <andrew.ng@sony.com>
Wed, 22 Jan 2020 17:08:09 +0000 (17:08 +0000)
committerAndrew Ng <andrew.ng@sony.com>
Tue, 28 Jan 2020 16:44:44 +0000 (16:44 +0000)
In ErrorHandler::error(), rearrange code to avoid calling exitLld with
the mutex locked. Acquire mutex lock when flushing the output streams in
exitLld.

Differential Revision: https://reviews.llvm.org/D73281

lld/Common/ErrorHandler.cpp

index b6066b5..fb84832 100644 (file)
@@ -62,8 +62,11 @@ void lld::exitLld(int val) {
   // 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 @@ void ErrorHandler::error(const Twine &msg) {
     }
   }
 
-  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) {