cmFileLockPool.cxx 3.8 KB

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