[msan] Handle X86 SIMD bitshift intrinsics.
[oota-llvm.git] / lib / Support / LockFileManager.cpp
index 59bfcfcd254ccd8c42c499b27031f461eedc97e1..3e23298e4f98a4781c7c3d4ec264f80f8b7f2c3d 100644 (file)
@@ -7,11 +7,13 @@
 //
 //===----------------------------------------------------------------------===//
 #include "llvm/Support/LockFileManager.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
-#include <fstream>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #if LLVM_ON_WIN32
 #include <windows.h>
 #endif
@@ -29,23 +31,26 @@ Optional<std::pair<std::string, int> >
 LockFileManager::readLockFile(StringRef LockFileName) {
   // Check whether the lock file exists. If not, clearly there's nothing
   // to read, so we just return.
-  bool Exists = false;
-  if (sys::fs::exists(LockFileName, Exists) || !Exists)
-    return Optional<std::pair<std::string, int> >();
+  if (!sys::fs::exists(LockFileName))
+    return None;
 
   // Read the owning host and PID out of the lock file. If it appears that the
   // owning process is dead, the lock file is invalid.
-  int PID = 0;
-  std::string Hostname;
-  std::ifstream Input(LockFileName.str().c_str());
-  if (Input >> Hostname >> PID && PID > 0 &&
-      processStillExecuting(Hostname, PID))
-    return std::make_pair(Hostname, PID);
+  OwningPtr<MemoryBuffer> MB;
+  if (MemoryBuffer::getFile(LockFileName, MB))
+    return None;
+
+  StringRef Hostname;
+  StringRef PIDStr;
+  std::tie(Hostname, PIDStr) = getToken(MB->getBuffer(), " ");
+  PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
+  int PID;
+  if (!PIDStr.getAsInteger(10, PID))
+    return std::make_pair(std::string(Hostname), PID);
 
   // Delete the lock file. It's invalid anyway.
-  bool Existed;
-  sys::fs::remove(LockFileName, Existed);
-  return Optional<std::pair<std::string, int> >();
+  sys::fs::remove(LockFileName);
+  return None;
 }
 
 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
@@ -64,6 +69,7 @@ bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
 
 LockFileManager::LockFileManager(StringRef FileName)
 {
+  this->FileName = FileName;
   LockFileName = FileName;
   LockFileName += ".lock";
 
@@ -77,10 +83,9 @@ LockFileManager::LockFileManager(StringRef FileName)
   UniqueLockFileName += "-%%%%%%%%";
   int UniqueLockFileID;
   if (error_code EC
-        = sys::fs::unique_file(UniqueLockFileName.str(),
-                                     UniqueLockFileID,
-                                     UniqueLockFileName,
-                                     /*makeAbsolute=*/false)) {
+        = sys::fs::createUniqueFile(UniqueLockFileName.str(),
+                                    UniqueLockFileID,
+                                    UniqueLockFileName)) {
     Error = EC;
     return;
   }
@@ -105,8 +110,7 @@ LockFileManager::LockFileManager(StringRef FileName)
       // We failed to write out PID, so make up an excuse, remove the
       // unique lock file, and fail.
       Error = make_error_code(errc::no_space_on_device);
-      bool Existed;
-      sys::fs::remove(UniqueLockFileName.c_str(), Existed);
+      sys::fs::remove(UniqueLockFileName.c_str());
       return;
     }
   }
@@ -131,14 +135,13 @@ LockFileManager::LockFileManager(StringRef FileName)
 
   // Someone else managed to create the lock file first. Wipe out our unique
   // lock file (it's useless now) and read the process ID from the lock file.
-  bool Existed;
-  sys::fs::remove(UniqueLockFileName.str(), Existed);
+  sys::fs::remove(UniqueLockFileName.str());
   if ((Owner = readLockFile(LockFileName)))
     return;
 
   // There is a lock file that nobody owns; try to clean it up and report
   // an error.
-  sys::fs::remove(LockFileName.str(), Existed);
+  sys::fs::remove(LockFileName.str());
   Error = EC;
 }
 
@@ -157,9 +160,8 @@ LockFileManager::~LockFileManager() {
     return;
 
   // Since we own the lock, remove the lock file and our own unique lock file.
-  bool Existed;
-  sys::fs::remove(LockFileName.str(), Existed);
-  sys::fs::remove(UniqueLockFileName.str(), Existed);
+  sys::fs::remove(LockFileName.str());
+  sys::fs::remove(UniqueLockFileName.str());
 }
 
 void LockFileManager::waitForUnlock() {
@@ -173,8 +175,9 @@ void LockFileManager::waitForUnlock() {
   Interval.tv_sec = 0;
   Interval.tv_nsec = 1000000;
 #endif
-  // Don't wait more than an hour for the file to appear.
-  const unsigned MaxSeconds = 3600;
+  // Don't wait more than five minutes for the file to appear.
+  unsigned MaxSeconds = 300;
+  bool LockFileGone = false;
   do {
     // Sleep for the designated interval, to allow the owning process time to
     // finish up and remove the lock file.
@@ -185,13 +188,47 @@ void LockFileManager::waitForUnlock() {
 #else
     nanosleep(&Interval, NULL);
 #endif
-    // If the file no longer exists, we're done.
-    bool Exists = false;
-    if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists)
-      return;
+    bool LockFileJustDisappeared = false;
+
+    // If the lock file is still expected to be there, check whether it still
+    // is.
+    if (!LockFileGone) {
+      bool Exists;
+      if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) {
+        LockFileGone = true;
+        LockFileJustDisappeared = true;
+      }
+    }
+
+    // If the lock file is no longer there, check if the original file is
+    // available now.
+    if (LockFileGone) {
+      if (sys::fs::exists(FileName.str())) {
+        return;
+      }
+
+      // The lock file is gone, so now we're waiting for the original file to
+      // show up. If this just happened, reset our waiting intervals and keep
+      // waiting.
+      if (LockFileJustDisappeared) {
+        MaxSeconds = 5;
 
-    if (!processStillExecuting((*Owner).first, (*Owner).second))
+#if LLVM_ON_WIN32
+        Interval = 1;
+#else
+        Interval.tv_sec = 0;
+        Interval.tv_nsec = 1000000;
+#endif
+        continue;
+      }
+    }
+
+    // If we're looking for the lock file to disappear, but the process
+    // owning the lock died without cleaning up, just bail out.
+    if (!LockFileGone &&
+        !processStillExecuting((*Owner).first, (*Owner).second)) {
       return;
+    }
 
     // Exponentially increase the time we wait for the lock to be removed.
 #if LLVM_ON_WIN32