cmFileLockPool.h 2.0 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. class cmFileLock;
  9. class cmFileLockResult;
  10. class cmFileLockPool
  11. {
  12. CM_DISABLE_COPY(cmFileLockPool)
  13. public:
  14. cmFileLockPool();
  15. ~cmFileLockPool();
  16. //@{
  17. /**
  18. * @brief Function scope control.
  19. */
  20. void PushFunctionScope();
  21. void PopFunctionScope();
  22. //@}
  23. //@{
  24. /**
  25. * @brief File scope control.
  26. */
  27. void PushFileScope();
  28. void PopFileScope();
  29. //@}
  30. //@{
  31. /**
  32. * @brief Lock the file in given scope.
  33. * @param timeoutSec Lock timeout. If -1 try until success or fatal error.
  34. */
  35. cmFileLockResult LockFunctionScope(const std::string& filename,
  36. unsigned long timeoutSec);
  37. cmFileLockResult LockFileScope(const std::string& filename,
  38. unsigned long timeoutSec);
  39. cmFileLockResult LockProcessScope(const std::string& filename,
  40. unsigned long timeoutSec);
  41. //@}
  42. /**
  43. * @brief Unlock the file explicitly.
  44. */
  45. cmFileLockResult Release(const std::string& filename);
  46. private:
  47. bool IsAlreadyLocked(const std::string& filename) const;
  48. class ScopePool
  49. {
  50. CM_DISABLE_COPY(ScopePool)
  51. public:
  52. ScopePool();
  53. ~ScopePool();
  54. cmFileLockResult Lock(const std::string& filename,
  55. unsigned long timeoutSec);
  56. cmFileLockResult Release(const std::string& filename);
  57. bool IsAlreadyLocked(const std::string& filename) const;
  58. private:
  59. typedef std::vector<cmFileLock*> List;
  60. typedef List::iterator It;
  61. typedef List::const_iterator CIt;
  62. List Locks;
  63. };
  64. typedef std::vector<ScopePool*> List;
  65. typedef List::iterator It;
  66. typedef List::const_iterator CIt;
  67. List FunctionScopes;
  68. List FileScopes;
  69. ScopePool ProcessScope;
  70. };
  71. #endif // cmFileLockPool_h