|
|
|||
File indexing completed on 2026-05-10 08:44:32
0001 //===--- LockFileManager.h - File-level locking utility ---------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 #ifndef LLVM_SUPPORT_LOCKFILEMANAGER_H 0009 #define LLVM_SUPPORT_LOCKFILEMANAGER_H 0010 0011 #include "llvm/ADT/SmallString.h" 0012 #include <optional> 0013 #include <system_error> 0014 #include <utility> // for std::pair 0015 0016 namespace llvm { 0017 class StringRef; 0018 0019 /// Class that manages the creation of a lock file to aid 0020 /// implicit coordination between different processes. 0021 /// 0022 /// The implicit coordination works by creating a ".lock" file alongside 0023 /// the file that we're coordinating for, using the atomicity of the file 0024 /// system to ensure that only a single process can create that ".lock" file. 0025 /// When the lock file is removed, the owning process has finished the 0026 /// operation. 0027 class LockFileManager { 0028 public: 0029 /// Describes the state of a lock file. 0030 enum LockFileState { 0031 /// The lock file has been created and is owned by this instance 0032 /// of the object. 0033 LFS_Owned, 0034 /// The lock file already exists and is owned by some other 0035 /// instance. 0036 LFS_Shared, 0037 /// An error occurred while trying to create or find the lock 0038 /// file. 0039 LFS_Error 0040 }; 0041 0042 /// Describes the result of waiting for the owner to release the lock. 0043 enum WaitForUnlockResult { 0044 /// The lock was released successfully. 0045 Res_Success, 0046 /// Owner died while holding the lock. 0047 Res_OwnerDied, 0048 /// Reached timeout while waiting for the owner to release the lock. 0049 Res_Timeout 0050 }; 0051 0052 private: 0053 SmallString<128> FileName; 0054 SmallString<128> LockFileName; 0055 SmallString<128> UniqueLockFileName; 0056 0057 std::optional<std::pair<std::string, int>> Owner; 0058 std::error_code ErrorCode; 0059 std::string ErrorDiagMsg; 0060 0061 LockFileManager(const LockFileManager &) = delete; 0062 LockFileManager &operator=(const LockFileManager &) = delete; 0063 0064 static std::optional<std::pair<std::string, int>> 0065 readLockFile(StringRef LockFileName); 0066 0067 static bool processStillExecuting(StringRef Hostname, int PID); 0068 0069 public: 0070 0071 LockFileManager(StringRef FileName); 0072 ~LockFileManager(); 0073 0074 /// Determine the state of the lock file. 0075 LockFileState getState() const; 0076 0077 operator LockFileState() const { return getState(); } 0078 0079 /// For a shared lock, wait until the owner releases the lock. 0080 /// Total timeout for the file to appear is ~1.5 minutes. 0081 /// \param MaxSeconds the maximum total wait time in seconds. 0082 WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds = 90); 0083 0084 /// Remove the lock file. This may delete a different lock file than 0085 /// the one previously read if there is a race. 0086 std::error_code unsafeRemoveLockFile(); 0087 0088 /// Get error message, or "" if there is no error. 0089 std::string getErrorMessage() const; 0090 0091 /// Set error and error message 0092 void setError(const std::error_code &EC, StringRef ErrorMsg = "") { 0093 ErrorCode = EC; 0094 ErrorDiagMsg = ErrorMsg.str(); 0095 } 0096 }; 0097 0098 } // end namespace llvm 0099 0100 #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|