cmFileLockPool.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFileLockPool_h
  4. #define cmFileLockPool_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <string>
  7. #include <vector>
  8. #include "cmFileLock.h"
  9. class cmFileLockResult;
  10. class cmFileLockPool
  11. {
  12. public:
  13. cmFileLockPool();
  14. ~cmFileLockPool();
  15. cmFileLockPool(cmFileLockPool const&) = delete;
  16. cmFileLockPool& operator=(cmFileLockPool const&) = delete;
  17. //@{
  18. /**
  19. * @brief Function scope control.
  20. */
  21. void PushFunctionScope();
  22. void PopFunctionScope();
  23. //@}
  24. //@{
  25. /**
  26. * @brief File scope control.
  27. */
  28. void PushFileScope();
  29. void PopFileScope();
  30. //@}
  31. //@{
  32. /**
  33. * @brief Lock the file in given scope.
  34. * @param timeoutSec Lock timeout. If -1 try until success or fatal error.
  35. */
  36. cmFileLockResult LockFunctionScope(const std::string& filename,
  37. unsigned long timeoutSec);
  38. cmFileLockResult LockFileScope(const std::string& filename,
  39. unsigned long timeoutSec);
  40. cmFileLockResult LockProcessScope(const std::string& filename,
  41. unsigned long timeoutSec);
  42. //@}
  43. /**
  44. * @brief Unlock the file explicitly.
  45. */
  46. cmFileLockResult Release(const std::string& filename);
  47. private:
  48. bool IsAlreadyLocked(const std::string& filename) const;
  49. class ScopePool
  50. {
  51. public:
  52. ScopePool();
  53. ~ScopePool();
  54. ScopePool(ScopePool const&) = delete;
  55. ScopePool(ScopePool&&) noexcept;
  56. ScopePool& operator=(ScopePool const&) = delete;
  57. ScopePool& operator=(ScopePool&&) noexcept;
  58. cmFileLockResult Lock(const std::string& filename,
  59. unsigned long timeoutSec);
  60. cmFileLockResult Release(const std::string& filename);
  61. bool IsAlreadyLocked(const std::string& filename) const;
  62. private:
  63. using List = std::vector<cmFileLock>;
  64. List Locks;
  65. };
  66. using List = std::vector<ScopePool>;
  67. List FunctionScopes;
  68. List FileScopes;
  69. ScopePool ProcessScope;
  70. };
  71. #endif // cmFileLockPool_h