LockFileManager.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "llvm/ADT/Optional.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include <system_error>
  15. #include <utility> // for std::pair
  16. namespace llvm {
  17. /// \brief Class that manages the creation of a lock file to aid
  18. /// implicit coordination between different processes.
  19. ///
  20. /// The implicit coordination works by creating a ".lock" file alongside
  21. /// the file that we're coordinating for, using the atomicity of the file
  22. /// system to ensure that only a single process can create that ".lock" file.
  23. /// When the lock file is removed, the owning process has finished the
  24. /// operation.
  25. class LockFileManager {
  26. public:
  27. /// \brief Describes the state of a lock file.
  28. enum LockFileState {
  29. /// \brief The lock file has been created and is owned by this instance
  30. /// of the object.
  31. LFS_Owned,
  32. /// \brief The lock file already exists and is owned by some other
  33. /// instance.
  34. LFS_Shared,
  35. /// \brief An error occurred while trying to create or find the lock
  36. /// file.
  37. LFS_Error
  38. };
  39. /// \brief Describes the result of waiting for the owner to release the lock.
  40. enum WaitForUnlockResult {
  41. /// \brief The lock was released successfully.
  42. Res_Success,
  43. /// \brief Owner died while holding the lock.
  44. Res_OwnerDied,
  45. /// \brief Reached timeout while waiting for the owner to release the lock.
  46. Res_Timeout
  47. };
  48. private:
  49. SmallString<128> FileName;
  50. SmallString<128> LockFileName;
  51. SmallString<128> UniqueLockFileName;
  52. Optional<std::pair<std::string, int> > Owner;
  53. Optional<std::error_code> Error;
  54. LockFileManager(const LockFileManager &) = delete;
  55. LockFileManager &operator=(const LockFileManager &) = delete;
  56. static Optional<std::pair<std::string, int> >
  57. readLockFile(StringRef LockFileName);
  58. static bool processStillExecuting(StringRef Hostname, int PID);
  59. public:
  60. LockFileManager(StringRef FileName);
  61. ~LockFileManager();
  62. /// \brief Determine the state of the lock file.
  63. LockFileState getState() const;
  64. operator LockFileState() const { return getState(); }
  65. /// \brief For a shared lock, wait until the owner releases the lock.
  66. WaitForUnlockResult waitForUnlock();
  67. /// \brief Remove the lock file. This may delete a different lock file than
  68. /// the one previously read if there is a race.
  69. std::error_code unsafeRemoveLockFile();
  70. };
  71. } // end namespace llvm
  72. #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H