[llvm] 8577595 - Revert "[Support] Add file lock/unlock functions"

Serge Pavlov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 01:42:48 PDT 2020


Author: Serge Pavlov
Date: 2020-06-03T15:40:12+07:00
New Revision: 8577595e03faf740ee0cfae1bbb2d0ff6f4e4516

URL: https://github.com/llvm/llvm-project/commit/8577595e03faf740ee0cfae1bbb2d0ff6f4e4516
DIFF: https://github.com/llvm/llvm-project/commit/8577595e03faf740ee0cfae1bbb2d0ff6f4e4516.diff

LOG: Revert "[Support] Add file lock/unlock functions"

This reverts commit f51bc4fb60fbcef26d18eff549fc68307fd46489.
It broke the Solaris buildbots (Builder clang-solaris11-sparcv9 Build #5494
<http://lab.llvm.org:8014/builders/clang-solaris11-sparcv9/builds/54).

Added: 
    

Modified: 
    llvm/include/llvm/Support/FileSystem.h
    llvm/lib/Support/Unix/Path.inc
    llvm/lib/Support/Windows/Path.inc
    llvm/unittests/Support/Path.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/FileSystem.h b/llvm/include/llvm/Support/FileSystem.h
index 8014c5c27b25..a29a9d787947 100644
--- a/llvm/include/llvm/Support/FileSystem.h
+++ b/llvm/include/llvm/Support/FileSystem.h
@@ -1131,39 +1131,6 @@ Expected<file_t>
 openNativeFileForRead(const Twine &Name, OpenFlags Flags = OF_None,
                       SmallVectorImpl<char> *RealPath = nullptr);
 
-/// Try to locks the file during the specified time.
-///
-/// This function implements advisory locking on entire file. If it returns
-/// <em>errc::success</em>, the file is locked by the calling process. Until the
-/// process unlocks the file by calling \a unlockFile, all attempts to lock the
-/// same file will fail/block. The process that locked the file may assume that
-/// none of other processes read or write this file, provided that all processes
-/// lock the file prior to accessing its content.
-///
-/// @param File    The descriptor representing the file to lock.
-/// @param Timeout Time in milliseconds that the process should wait before
-///                reporting lock failure. Zero value means try to get lock only
-///                once.
-/// @returns errc::success if lock is successfully obtained,
-/// errc::no_lock_available if the file cannot be locked, or platform-specific
-/// error_code otherwise.
-std::error_code
-tryLockFile(int FD,
-            std::chrono::milliseconds Timeout = std::chrono::milliseconds(0));
-
-/// Lock the file.
-///
-/// This function acts as @ref tryLockFile(int,std::chrono::milliseconds) but it
-/// waits infinitely.
-std::error_code lockFile(int FD);
-
-/// Unlock the file.
-///
-/// @param File The descriptor representing the file to unlock.
-/// @returns errc::success if lock is successfully released or platform-specific
-/// error_code otherwise.
-std::error_code unlockFile(int FD);
-
 /// @brief Close the file object.  This should be used instead of ::close for
 /// portability. On error, the caller should assume the file is closed, as is
 /// the case for Process::SafelyCloseFileDescriptor

diff  --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index cab31f92984e..bf720b318ded 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -33,7 +33,6 @@
 
 #include <dirent.h>
 #include <pwd.h>
-#include <sys/file.h>
 
 #ifdef __APPLE__
 #include <mach-o/dyld.h>
@@ -1048,34 +1047,6 @@ Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
   return NumRead;
 }
 
-std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) {
-  auto Start = std::chrono::steady_clock::now();
-  auto End = Start + Timeout;
-  do {
-    if (::flock(FD, LOCK_EX | LOCK_NB) == 0)
-      return std::error_code();
-    int Error = errno;
-    if (Error == EWOULDBLOCK) {
-      usleep(1000);
-      continue;
-    }
-    return std::error_code(Error, std::generic_category());
-  } while (std::chrono::steady_clock::now() < End);
-  return make_error_code(errc::no_lock_available);
-}
-
-std::error_code lockFile(int FD) {
-  if (::flock(FD, LOCK_EX) == 0)
-    return std::error_code();
-  return std::error_code(errno, std::generic_category());
-}
-
-std::error_code unlockFile(int FD) {
-  if (::flock(FD, LOCK_UN) == -1)
-    return std::error_code(errno, std::generic_category());
-  return std::error_code();
-}
-
 std::error_code closeFile(file_t &F) {
   file_t TmpF = F;
   F = kInvalidFile;

diff  --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 39af3a5f46b6..ec62e656ddf0 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -1255,43 +1255,6 @@ Expected<size_t> readNativeFileSlice(file_t FileHandle,
   return readNativeFileImpl(FileHandle, Buf, &Overlapped);
 }
 
-std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) {
-  DWORD Flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
-  OVERLAPPED OV = {0};
-  file_t File = convertFDToNativeFile(FD);
-  auto Start = std::chrono::steady_clock::now();
-  auto End = Start + Timeout;
-  do {
-    if (::LockFileEx(File, Flags, 0, MAXDWORD, MAXDWORD, &OV))
-      return std::error_code();
-    DWORD Error = ::GetLastError();
-    if (Error == ERROR_LOCK_VIOLATION) {
-      ::Sleep(1);
-      continue;
-    }
-    return mapWindowsError(Error);
-  } while (std::chrono::steady_clock::now() < End);
-  return mapWindowsError(ERROR_LOCK_VIOLATION);
-}
-
-std::error_code lockFile(int FD) {
-  DWORD Flags = LOCKFILE_EXCLUSIVE_LOCK;
-  OVERLAPPED OV = {0};
-  file_t File = convertFDToNativeFile(FD);
-  if (::LockFileEx(File, Flags, 0, MAXDWORD, MAXDWORD, &OV))
-    return std::error_code();
-  DWORD Error = ::GetLastError();
-  return mapWindowsError(Error);
-}
-
-std::error_code unlockFile(int FD) {
-  OVERLAPPED OV = {0};
-  file_t File = convertFDToNativeFile(FD);
-  if (::UnlockFileEx(File, 0, MAXDWORD, MAXDWORD, &OV))
-    return std::error_code();
-  return mapWindowsError(::GetLastError());
-}
-
 std::error_code closeFile(file_t &F) {
   file_t TmpF = F;
   F = kInvalidFile;

diff  --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index 92b3917724ff..8e842a95b2b2 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -39,8 +39,6 @@
 #include <sys/stat.h>
 #endif
 
-#include <future>
-
 using namespace llvm;
 using namespace llvm::sys;
 
@@ -2040,47 +2038,4 @@ TEST_F(FileSystemTest, widenPath) {
 }
 #endif
 
-TEST_F(FileSystemTest, lockFile) {
-  int FD1, FD2;
-  SmallString<64> TempPath;
-  ASSERT_NO_ERROR(fs::createTemporaryFile("test", "temp", FD1, TempPath));
-  FileRemover Cleanup(TempPath);
-  ASSERT_NO_ERROR(fs::openFileForReadWrite(TempPath, FD2, fs::CD_OpenExisting,
-                                           fs::OF_Append));
-  ASSERT_NO_ERROR(fs::tryLockFile(FD1));
-
-  ASSERT_EQ(errc::no_lock_available,
-            fs::tryLockFile(FD2, std::chrono::milliseconds(5)));
-  ASSERT_NO_ERROR(fs::unlockFile(FD1));
-  ASSERT_NO_ERROR(fs::tryLockFile(FD2));
-  ASSERT_NO_ERROR(fs::unlockFile(FD2));
-}
-
-TEST_F(FileSystemTest, lockFileThread) {
-#if LLVM_ENABLE_THREADS
-  int FD1, FD2;
-  SmallString<64> TempPath;
-  ASSERT_NO_ERROR(fs::createTemporaryFile("test", "temp", FD1, TempPath));
-  FileRemover Cleanup(TempPath);
-  ASSERT_NO_ERROR(fs::openFileForReadWrite(TempPath, FD2, fs::CD_OpenExisting,
-                                           fs::OF_Append));
-
-  ASSERT_NO_ERROR(fs::tryLockFile(FD1));
-  ASSERT_ERROR(fs::tryLockFile(FD2));
-  std::future<std::error_code> Future = std::async(std::launch::async, [&] {
-    return fs::tryLockFile(FD2, std::chrono::seconds(5));
-  });
-  ASSERT_NO_ERROR(fs::unlockFile(FD1));
-  ASSERT_NO_ERROR(Future.get());
-  fs::unlockFile(FD2);
-
-  ASSERT_NO_ERROR(fs::tryLockFile(FD1));
-  ASSERT_ERROR(fs::tryLockFile(FD2));
-  Future = std::async(std::launch::async, [&] { return fs::lockFile(FD2); });
-  ASSERT_NO_ERROR(fs::unlockFile(FD1));
-  ASSERT_NO_ERROR(Future.get());
-  fs::unlockFile(FD2);
-#endif
-}
-
 } // anonymous namespace


        


More information about the llvm-commits mailing list