[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / include / llvm / Support / LockFileManager.h
blob57e4fbd84cd954630b846d4424a0e91813341119
1 //===--- LockFileManager.h - File-level locking utility ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_SUPPORT_LOCKFILEMANAGER_H
9 #define LLVM_SUPPORT_LOCKFILEMANAGER_H
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/SmallString.h"
13 #include <system_error>
14 #include <utility> // for std::pair
16 namespace llvm {
17 class StringRef;
19 /// Class that manages the creation of a lock file to aid
20 /// implicit coordination between different processes.
21 ///
22 /// The implicit coordination works by creating a ".lock" file alongside
23 /// the file that we're coordinating for, using the atomicity of the file
24 /// system to ensure that only a single process can create that ".lock" file.
25 /// When the lock file is removed, the owning process has finished the
26 /// operation.
27 class LockFileManager {
28 public:
29 /// Describes the state of a lock file.
30 enum LockFileState {
31 /// The lock file has been created and is owned by this instance
32 /// of the object.
33 LFS_Owned,
34 /// The lock file already exists and is owned by some other
35 /// instance.
36 LFS_Shared,
37 /// An error occurred while trying to create or find the lock
38 /// file.
39 LFS_Error
42 /// Describes the result of waiting for the owner to release the lock.
43 enum WaitForUnlockResult {
44 /// The lock was released successfully.
45 Res_Success,
46 /// Owner died while holding the lock.
47 Res_OwnerDied,
48 /// Reached timeout while waiting for the owner to release the lock.
49 Res_Timeout
52 private:
53 SmallString<128> FileName;
54 SmallString<128> LockFileName;
55 SmallString<128> UniqueLockFileName;
57 Optional<std::pair<std::string, int> > Owner;
58 std::error_code ErrorCode;
59 std::string ErrorDiagMsg;
61 LockFileManager(const LockFileManager &) = delete;
62 LockFileManager &operator=(const LockFileManager &) = delete;
64 static Optional<std::pair<std::string, int> >
65 readLockFile(StringRef LockFileName);
67 static bool processStillExecuting(StringRef Hostname, int PID);
69 public:
71 LockFileManager(StringRef FileName);
72 ~LockFileManager();
74 /// Determine the state of the lock file.
75 LockFileState getState() const;
77 operator LockFileState() const { return getState(); }
79 /// For a shared lock, wait until the owner releases the lock.
80 WaitForUnlockResult waitForUnlock();
82 /// Remove the lock file. This may delete a different lock file than
83 /// the one previously read if there is a race.
84 std::error_code unsafeRemoveLockFile();
86 /// Get error message, or "" if there is no error.
87 std::string getErrorMessage() const;
89 /// Set error and error message
90 void setError(const std::error_code &EC, StringRef ErrorMsg = "") {
91 ErrorCode = EC;
92 ErrorDiagMsg = ErrorMsg.str();
96 } // end namespace llvm
98 #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H