cmake/Source/cmFileLockPool.cxx

154 lines
3.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmFileLockPool.h"
2021-09-14 00:13:48 +02:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cassert>
2020-08-30 11:54:41 +02:00
#include <utility>
2015-04-27 22:25:09 +02:00
#include "cmFileLock.h"
#include "cmFileLockResult.h"
2019-11-11 23:01:05 +01:00
cmFileLockPool::cmFileLockPool() = default;
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
cmFileLockPool::~cmFileLockPool() = default;
2015-04-27 22:25:09 +02:00
void cmFileLockPool::PushFunctionScope()
{
2023-05-23 16:38:00 +02:00
this->FunctionScopes.emplace_back();
2015-04-27 22:25:09 +02:00
}
void cmFileLockPool::PopFunctionScope()
{
assert(!this->FunctionScopes.empty());
this->FunctionScopes.pop_back();
}
void cmFileLockPool::PushFileScope()
{
2023-05-23 16:38:00 +02:00
this->FileScopes.emplace_back();
2015-04-27 22:25:09 +02:00
}
void cmFileLockPool::PopFileScope()
{
assert(!this->FileScopes.empty());
this->FileScopes.pop_back();
}
2016-07-09 11:21:54 +02:00
cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
unsigned long timeoutSec)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (this->IsAlreadyLocked(filename)) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeAlreadyLocked();
2016-07-09 11:21:54 +02:00
}
if (this->FunctionScopes.empty()) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeNoFunction();
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
return this->FunctionScopes.back().Lock(filename, timeoutSec);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
unsigned long timeoutSec)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (this->IsAlreadyLocked(filename)) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeAlreadyLocked();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
assert(!this->FileScopes.empty());
2020-08-30 11:54:41 +02:00
return this->FileScopes.back().Lock(filename, timeoutSec);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
unsigned long timeoutSec)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (this->IsAlreadyLocked(filename)) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeAlreadyLocked();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return this->ProcessScope.Lock(filename, timeoutSec);
}
cmFileLockResult cmFileLockPool::Release(const std::string& filename)
{
2018-01-26 17:06:56 +01:00
for (auto& funcScope : this->FunctionScopes) {
2020-08-30 11:54:41 +02:00
const cmFileLockResult result = funcScope.Release(filename);
2016-07-09 11:21:54 +02:00
if (!result.IsOk()) {
2015-04-27 22:25:09 +02:00
return result;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
for (auto& fileScope : this->FileScopes) {
2020-08-30 11:54:41 +02:00
const cmFileLockResult result = fileScope.Release(filename);
2016-07-09 11:21:54 +02:00
if (!result.IsOk()) {
2015-04-27 22:25:09 +02:00
return result;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return this->ProcessScope.Release(filename);
}
bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
{
2018-01-26 17:06:56 +01:00
for (auto const& funcScope : this->FunctionScopes) {
2020-08-30 11:54:41 +02:00
const bool result = funcScope.IsAlreadyLocked(filename);
2016-07-09 11:21:54 +02:00
if (result) {
2015-04-27 22:25:09 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
for (auto const& fileScope : this->FileScopes) {
2020-08-30 11:54:41 +02:00
const bool result = fileScope.IsAlreadyLocked(filename);
2016-07-09 11:21:54 +02:00
if (result) {
2015-04-27 22:25:09 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return this->ProcessScope.IsAlreadyLocked(filename);
}
2019-11-11 23:01:05 +01:00
cmFileLockPool::ScopePool::ScopePool() = default;
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
cmFileLockPool::ScopePool::~ScopePool() = default;
cmFileLockPool::ScopePool::ScopePool(ScopePool&&) noexcept = default;
cmFileLockPool::ScopePool& cmFileLockPool::ScopePool::operator=(
ScopePool&& other) noexcept
2015-04-27 22:25:09 +02:00
{
2020-08-30 11:54:41 +02:00
if (this != &other) {
this->Locks = std::move(other.Locks);
}
return *this;
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
unsigned long timeoutSec)
2015-04-27 22:25:09 +02:00
{
2020-08-30 11:54:41 +02:00
cmFileLock lock;
const cmFileLockResult result = lock.Lock(filename, timeoutSec);
2016-07-09 11:21:54 +02:00
if (result.IsOk()) {
2020-08-30 11:54:41 +02:00
this->Locks.push_back(std::move(lock));
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
return result;
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLockPool::ScopePool::Release(
2016-07-09 11:21:54 +02:00
const std::string& filename)
2015-04-27 22:25:09 +02:00
{
2018-01-26 17:06:56 +01:00
for (auto& lock : this->Locks) {
2020-08-30 11:54:41 +02:00
if (lock.IsLocked(filename)) {
return lock.Release();
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
}
bool cmFileLockPool::ScopePool::IsAlreadyLocked(
2016-07-09 11:21:54 +02:00
const std::string& filename) const
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
return std::any_of(this->Locks.begin(), this->Locks.end(),
[&filename](cmFileLock const& lock) -> bool {
return lock.IsLocked(filename);
});
2015-04-27 22:25:09 +02:00
}