LLVM 20.0.0git
LockFileManager.h
Go to the documentation of this file.
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
10
12#include <optional>
13#include <system_error>
14#include <utility> // for std::pair
15
16namespace llvm {
17class StringRef;
18
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.
28public:
29 /// Describes the state of a lock file.
31 /// The lock file has been created and is owned by this instance
32 /// of the object.
34 /// The lock file already exists and is owned by some other
35 /// instance.
37 /// An error occurred while trying to create or find the lock
38 /// file.
40 };
41
42 /// Describes the result of waiting for the owner to release the lock.
44 /// The lock was released successfully.
46 /// Owner died while holding the lock.
48 /// Reached timeout while waiting for the owner to release the lock.
50 };
51
52private:
53 SmallString<128> FileName;
54 SmallString<128> LockFileName;
55 SmallString<128> UniqueLockFileName;
56
57 std::optional<std::pair<std::string, int>> Owner;
58 std::error_code ErrorCode;
59 std::string ErrorDiagMsg;
60
61 LockFileManager(const LockFileManager &) = delete;
62 LockFileManager &operator=(const LockFileManager &) = delete;
63
64 static std::optional<std::pair<std::string, int>>
65 readLockFile(StringRef LockFileName);
66
67 static bool processStillExecuting(StringRef Hostname, int PID);
68
69public:
70
71 LockFileManager(StringRef FileName);
73
74 /// Determine the state of the lock file.
75 LockFileState getState() const;
76
77 operator LockFileState() const { return getState(); }
78
79 /// For a shared lock, wait until the owner releases the lock.
80 /// Total timeout for the file to appear is ~1.5 minutes.
81 /// \param MaxSeconds the maximum total wait time in seconds.
82 WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds = 90);
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 void setError(const std::error_code &EC, StringRef ErrorMsg = "") {
93 ErrorCode = EC;
94 ErrorDiagMsg = ErrorMsg.str();
95 }
96};
97
98} // end namespace llvm
99
100#endif // LLVM_SUPPORT_LOCKFILEMANAGER_H
This file defines the SmallString class.
Class that manages the creation of a lock file to aid implicit coordination between different process...
LockFileState
Describes the state of a lock file.
@ LFS_Error
An error occurred while trying to create or find the lock file.
@ LFS_Owned
The lock file has been created and is owned by this instance of the object.
@ LFS_Shared
The lock file already exists and is owned by some other instance.
std::string getErrorMessage() const
Get error message, or "" if there is no error.
void setError(const std::error_code &EC, StringRef ErrorMsg="")
Set error and error message.
std::error_code unsafeRemoveLockFile()
Remove the lock file.
WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds=90)
For a shared lock, wait until the owner releases the lock.
WaitForUnlockResult
Describes the result of waiting for the owner to release the lock.
@ Res_Success
The lock was released successfully.
@ Res_Timeout
Reached timeout while waiting for the owner to release the lock.
@ Res_OwnerDied
Owner died while holding the lock.
LockFileState getState() const
Determine the state of the lock file.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18