LockFileManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- LockFileManager.h - File-level locking utility ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_LOCKFILEMANAGER_H
  14. #define LLVM_SUPPORT_LOCKFILEMANAGER_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include <system_error>
  18. #include <utility> // for std::pair
  19. namespace llvm {
  20. class StringRef;
  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. /// Describes the result of waiting for the owner to release the lock.
  44. enum WaitForUnlockResult {
  45. /// The lock was released successfully.
  46. Res_Success,
  47. /// Owner died while holding the lock.
  48. Res_OwnerDied,
  49. /// Reached timeout while waiting for the owner to release the lock.
  50. Res_Timeout
  51. };
  52. private:
  53. SmallString<128> FileName;
  54. SmallString<128> LockFileName;
  55. SmallString<128> UniqueLockFileName;
  56. Optional<std::pair<std::string, int> > Owner;
  57. std::error_code ErrorCode;
  58. std::string ErrorDiagMsg;
  59. LockFileManager(const LockFileManager &) = delete;
  60. LockFileManager &operator=(const LockFileManager &) = delete;
  61. static Optional<std::pair<std::string, int> >
  62. readLockFile(StringRef LockFileName);
  63. static bool processStillExecuting(StringRef Hostname, int PID);
  64. public:
  65. LockFileManager(StringRef FileName);
  66. ~LockFileManager();
  67. /// Determine the state of the lock file.
  68. LockFileState getState() const;
  69. operator LockFileState() const { return getState(); }
  70. /// For a shared lock, wait until the owner releases the lock.
  71. /// Total timeout for the file to appear is ~1.5 minutes.
  72. /// \param MaxSeconds the maximum total wait time in seconds.
  73. WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds = 90);
  74. /// Remove the lock file. This may delete a different lock file than
  75. /// the one previously read if there is a race.
  76. std::error_code unsafeRemoveLockFile();
  77. /// Get error message, or "" if there is no error.
  78. std::string getErrorMessage() const;
  79. /// Set error and error message
  80. void setError(const std::error_code &EC, StringRef ErrorMsg = "") {
  81. ErrorCode = EC;
  82. ErrorDiagMsg = ErrorMsg.str();
  83. }
  84. };
  85. } // end namespace llvm
  86. #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H
  87. #ifdef __GNUC__
  88. #pragma GCC diagnostic pop
  89. #endif