1234285Sdim//===--- LockFileManager.h - File-level locking utility ---------*- C++ -*-===//
2234285Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6234285Sdim//
7234285Sdim//===----------------------------------------------------------------------===//
8234285Sdim#ifndef LLVM_SUPPORT_LOCKFILEMANAGER_H
9234285Sdim#define LLVM_SUPPORT_LOCKFILEMANAGER_H
10234285Sdim
11234285Sdim#include "llvm/ADT/Optional.h"
12234285Sdim#include "llvm/ADT/SmallString.h"
13276479Sdim#include <system_error>
14234285Sdim#include <utility> // for std::pair
15234285Sdim
16234285Sdimnamespace llvm {
17309124Sdimclass StringRef;
18309124Sdim
19341825Sdim/// Class that manages the creation of a lock file to aid
20234285Sdim/// implicit coordination between different processes.
21234285Sdim///
22234285Sdim/// The implicit coordination works by creating a ".lock" file alongside
23234285Sdim/// the file that we're coordinating for, using the atomicity of the file
24234285Sdim/// system to ensure that only a single process can create that ".lock" file.
25234285Sdim/// When the lock file is removed, the owning process has finished the
26234285Sdim/// operation.
27234285Sdimclass LockFileManager {
28234285Sdimpublic:
29341825Sdim  /// Describes the state of a lock file.
30234285Sdim  enum LockFileState {
31341825Sdim    /// The lock file has been created and is owned by this instance
32234285Sdim    /// of the object.
33234285Sdim    LFS_Owned,
34341825Sdim    /// The lock file already exists and is owned by some other
35234285Sdim    /// instance.
36234285Sdim    LFS_Shared,
37341825Sdim    /// An error occurred while trying to create or find the lock
38234285Sdim    /// file.
39234285Sdim    LFS_Error
40234285Sdim  };
41234285Sdim
42341825Sdim  /// Describes the result of waiting for the owner to release the lock.
43276479Sdim  enum WaitForUnlockResult {
44341825Sdim    /// The lock was released successfully.
45276479Sdim    Res_Success,
46341825Sdim    /// Owner died while holding the lock.
47276479Sdim    Res_OwnerDied,
48341825Sdim    /// Reached timeout while waiting for the owner to release the lock.
49276479Sdim    Res_Timeout
50276479Sdim  };
51276479Sdim
52234285Sdimprivate:
53249423Sdim  SmallString<128> FileName;
54234285Sdim  SmallString<128> LockFileName;
55341825Sdim  SmallString<128> UniqueLockFileName;
56234285Sdim
57234285Sdim  Optional<std::pair<std::string, int> > Owner;
58327952Sdim  std::error_code ErrorCode;
59309124Sdim  std::string ErrorDiagMsg;
60234285Sdim
61288943Sdim  LockFileManager(const LockFileManager &) = delete;
62288943Sdim  LockFileManager &operator=(const LockFileManager &) = delete;
63234285Sdim
64234285Sdim  static Optional<std::pair<std::string, int> >
65234285Sdim  readLockFile(StringRef LockFileName);
66234285Sdim
67234285Sdim  static bool processStillExecuting(StringRef Hostname, int PID);
68234285Sdim
69234285Sdimpublic:
70234285Sdim
71234285Sdim  LockFileManager(StringRef FileName);
72234285Sdim  ~LockFileManager();
73234285Sdim
74341825Sdim  /// Determine the state of the lock file.
75234285Sdim  LockFileState getState() const;
76234285Sdim
77234285Sdim  operator LockFileState() const { return getState(); }
78234285Sdim
79341825Sdim  /// For a shared lock, wait until the owner releases the lock.
80360784Sdim  /// Total timeout for the file to appear is ~1.5 minutes.
81360784Sdim  /// \param MaxSeconds the maximum wait time per iteration in seconds.
82360784Sdim  WaitForUnlockResult waitForUnlock(const unsigned MaxSeconds = 40);
83288943Sdim
84341825Sdim  /// Remove the lock file.  This may delete a different lock file than
85288943Sdim  /// the one previously read if there is a race.
86288943Sdim  std::error_code unsafeRemoveLockFile();
87309124Sdim
88341825Sdim  /// Get error message, or "" if there is no error.
89309124Sdim  std::string getErrorMessage() const;
90309124Sdim
91341825Sdim  /// Set error and error message
92327952Sdim  void setError(const std::error_code &EC, StringRef ErrorMsg = "") {
93327952Sdim    ErrorCode = EC;
94309124Sdim    ErrorDiagMsg = ErrorMsg.str();
95309124Sdim  }
96234285Sdim};
97234285Sdim
98234285Sdim} // end namespace llvm
99234285Sdim
100234285Sdim#endif // LLVM_SUPPORT_LOCKFILEMANAGER_H
101