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