cmFileLockPool.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "cmFileLock.h"
  13. #include "cmFileLockResult.h"
  14. #include "cmAlgorithms.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(
  44. const std::string& filename, unsigned long timeoutSec)
  45. {
  46. if (this->IsAlreadyLocked(filename))
  47. {
  48. return cmFileLockResult::MakeAlreadyLocked();
  49. }
  50. if (this->FunctionScopes.empty())
  51. {
  52. return cmFileLockResult::MakeNoFunction();
  53. }
  54. return this->FunctionScopes.back()->Lock(filename, timeoutSec);
  55. }
  56. cmFileLockResult cmFileLockPool::LockFileScope(
  57. const std::string& filename, unsigned long timeoutSec)
  58. {
  59. if (this->IsAlreadyLocked(filename))
  60. {
  61. return cmFileLockResult::MakeAlreadyLocked();
  62. }
  63. assert(!this->FileScopes.empty());
  64. return this->FileScopes.back()->Lock(filename, timeoutSec);
  65. }
  66. cmFileLockResult cmFileLockPool::LockProcessScope(
  67. const std::string& filename, unsigned long timeoutSec)
  68. {
  69. if (this->IsAlreadyLocked(filename))
  70. {
  71. return cmFileLockResult::MakeAlreadyLocked();
  72. }
  73. return this->ProcessScope.Lock(filename, timeoutSec);
  74. }
  75. cmFileLockResult cmFileLockPool::Release(const std::string& filename)
  76. {
  77. for (It i = this->FunctionScopes.begin();
  78. i != this->FunctionScopes.end(); ++i)
  79. {
  80. const cmFileLockResult result = (*i)->Release(filename);
  81. if (!result.IsOk())
  82. {
  83. return result;
  84. }
  85. }
  86. for (It i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i)
  87. {
  88. const cmFileLockResult result = (*i)->Release(filename);
  89. if (!result.IsOk())
  90. {
  91. return result;
  92. }
  93. }
  94. return this->ProcessScope.Release(filename);
  95. }
  96. bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
  97. {
  98. for (CIt i = this->FunctionScopes.begin();
  99. i != this->FunctionScopes.end(); ++i)
  100. {
  101. const bool result = (*i)->IsAlreadyLocked(filename);
  102. if (result)
  103. {
  104. return true;
  105. }
  106. }
  107. for (CIt i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i)
  108. {
  109. const bool result = (*i)->IsAlreadyLocked(filename);
  110. if (result)
  111. {
  112. return true;
  113. }
  114. }
  115. return this->ProcessScope.IsAlreadyLocked(filename);
  116. }
  117. cmFileLockPool::ScopePool::ScopePool()
  118. {
  119. }
  120. cmFileLockPool::ScopePool::~ScopePool()
  121. {
  122. cmDeleteAll(this->Locks);
  123. }
  124. cmFileLockResult cmFileLockPool::ScopePool::Lock(
  125. const std::string& filename, unsigned long timeoutSec)
  126. {
  127. cmFileLock *lock = new cmFileLock();
  128. const cmFileLockResult result = lock->Lock(filename, timeoutSec);
  129. if (result.IsOk())
  130. {
  131. this->Locks.push_back(lock);
  132. return cmFileLockResult::MakeOk();
  133. }
  134. else
  135. {
  136. delete lock;
  137. return result;
  138. }
  139. }
  140. cmFileLockResult cmFileLockPool::ScopePool::Release(
  141. const std::string& filename)
  142. {
  143. for (It i = this->Locks.begin(); i != this->Locks.end(); ++i)
  144. {
  145. if ((*i)->IsLocked(filename))
  146. {
  147. return (*i)->Release();
  148. }
  149. }
  150. return cmFileLockResult::MakeOk();
  151. }
  152. bool cmFileLockPool::ScopePool::IsAlreadyLocked(
  153. const std::string& filename) const
  154. {
  155. for (CIt i = this->Locks.begin(); i != this->Locks.end(); ++i)
  156. {
  157. if ((*i)->IsLocked(filename))
  158. {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }