2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2014-11-25 22:49:25 +00:00
|
|
|
#include "cmFileLockPool.h"
|
|
|
|
|
2019-09-06 20:58:06 +00:00
|
|
|
#include <cassert>
|
2019-11-12 16:17:27 +00:00
|
|
|
#include <utility>
|
2014-11-25 22:49:25 +00:00
|
|
|
|
|
|
|
#include "cmFileLock.h"
|
|
|
|
#include "cmFileLockResult.h"
|
|
|
|
|
2019-01-22 22:44:50 +00:00
|
|
|
cmFileLockPool::cmFileLockPool() = default;
|
2014-11-25 22:49:25 +00:00
|
|
|
|
2019-11-12 16:17:27 +00:00
|
|
|
cmFileLockPool::~cmFileLockPool() = default;
|
2014-11-25 22:49:25 +00:00
|
|
|
|
|
|
|
void cmFileLockPool::PushFunctionScope()
|
|
|
|
{
|
2019-11-12 16:17:27 +00:00
|
|
|
this->FunctionScopes.push_back(ScopePool());
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmFileLockPool::PopFunctionScope()
|
|
|
|
{
|
|
|
|
assert(!this->FunctionScopes.empty());
|
|
|
|
this->FunctionScopes.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmFileLockPool::PushFileScope()
|
|
|
|
{
|
2019-11-12 16:17:27 +00:00
|
|
|
this->FileScopes.push_back(ScopePool());
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmFileLockPool::PopFileScope()
|
|
|
|
{
|
|
|
|
assert(!this->FileScopes.empty());
|
|
|
|
this->FileScopes.pop_back();
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->IsAlreadyLocked(filename)) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return cmFileLockResult::MakeAlreadyLocked();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (this->FunctionScopes.empty()) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return cmFileLockResult::MakeNoFunction();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2019-11-12 16:17:27 +00:00
|
|
|
return this->FunctionScopes.back().Lock(filename, timeoutSec);
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->IsAlreadyLocked(filename)) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return cmFileLockResult::MakeAlreadyLocked();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
assert(!this->FileScopes.empty());
|
2019-11-12 16:17:27 +00:00
|
|
|
return this->FileScopes.back().Lock(filename, timeoutSec);
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->IsAlreadyLocked(filename)) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return cmFileLockResult::MakeAlreadyLocked();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
return this->ProcessScope.Lock(filename, timeoutSec);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockResult cmFileLockPool::Release(const std::string& filename)
|
|
|
|
{
|
2017-09-11 10:40:26 +00:00
|
|
|
for (auto& funcScope : this->FunctionScopes) {
|
2019-11-12 16:17:27 +00:00
|
|
|
const cmFileLockResult result = funcScope.Release(filename);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!result.IsOk()) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
|
2017-09-11 10:40:26 +00:00
|
|
|
for (auto& fileScope : this->FileScopes) {
|
2019-11-12 16:17:27 +00:00
|
|
|
const cmFileLockResult result = fileScope.Release(filename);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!result.IsOk()) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
|
|
|
|
return this->ProcessScope.Release(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
|
|
|
|
{
|
2017-09-11 10:40:26 +00:00
|
|
|
for (auto const& funcScope : this->FunctionScopes) {
|
2019-11-12 16:17:27 +00:00
|
|
|
const bool result = funcScope.IsAlreadyLocked(filename);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (result) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
|
2017-09-11 10:40:26 +00:00
|
|
|
for (auto const& fileScope : this->FileScopes) {
|
2019-11-12 16:17:27 +00:00
|
|
|
const bool result = fileScope.IsAlreadyLocked(filename);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (result) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
|
|
|
|
return this->ProcessScope.IsAlreadyLocked(filename);
|
|
|
|
}
|
|
|
|
|
2019-01-22 22:44:50 +00:00
|
|
|
cmFileLockPool::ScopePool::ScopePool() = default;
|
2014-11-25 22:49:25 +00:00
|
|
|
|
2019-11-12 16:17:27 +00:00
|
|
|
cmFileLockPool::ScopePool::~ScopePool() = default;
|
|
|
|
|
|
|
|
cmFileLockPool::ScopePool::ScopePool(ScopePool&&) noexcept = default;
|
|
|
|
|
|
|
|
cmFileLockPool::ScopePool& cmFileLockPool::ScopePool::operator=(
|
|
|
|
ScopePool&& other) noexcept
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2019-11-12 16:17:27 +00:00
|
|
|
if (this != &other) {
|
|
|
|
this->Locks = std::move(other.Locks);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2019-11-12 16:17:27 +00:00
|
|
|
cmFileLock lock;
|
|
|
|
const cmFileLockResult result = lock.Lock(filename, timeoutSec);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (result.IsOk()) {
|
2019-11-12 16:17:27 +00:00
|
|
|
this->Locks.push_back(std::move(lock));
|
2014-11-25 22:49:25 +00:00
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2016-08-18 18:36:29 +00:00
|
|
|
return result;
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockResult cmFileLockPool::ScopePool::Release(
|
2016-05-16 14:34:04 +00:00
|
|
|
const std::string& filename)
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2017-09-11 10:40:26 +00:00
|
|
|
for (auto& lock : this->Locks) {
|
2019-11-12 16:17:27 +00:00
|
|
|
if (lock.IsLocked(filename)) {
|
|
|
|
return lock.Release();
|
2014-11-25 22:49:25 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
return cmFileLockResult::MakeOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFileLockPool::ScopePool::IsAlreadyLocked(
|
2016-05-16 14:34:04 +00:00
|
|
|
const std::string& filename) const
|
2014-11-25 22:49:25 +00:00
|
|
|
{
|
2017-09-11 10:40:26 +00:00
|
|
|
for (auto const& lock : this->Locks) {
|
2019-11-12 16:17:27 +00:00
|
|
|
if (lock.IsLocked(filename)) {
|
2014-11-25 22:49:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-11-25 22:49:25 +00:00
|
|
|
return false;
|
|
|
|
}
|