cmFileLockPool.cxx 4.3 KB

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