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"
9 #include "cmFileLock.h"
10 #include "cmFileLockResult.h"
12 cmFileLockPool::cmFileLockPool() = default;
14 cmFileLockPool::~cmFileLockPool() = default;
16 void cmFileLockPool::PushFunctionScope()
18 this->FunctionScopes.push_back(ScopePool());
21 void cmFileLockPool::PopFunctionScope()
23 assert(!this->FunctionScopes.empty());
24 this->FunctionScopes.pop_back();
27 void cmFileLockPool::PushFileScope()
29 this->FileScopes.push_back(ScopePool());
32 void cmFileLockPool::PopFileScope()
34 assert(!this->FileScopes.empty());
35 this->FileScopes.pop_back();
38 cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
39 unsigned long timeoutSec)
41 if (this->IsAlreadyLocked(filename)) {
42 return cmFileLockResult::MakeAlreadyLocked();
44 if (this->FunctionScopes.empty()) {
45 return cmFileLockResult::MakeNoFunction();
47 return this->FunctionScopes.back().Lock(filename, timeoutSec);
50 cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
51 unsigned long timeoutSec)
53 if (this->IsAlreadyLocked(filename)) {
54 return cmFileLockResult::MakeAlreadyLocked();
56 assert(!this->FileScopes.empty());
57 return this->FileScopes.back().Lock(filename, timeoutSec);
60 cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
61 unsigned long timeoutSec)
63 if (this->IsAlreadyLocked(filename)) {
64 return cmFileLockResult::MakeAlreadyLocked();
66 return this->ProcessScope.Lock(filename, timeoutSec);
69 cmFileLockResult cmFileLockPool::Release(const std::string& filename)
71 for (auto& funcScope : this->FunctionScopes) {
72 const cmFileLockResult result = funcScope.Release(filename);
78 for (auto& fileScope : this->FileScopes) {
79 const cmFileLockResult result = fileScope.Release(filename);
85 return this->ProcessScope.Release(filename);
88 bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
90 for (auto const& funcScope : this->FunctionScopes) {
91 const bool result = funcScope.IsAlreadyLocked(filename);
97 for (auto const& fileScope : this->FileScopes) {
98 const bool result = fileScope.IsAlreadyLocked(filename);
104 return this->ProcessScope.IsAlreadyLocked(filename);
107 cmFileLockPool::ScopePool::ScopePool() = default;
109 cmFileLockPool::ScopePool::~ScopePool() = default;
111 cmFileLockPool::ScopePool::ScopePool(ScopePool&&) noexcept = default;
113 cmFileLockPool::ScopePool& cmFileLockPool::ScopePool::operator=(
114 ScopePool&& other) noexcept
116 if (this != &other) {
117 this->Locks = std::move(other.Locks);
123 cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
124 unsigned long timeoutSec)
127 const cmFileLockResult result = lock.Lock(filename, timeoutSec);
129 this->Locks.push_back(std::move(lock));
130 return cmFileLockResult::MakeOk();
135 cmFileLockResult cmFileLockPool::ScopePool::Release(
136 const std::string& filename)
138 for (auto& lock : this->Locks) {
139 if (lock.IsLocked(filename)) {
140 return lock.Release();
143 return cmFileLockResult::MakeOk();
146 bool cmFileLockPool::ScopePool::IsAlreadyLocked(
147 const std::string& filename) const
149 return std::any_of(this->Locks.begin(), this->Locks.end(),
150 [&filename](cmFileLock const& lock) -> bool {
151 return lock.IsLocked(filename);