LCOV - code coverage report
Current view: top level - include/llvm/Support - LockFileManager.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 5 5 100.0 %
Date: 2018-05-20 00:06:23 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- LockFileManager.h - File-level locking utility ---------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : #ifndef LLVM_SUPPORT_LOCKFILEMANAGER_H
      10             : #define LLVM_SUPPORT_LOCKFILEMANAGER_H
      11             : 
      12             : #include "llvm/ADT/Optional.h"
      13             : #include "llvm/ADT/SmallString.h"
      14             : #include "llvm/Support/FileSystem.h"
      15             : #include <system_error>
      16             : #include <utility> // for std::pair
      17             : 
      18             : namespace llvm {
      19             : class StringRef;
      20             : 
      21             : /// Class that manages the creation of a lock file to aid
      22             : /// implicit coordination between different processes.
      23             : ///
      24             : /// The implicit coordination works by creating a ".lock" file alongside
      25             : /// the file that we're coordinating for, using the atomicity of the file
      26             : /// system to ensure that only a single process can create that ".lock" file.
      27             : /// When the lock file is removed, the owning process has finished the
      28             : /// operation.
      29             : class LockFileManager {
      30             : public:
      31             :   /// Describes the state of a lock file.
      32             :   enum LockFileState {
      33             :     /// The lock file has been created and is owned by this instance
      34             :     /// of the object.
      35             :     LFS_Owned,
      36             :     /// The lock file already exists and is owned by some other
      37             :     /// instance.
      38             :     LFS_Shared,
      39             :     /// An error occurred while trying to create or find the lock
      40             :     /// file.
      41             :     LFS_Error
      42             :   };
      43             : 
      44             :   /// Describes the result of waiting for the owner to release the lock.
      45             :   enum WaitForUnlockResult {
      46             :     /// The lock was released successfully.
      47             :     Res_Success,
      48             :     /// Owner died while holding the lock.
      49             :     Res_OwnerDied,
      50             :     /// Reached timeout while waiting for the owner to release the lock.
      51             :     Res_Timeout
      52             :   };
      53             : 
      54             : private:
      55             :   SmallString<128> FileName;
      56             :   SmallString<128> LockFileName;
      57             :   Optional<sys::fs::TempFile> UniqueLockFile;
      58             : 
      59             :   Optional<std::pair<std::string, int> > Owner;
      60             :   std::error_code ErrorCode;
      61             :   std::string ErrorDiagMsg;
      62             : 
      63             :   LockFileManager(const LockFileManager &) = delete;
      64             :   LockFileManager &operator=(const LockFileManager &) = delete;
      65             : 
      66             :   static Optional<std::pair<std::string, int> >
      67             :   readLockFile(StringRef LockFileName);
      68             : 
      69             :   static bool processStillExecuting(StringRef Hostname, int PID);
      70             : 
      71             : public:
      72             : 
      73             :   LockFileManager(StringRef FileName);
      74             :   ~LockFileManager();
      75             : 
      76             :   /// Determine the state of the lock file.
      77             :   LockFileState getState() const;
      78             : 
      79        1872 :   operator LockFileState() const { return getState(); }
      80             : 
      81             :   /// For a shared lock, wait until the owner releases the lock.
      82             :   WaitForUnlockResult waitForUnlock();
      83             : 
      84             :   /// Remove the lock file.  This may delete a different lock file than
      85             :   /// the one previously read if there is a race.
      86             :   std::error_code unsafeRemoveLockFile();
      87             : 
      88             :   /// Get error message, or "" if there is no error.
      89             :   std::string getErrorMessage() const;
      90             : 
      91             :   /// Set error and error message
      92           9 :   void setError(const std::error_code &EC, StringRef ErrorMsg = "") {
      93           9 :     ErrorCode = EC;
      94          18 :     ErrorDiagMsg = ErrorMsg.str();
      95           9 :   }
      96             : };
      97             : 
      98             : } // end namespace llvm
      99             : 
     100             : #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H

Generated by: LCOV version 1.13