Use LLVM_DELETED_FUNCTION for copy constructors and copy assignment operators that...
[oota-llvm.git] / include / llvm / Support / LockFileManager.h
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 "llvm/Support/system_error.h"
16 #include <utility> // for std::pair
17
18 namespace llvm {
19
20 /// \brief Class that manages the creation of a lock file to aid
21 /// implicit coordination between different processes.
22 ///
23 /// The implicit coordination works by creating a ".lock" file alongside
24 /// the file that we're coordinating for, using the atomicity of the file
25 /// system to ensure that only a single process can create that ".lock" file.
26 /// When the lock file is removed, the owning process has finished the
27 /// operation.
28 class LockFileManager {
29 public:
30   /// \brief Describes the state of a lock file.
31   enum LockFileState {
32     /// \brief The lock file has been created and is owned by this instance
33     /// of the object.
34     LFS_Owned,
35     /// \brief The lock file already exists and is owned by some other
36     /// instance.
37     LFS_Shared,
38     /// \brief An error occurred while trying to create or find the lock
39     /// file.
40     LFS_Error
41   };
42
43 private:
44   SmallString<128> LockFileName;
45   SmallString<128> UniqueLockFileName;
46
47   Optional<std::pair<std::string, int> > Owner;
48   Optional<error_code> Error;
49
50   LockFileManager(const LockFileManager &) LLVM_DELETED_FUNCTION;
51   LockFileManager &operator=(const LockFileManager &) LLVM_DELETED_FUNCTION;
52
53   static Optional<std::pair<std::string, int> >
54   readLockFile(StringRef LockFileName);
55
56   static bool processStillExecuting(StringRef Hostname, int PID);
57
58 public:
59
60   LockFileManager(StringRef FileName);
61   ~LockFileManager();
62
63   /// \brief Determine the state of the lock file.
64   LockFileState getState() const;
65
66   operator LockFileState() const { return getState(); }
67
68   /// \brief For a shared lock, wait until the owner releases the lock.
69   void waitForUnlock();
70 };
71
72 } // end namespace llvm
73
74 #endif // LLVM_SUPPORT_LOCKFILEMANAGER_H