cmFileLockPool.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2014 Ruslan Baratov
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFileLockPool.h"
  11. #include <assert.h>
  12. #include "cmAlgorithms.h"
  13. #include "cmFileLock.h"
  14. #include "cmFileLockResult.h"
  15. cmFileLockPool::cmFileLockPool()
  16. {
  17. }
  18. cmFileLockPool::~cmFileLockPool()
  19. {
  20. cmDeleteAll(this->FunctionScopes);
  21. cmDeleteAll(this->FileScopes);
  22. }
  23. void cmFileLockPool::PushFunctionScope()
  24. {
  25. this->FunctionScopes.push_back(new ScopePool());
  26. }
  27. void cmFileLockPool::PopFunctionScope()
  28. {
  29. assert(!this->FunctionScopes.empty());
  30. delete this->FunctionScopes.back();
  31. this->FunctionScopes.pop_back();
  32. }
  33. void cmFileLockPool::PushFileScope()
  34. {
  35. this->FileScopes.push_back(new ScopePool());
  36. }
  37. void cmFileLockPool::PopFileScope()
  38. {
  39. assert(!this->FileScopes.empty());
  40. delete this->FileScopes.back();
  41. this->FileScopes.pop_back();
  42. }
  43. cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
  44. unsigned long timeoutSec)
  45. {
  46. if (this->IsAlreadyLocked(filename)) {
  47. return cmFileLockResult::MakeAlreadyLocked();
  48. }
  49. if (this->FunctionScopes.empty()) {
  50. return cmFileLockResult::MakeNoFunction();
  51. }
  52. return this->FunctionScopes.back()->Lock(filename, timeoutSec);
  53. }
  54. cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
  55. unsigned long timeoutSec)
  56. {
  57. if (this->IsAlreadyLocked(filename)) {
  58. return cmFileLockResult::MakeAlreadyLocked();
  59. }
  60. assert(!this->FileScopes.empty());
  61. return this->FileScopes.back()->Lock(filename, timeoutSec);
  62. }
  63. cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
  64. unsigned long timeoutSec)
  65. {
  66. if (this->IsAlreadyLocked(filename)) {
  67. return cmFileLockResult::MakeAlreadyLocked();
  68. }
  69. return this->ProcessScope.Lock(filename, timeoutSec);
  70. }
  71. cmFileLockResult cmFileLockPool::Release(const std::string& filename)
  72. {
  73. for (It i = this->FunctionScopes.begin(); i != this->FunctionScopes.end();
  74. ++i) {
  75. const cmFileLockResult result = (*i)->Release(filename);
  76. if (!result.IsOk()) {
  77. return result;
  78. }
  79. }
  80. for (It i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i) {
  81. const cmFileLockResult result = (*i)->Release(filename);
  82. if (!result.IsOk()) {
  83. return result;
  84. }
  85. }
  86. return this->ProcessScope.Release(filename);
  87. }
  88. bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
  89. {
  90. for (CIt i = this->FunctionScopes.begin(); i != this->FunctionScopes.end();
  91. ++i) {
  92. const bool result = (*i)->IsAlreadyLocked(filename);
  93. if (result) {
  94. return true;
  95. }
  96. }
  97. for (CIt i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i) {
  98. const bool result = (*i)->IsAlreadyLocked(filename);
  99. if (result) {
  100. return true;
  101. }
  102. }
  103. return this->ProcessScope.IsAlreadyLocked(filename);
  104. }
  105. cmFileLockPool::ScopePool::ScopePool()
  106. {
  107. }
  108. cmFileLockPool::ScopePool::~ScopePool()
  109. {
  110. cmDeleteAll(this->Locks);
  111. }
  112. cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
  113. unsigned long timeoutSec)
  114. {
  115. cmFileLock* lock = new cmFileLock();
  116. const cmFileLockResult result = lock->Lock(filename, timeoutSec);
  117. if (result.IsOk()) {
  118. this->Locks.push_back(lock);
  119. return cmFileLockResult::MakeOk();
  120. }
  121. delete lock;
  122. return result;
  123. }
  124. cmFileLockResult cmFileLockPool::ScopePool::Release(
  125. const std::string& filename)
  126. {
  127. for (It i = this->Locks.begin(); i != this->Locks.end(); ++i) {
  128. if ((*i)->IsLocked(filename)) {
  129. return (*i)->Release();
  130. }
  131. }
  132. return cmFileLockResult::MakeOk();
  133. }
  134. bool cmFileLockPool::ScopePool::IsAlreadyLocked(
  135. const std::string& filename) const
  136. {
  137. for (CIt i = this->Locks.begin(); i != this->Locks.end(); ++i) {
  138. if ((*i)->IsLocked(filename)) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }