LLVM  3.7.0
LockFileManager.h
Go to the documentation of this file.
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 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <system_error>
16 #include <utility> // for std::pair
17 
18 namespace llvm {
19 /// \brief 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.
28 public:
29  /// \brief Describes the state of a lock file.
31  /// \brief The lock file has been created and is owned by this instance
32  /// of the object.
34  /// \brief The lock file already exists and is owned by some other
35  /// instance.
37  /// \brief An error occurred while trying to create or find the lock
38  /// file.
40  };
41 
42  /// \brief Describes the result of waiting for the owner to release the lock.
44  /// \brief The lock was released successfully.
46  /// \brief Owner died while holding the lock.
48  /// \brief Reached timeout while waiting for the owner to release the lock.
50  };
51 
52 private:
53  SmallString<128> FileName;
54  SmallString<128> LockFileName;
55  SmallString<128> UniqueLockFileName;
56 
59 
60  LockFileManager(const LockFileManager &) = delete;
61  LockFileManager &operator=(const LockFileManager &) = delete;
62 
64  readLockFile(StringRef LockFileName);
65 
66  static bool processStillExecuting(StringRef Hostname, int PID);
67 
68 public:
69 
70  LockFileManager(StringRef FileName);
72 
73  /// \brief Determine the state of the lock file.
74  LockFileState getState() const;
75 
76  operator LockFileState() const { return getState(); }
77 
78  /// \brief For a shared lock, wait until the owner releases the lock.
80 
81  /// \brief Remove the lock file. This may delete a different lock file than
82  /// the one previously read if there is a race.
83  std::error_code unsafeRemoveLockFile();
84 };
85 
86 } // end namespace llvm
87 
88 #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H
WaitForUnlockResult
Describes the result of waiting for the owner to release the lock.
The lock was released successfully.
An error occurred while trying to create or find the lock file.
std::error_code unsafeRemoveLockFile()
Remove the lock file.
LockFileState getState() const
Determine the state of the lock file.
LockFileState
Describes the state of a lock file.
Reached timeout while waiting for the owner to release the lock.
Owner died while holding the lock.
The lock file has been created and is owned by this instance of the object.
WaitForUnlockResult waitForUnlock()
For a shared lock, wait until the owner releases the lock.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Class that manages the creation of a lock file to aid implicit coordination between different process...
The lock file already exists and is owned by some other instance.