cmFileLockPool.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <assert.h>
  5. #include "cmAlgorithms.h"
  6. #include "cmFileLock.h"
  7. #include "cmFileLockResult.h"
  8. cmFileLockPool::cmFileLockPool() = default;
  9. cmFileLockPool::~cmFileLockPool()
  10. {
  11. cmDeleteAll(this->FunctionScopes);
  12. cmDeleteAll(this->FileScopes);
  13. }
  14. void cmFileLockPool::PushFunctionScope()
  15. {
  16. this->FunctionScopes.push_back(new ScopePool());
  17. }
  18. void cmFileLockPool::PopFunctionScope()
  19. {
  20. assert(!this->FunctionScopes.empty());
  21. delete this->FunctionScopes.back();
  22. this->FunctionScopes.pop_back();
  23. }
  24. void cmFileLockPool::PushFileScope()
  25. {
  26. this->FileScopes.push_back(new ScopePool());
  27. }
  28. void cmFileLockPool::PopFileScope()
  29. {
  30. assert(!this->FileScopes.empty());
  31. delete this->FileScopes.back();
  32. this->FileScopes.pop_back();
  33. }
  34. cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
  35. unsigned long timeoutSec)
  36. {
  37. if (this->IsAlreadyLocked(filename)) {
  38. return cmFileLockResult::MakeAlreadyLocked();
  39. }
  40. if (this->FunctionScopes.empty()) {
  41. return cmFileLockResult::MakeNoFunction();
  42. }
  43. return this->FunctionScopes.back()->Lock(filename, timeoutSec);
  44. }
  45. cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
  46. unsigned long timeoutSec)
  47. {
  48. if (this->IsAlreadyLocked(filename)) {
  49. return cmFileLockResult::MakeAlreadyLocked();
  50. }
  51. assert(!this->FileScopes.empty());
  52. return this->FileScopes.back()->Lock(filename, timeoutSec);
  53. }
  54. cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
  55. unsigned long timeoutSec)
  56. {
  57. if (this->IsAlreadyLocked(filename)) {
  58. return cmFileLockResult::MakeAlreadyLocked();
  59. }
  60. return this->ProcessScope.Lock(filename, timeoutSec);
  61. }
  62. cmFileLockResult cmFileLockPool::Release(const std::string& filename)
  63. {
  64. for (auto& funcScope : this->FunctionScopes) {
  65. const cmFileLockResult result = funcScope->Release(filename);
  66. if (!result.IsOk()) {
  67. return result;
  68. }
  69. }
  70. for (auto& fileScope : this->FileScopes) {
  71. const cmFileLockResult result = fileScope->Release(filename);
  72. if (!result.IsOk()) {
  73. return result;
  74. }
  75. }
  76. return this->ProcessScope.Release(filename);
  77. }
  78. bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
  79. {
  80. for (auto const& funcScope : this->FunctionScopes) {
  81. const bool result = funcScope->IsAlreadyLocked(filename);
  82. if (result) {
  83. return true;
  84. }
  85. }
  86. for (auto const& fileScope : this->FileScopes) {
  87. const bool result = fileScope->IsAlreadyLocked(filename);
  88. if (result) {
  89. return true;
  90. }
  91. }
  92. return this->ProcessScope.IsAlreadyLocked(filename);
  93. }
  94. cmFileLockPool::ScopePool::ScopePool() = default;
  95. cmFileLockPool::ScopePool::~ScopePool()
  96. {
  97. cmDeleteAll(this->Locks);
  98. }
  99. cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
  100. unsigned long timeoutSec)
  101. {
  102. cmFileLock* lock = new cmFileLock();
  103. const cmFileLockResult result = lock->Lock(filename, timeoutSec);
  104. if (result.IsOk()) {
  105. this->Locks.push_back(lock);
  106. return cmFileLockResult::MakeOk();
  107. }
  108. delete lock;
  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. for (auto const& lock : this->Locks) {
  125. if (lock->IsLocked(filename)) {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }