cmFileLockPool.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. cmFileLockPool::cmFileLockPool()
  15. {
  16. }
  17. cmFileLockPool::~cmFileLockPool()
  18. {
  19. for (It i = this->FunctionScopes.begin();
  20. i != this->FunctionScopes.end(); ++i)
  21. {
  22. delete *i;
  23. }
  24. for (It i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i)
  25. {
  26. delete *i;
  27. }
  28. }
  29. void cmFileLockPool::PushFunctionScope()
  30. {
  31. this->FunctionScopes.push_back(new ScopePool());
  32. }
  33. void cmFileLockPool::PopFunctionScope()
  34. {
  35. assert(!this->FunctionScopes.empty());
  36. delete this->FunctionScopes.back();
  37. this->FunctionScopes.pop_back();
  38. }
  39. void cmFileLockPool::PushFileScope()
  40. {
  41. this->FileScopes.push_back(new ScopePool());
  42. }
  43. void cmFileLockPool::PopFileScope()
  44. {
  45. assert(!this->FileScopes.empty());
  46. delete this->FileScopes.back();
  47. this->FileScopes.pop_back();
  48. }
  49. cmFileLockResult cmFileLockPool::LockFunctionScope(
  50. const std::string& filename, unsigned long timeoutSec)
  51. {
  52. if (this->IsAlreadyLocked(filename))
  53. {
  54. return cmFileLockResult::MakeAlreadyLocked();
  55. }
  56. if (this->FunctionScopes.empty())
  57. {
  58. return cmFileLockResult::MakeNoFunction();
  59. }
  60. return this->FunctionScopes.back()->Lock(filename, timeoutSec);
  61. }
  62. cmFileLockResult cmFileLockPool::LockFileScope(
  63. const std::string& filename, unsigned long timeoutSec)
  64. {
  65. if (this->IsAlreadyLocked(filename))
  66. {
  67. return cmFileLockResult::MakeAlreadyLocked();
  68. }
  69. assert(!this->FileScopes.empty());
  70. return this->FileScopes.back()->Lock(filename, timeoutSec);
  71. }
  72. cmFileLockResult cmFileLockPool::LockProcessScope(
  73. const std::string& filename, unsigned long timeoutSec)
  74. {
  75. if (this->IsAlreadyLocked(filename))
  76. {
  77. return cmFileLockResult::MakeAlreadyLocked();
  78. }
  79. return this->ProcessScope.Lock(filename, timeoutSec);
  80. }
  81. cmFileLockResult cmFileLockPool::Release(const std::string& filename)
  82. {
  83. for (It i = this->FunctionScopes.begin();
  84. i != this->FunctionScopes.end(); ++i)
  85. {
  86. const cmFileLockResult result = (*i)->Release(filename);
  87. if (!result.IsOk())
  88. {
  89. return result;
  90. }
  91. }
  92. for (It i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i)
  93. {
  94. const cmFileLockResult result = (*i)->Release(filename);
  95. if (!result.IsOk())
  96. {
  97. return result;
  98. }
  99. }
  100. return this->ProcessScope.Release(filename);
  101. }
  102. bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
  103. {
  104. for (CIt i = this->FunctionScopes.begin();
  105. i != this->FunctionScopes.end(); ++i)
  106. {
  107. const bool result = (*i)->IsAlreadyLocked(filename);
  108. if (result)
  109. {
  110. return true;
  111. }
  112. }
  113. for (CIt i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i)
  114. {
  115. const bool result = (*i)->IsAlreadyLocked(filename);
  116. if (result)
  117. {
  118. return true;
  119. }
  120. }
  121. return this->ProcessScope.IsAlreadyLocked(filename);
  122. }
  123. cmFileLockPool::ScopePool::ScopePool()
  124. {
  125. }
  126. cmFileLockPool::ScopePool::~ScopePool()
  127. {
  128. for (It i = this->Locks.begin(); i != this->Locks.end(); ++i)
  129. {
  130. delete *i;
  131. }
  132. }
  133. cmFileLockResult cmFileLockPool::ScopePool::Lock(
  134. const std::string& filename, unsigned long timeoutSec)
  135. {
  136. cmFileLock *lock = new cmFileLock();
  137. const cmFileLockResult result = lock->Lock(filename, timeoutSec);
  138. if (result.IsOk())
  139. {
  140. this->Locks.push_back(lock);
  141. return cmFileLockResult::MakeOk();
  142. }
  143. else
  144. {
  145. delete lock;
  146. return result;
  147. }
  148. }
  149. cmFileLockResult cmFileLockPool::ScopePool::Release(
  150. const std::string& filename)
  151. {
  152. for (It i = this->Locks.begin(); i != this->Locks.end(); ++i)
  153. {
  154. if ((*i)->IsLocked(filename))
  155. {
  156. return (*i)->Release();
  157. }
  158. }
  159. return cmFileLockResult::MakeOk();
  160. }
  161. bool cmFileLockPool::ScopePool::IsAlreadyLocked(
  162. const std::string& filename) const
  163. {
  164. for (CIt i = this->Locks.begin(); i != this->Locks.end(); ++i)
  165. {
  166. if ((*i)->IsLocked(filename))
  167. {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }