cmake/Source/cmFileLock.cxx

89 lines
2.0 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 "cmFileLock.h"
2020-02-01 23:06:01 +01:00
#include <cassert>
2020-08-30 11:54:41 +02:00
#include <utility>
2020-02-01 23:06:01 +01:00
2015-04-27 22:25:09 +02:00
#include "cmFileLockResult.h"
// Common implementation
2020-08-30 11:54:41 +02:00
cmFileLock::cmFileLock(cmFileLock&& other) noexcept
{
this->File = other.File;
#if defined(_WIN32)
other.File = INVALID_HANDLE_VALUE;
#else
other.File = -1;
#endif
this->Filename = std::move(other.Filename);
}
2015-04-27 22:25:09 +02:00
cmFileLock::~cmFileLock()
{
2016-07-09 11:21:54 +02:00
if (!this->Filename.empty()) {
2015-04-27 22:25:09 +02:00
const cmFileLockResult result = this->Release();
static_cast<void>(result);
assert(result.IsOk());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
cmFileLock& cmFileLock::operator=(cmFileLock&& other) noexcept
{
this->File = other.File;
#if defined(_WIN32)
other.File = INVALID_HANDLE_VALUE;
#else
other.File = -1;
#endif
this->Filename = std::move(other.Filename);
return *this;
}
2016-07-09 11:21:54 +02:00
cmFileLockResult cmFileLock::Lock(const std::string& filename,
unsigned long timeout)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (filename.empty()) {
2015-04-27 22:25:09 +02:00
// Error is internal since all the directories and file must be created
// before actual lock called.
return cmFileLockResult::MakeInternal();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!this->Filename.empty()) {
2015-04-27 22:25:09 +02:00
// Error is internal since double-lock must be checked in class
// cmFileLockPool by the cmFileLock::IsLocked method.
return cmFileLockResult::MakeInternal();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->Filename = filename;
cmFileLockResult result = this->OpenFile();
2016-07-09 11:21:54 +02:00
if (result.IsOk()) {
if (timeout == static_cast<unsigned long>(-1)) {
2015-04-27 22:25:09 +02:00
result = this->LockWithoutTimeout();
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
result = this->LockWithTimeout(timeout);
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!result.IsOk()) {
2018-01-26 17:06:56 +01:00
this->Filename.clear();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return result;
}
bool cmFileLock::IsLocked(const std::string& filename) const
{
return filename == this->Filename;
}
#if defined(_WIN32)
2021-09-14 00:13:48 +02:00
// NOLINTNEXTLINE(bugprone-suspicious-include)
2018-08-09 18:06:22 +02:00
# include "cmFileLockWin32.cxx"
2015-04-27 22:25:09 +02:00
#else
2021-09-14 00:13:48 +02:00
// NOLINTNEXTLINE(bugprone-suspicious-include)
2018-08-09 18:06:22 +02:00
# include "cmFileLockUnix.cxx"
2015-04-27 22:25:09 +02:00
#endif