cmFileLockPool.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileLockPool.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <utility>
  7. #include "cmFileLock.h"
  8. #include "cmFileLockResult.h"
  9. cmFileLockPool::cmFileLockPool() = default;
  10. cmFileLockPool::~cmFileLockPool() = default;
  11. void cmFileLockPool::PushFunctionScope()
  12. {
  13. this->FunctionScopes.push_back(ScopePool());
  14. }
  15. void cmFileLockPool::PopFunctionScope()
  16. {
  17. assert(!this->FunctionScopes.empty());
  18. this->FunctionScopes.pop_back();
  19. }
  20. void cmFileLockPool::PushFileScope()
  21. {
  22. this->FileScopes.push_back(ScopePool());
  23. }
  24. void cmFileLockPool::PopFileScope()
  25. {
  26. assert(!this->FileScopes.empty());
  27. this->FileScopes.pop_back();
  28. }
  29. cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
  30. unsigned long timeoutSec)
  31. {
  32. if (this->IsAlreadyLocked(filename)) {
  33. return cmFileLockResult::MakeAlreadyLocked();
  34. }
  35. if (this->FunctionScopes.empty()) {
  36. return cmFileLockResult::MakeNoFunction();
  37. }
  38. return this->FunctionScopes.back().Lock(filename, timeoutSec);
  39. }
  40. cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
  41. unsigned long timeoutSec)
  42. {
  43. if (this->IsAlreadyLocked(filename)) {
  44. return cmFileLockResult::MakeAlreadyLocked();
  45. }
  46. assert(!this->FileScopes.empty());
  47. return this->FileScopes.back().Lock(filename, timeoutSec);
  48. }
  49. cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
  50. unsigned long timeoutSec)
  51. {
  52. if (this->IsAlreadyLocked(filename)) {
  53. return cmFileLockResult::MakeAlreadyLocked();
  54. }
  55. return this->ProcessScope.Lock(filename, timeoutSec);
  56. }
  57. cmFileLockResult cmFileLockPool::Release(const std::string& filename)
  58. {
  59. for (auto& funcScope : this->FunctionScopes) {
  60. const cmFileLockResult result = funcScope.Release(filename);
  61. if (!result.IsOk()) {
  62. return result;
  63. }
  64. }
  65. for (auto& fileScope : this->FileScopes) {
  66. const cmFileLockResult result = fileScope.Release(filename);
  67. if (!result.IsOk()) {
  68. return result;
  69. }
  70. }
  71. return this->ProcessScope.Release(filename);
  72. }
  73. bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
  74. {
  75. for (auto const& funcScope : this->FunctionScopes) {
  76. const bool result = funcScope.IsAlreadyLocked(filename);
  77. if (result) {
  78. return true;
  79. }
  80. }
  81. for (auto const& fileScope : this->FileScopes) {
  82. const bool result = fileScope.IsAlreadyLocked(filename);
  83. if (result) {
  84. return true;
  85. }
  86. }
  87. return this->ProcessScope.IsAlreadyLocked(filename);
  88. }
  89. cmFileLockPool::ScopePool::ScopePool() = default;
  90. cmFileLockPool::ScopePool::~ScopePool() = default;
  91. cmFileLockPool::ScopePool::ScopePool(ScopePool&&) noexcept = default;
  92. cmFileLockPool::ScopePool& cmFileLockPool::ScopePool::operator=(
  93. ScopePool&& other) noexcept
  94. {
  95. if (this != &other) {
  96. this->Locks = std::move(other.Locks);
  97. }
  98. return *this;
  99. }
  100. cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
  101. unsigned long timeoutSec)
  102. {
  103. cmFileLock lock;
  104. const cmFileLockResult result = lock.Lock(filename, timeoutSec);
  105. if (result.IsOk()) {
  106. this->Locks.push_back(std::move(lock));
  107. return cmFileLockResult::MakeOk();
  108. }
  109. return result;
  110. }
  111. cmFileLockResult cmFileLockPool::ScopePool::Release(
  112. const std::string& filename)
  113. {
  114. for (auto& lock : this->Locks) {
  115. if (lock.IsLocked(filename)) {
  116. return lock.Release();
  117. }
  118. }
  119. return cmFileLockResult::MakeOk();
  120. }
  121. bool cmFileLockPool::ScopePool::IsAlreadyLocked(
  122. const std::string& filename) const
  123. {
  124. return std::any_of(this->Locks.begin(), this->Locks.end(),
  125. [&filename](cmFileLock const& lock) -> bool {
  126. return lock.IsLocked(filename);
  127. });
  128. }