2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2014-11-26 01:49:25 +03:00
|
|
|
#include "cmFileLockPool.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmAlgorithms.h"
|
2014-11-26 01:49:25 +03:00
|
|
|
#include "cmFileLock.h"
|
|
|
|
#include "cmFileLockResult.h"
|
|
|
|
|
|
|
|
cmFileLockPool::cmFileLockPool()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockPool::~cmFileLockPool()
|
|
|
|
{
|
2015-01-04 15:33:16 +03:00
|
|
|
cmDeleteAll(this->FunctionScopes);
|
|
|
|
cmDeleteAll(this->FileScopes);
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-16 17:34:04 +03:00
|
|
|
cmFileLockResult cmFileLockPool::LockFunctionScope(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->IsAlreadyLocked(filename)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeAlreadyLocked();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->FunctionScopes.empty()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeNoFunction();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
return this->FunctionScopes.back()->Lock(filename, timeoutSec);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileLockResult cmFileLockPool::LockFileScope(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->IsAlreadyLocked(filename)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeAlreadyLocked();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
assert(!this->FileScopes.empty());
|
|
|
|
return this->FileScopes.back()->Lock(filename, timeoutSec);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileLockResult cmFileLockPool::LockProcessScope(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->IsAlreadyLocked(filename)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeAlreadyLocked();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
return this->ProcessScope.Lock(filename, timeoutSec);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockResult cmFileLockPool::Release(const std::string& filename)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (It i = this->FunctionScopes.begin(); i != this->FunctionScopes.end();
|
|
|
|
++i) {
|
2014-11-26 01:49:25 +03:00
|
|
|
const cmFileLockResult result = (*i)->Release(filename);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!result.IsOk()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return result;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (It i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i) {
|
2014-11-26 01:49:25 +03:00
|
|
|
const cmFileLockResult result = (*i)->Release(filename);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!result.IsOk()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return result;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
|
|
|
return this->ProcessScope.Release(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFileLockPool::IsAlreadyLocked(const std::string& filename) const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (CIt i = this->FunctionScopes.begin(); i != this->FunctionScopes.end();
|
|
|
|
++i) {
|
2014-11-26 01:49:25 +03:00
|
|
|
const bool result = (*i)->IsAlreadyLocked(filename);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (result) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (CIt i = this->FileScopes.begin(); i != this->FileScopes.end(); ++i) {
|
2014-11-26 01:49:25 +03:00
|
|
|
const bool result = (*i)->IsAlreadyLocked(filename);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (result) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
|
|
|
return this->ProcessScope.IsAlreadyLocked(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockPool::ScopePool::ScopePool()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockPool::ScopePool::~ScopePool()
|
|
|
|
{
|
2015-01-04 15:33:16 +03:00
|
|
|
cmDeleteAll(this->Locks);
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileLockResult cmFileLockPool::ScopePool::Lock(const std::string& filename,
|
|
|
|
unsigned long timeoutSec)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileLock* lock = new cmFileLock();
|
2014-11-26 01:49:25 +03:00
|
|
|
const cmFileLockResult result = lock->Lock(filename, timeoutSec);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (result.IsOk()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
this->Locks.push_back(lock);
|
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-08-18 21:36:29 +03:00
|
|
|
delete lock;
|
|
|
|
return result;
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockResult cmFileLockPool::ScopePool::Release(
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& filename)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (It i = this->Locks.begin(); i != this->Locks.end(); ++i) {
|
|
|
|
if ((*i)->IsLocked(filename)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return (*i)->Release();
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFileLockPool::ScopePool::IsAlreadyLocked(
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& filename) const
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (CIt i = this->Locks.begin(); i != this->Locks.end(); ++i) {
|
|
|
|
if ((*i)->IsLocked(filename)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
return false;
|
|
|
|
}
|