Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

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