cmake/Source/cmFileLockPool.cxx

157 lines
3.8 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"
#include <assert.h>
2016-07-09 11:21:54 +02:00
#include "cmAlgorithms.h"
2015-04-27 22:25:09 +02:00
#include "cmFileLock.h"
#include "cmFileLockResult.h"
cmFileLockPool::cmFileLockPool()
{
}
cmFileLockPool::~cmFileLockPool()
{
cmDeleteAll(this->FunctionScopes);
cmDeleteAll(this->FileScopes);
}
void cmFileLockPool::PushFunctionScope()
{
this->FunctionScopes.push_back(new ScopePool());
}
void cmFileLockPool::PopFunctionScope()
{
assert(!this->FunctionScopes.empty());
delete this->FunctionScopes.back();
this->FunctionScopes.pop_back();
}
void cmFileLockPool::PushFileScope()
{
this->FileScopes.push_back(new ScopePool());
}
void cmFileLockPool::PopFileScope()
{
assert(!this->FileScopes.empty());
delete this->FileScopes.back();
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
}
2015-04-27 22:25:09 +02:00
return this->FunctionScopes.back()->Lock(filename, timeoutSec);
}
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());
return this->FileScopes.back()->Lock(filename, timeoutSec);
}
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) {
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) {
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) {
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) {
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);
}
cmFileLockPool::ScopePool::ScopePool()
{
}
cmFileLockPool::ScopePool::~ScopePool()
{
cmDeleteAll(this->Locks);
}
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
{
2016-07-09 11:21:54 +02:00
cmFileLock* lock = new cmFileLock();
2015-04-27 22:25:09 +02:00
const cmFileLockResult result = lock->Lock(filename, timeoutSec);
2016-07-09 11:21:54 +02:00
if (result.IsOk()) {
2015-04-27 22:25:09 +02:00
this->Locks.push_back(lock);
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
delete lock;
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) {
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
{
2018-01-26 17:06:56 +01:00
for (auto const& lock : this->Locks) {
if (lock->IsLocked(filename)) {
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 false;
}