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 "cmFileLock.h"
|
|
|
|
|
|
|
|
#include "cmFileLockResult.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <assert.h>
|
2014-11-26 01:49:25 +03:00
|
|
|
|
|
|
|
// Common implementation
|
|
|
|
|
|
|
|
cmFileLock::~cmFileLock()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Filename.empty()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
const cmFileLockResult result = this->Release();
|
|
|
|
static_cast<void>(result);
|
|
|
|
assert(result.IsOk());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileLockResult cmFileLock::Lock(const std::string& filename,
|
|
|
|
unsigned long timeout)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (filename.empty()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
// Error is internal since all the directories and file must be created
|
|
|
|
// before actual lock called.
|
|
|
|
return cmFileLockResult::MakeInternal();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Filename.empty()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
// Error is internal since double-lock must be checked in class
|
|
|
|
// cmFileLockPool by the cmFileLock::IsLocked method.
|
|
|
|
return cmFileLockResult::MakeInternal();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
|
|
|
this->Filename = filename;
|
|
|
|
cmFileLockResult result = this->OpenFile();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (result.IsOk()) {
|
|
|
|
if (timeout == static_cast<unsigned long>(-1)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
result = this->LockWithoutTimeout();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-11-26 01:49:25 +03:00
|
|
|
result = this->LockWithTimeout(timeout);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!result.IsOk()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
this->Filename = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFileLock::IsLocked(const std::string& filename) const
|
|
|
|
{
|
|
|
|
return filename == this->Filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include "cmFileLockWin32.cxx"
|
2014-11-26 01:49:25 +03:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#include "cmFileLockUnix.cxx"
|
2014-11-26 01:49:25 +03:00
|
|
|
#endif
|