cmake/Source/cmFileLock.h

66 lines
1.5 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
#ifndef cmFileLock_h
#define cmFileLock_h
2018-01-26 17:06:56 +01:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-10-30 18:24:19 +01:00
#include <string>
2015-04-27 22:25:09 +02:00
#if defined(_WIN32)
2018-08-09 18:06:22 +02:00
# include <windows.h> // HANDLE
2015-04-27 22:25:09 +02:00
#endif
class cmFileLockResult;
/**
2018-08-09 18:06:22 +02:00
* @brief Cross-platform file locking.
* @details Under the hood this class use 'fcntl' for Unix-like platforms and
* 'LockFileEx'/'UnlockFileEx' for Win32 platform. Locks are exclusive and
* advisory.
*/
2015-04-27 22:25:09 +02:00
class cmFileLock
{
2017-07-20 19:35:53 +02:00
CM_DISABLE_COPY(cmFileLock)
2016-07-09 11:21:54 +02:00
public:
2015-04-27 22:25:09 +02:00
cmFileLock();
~cmFileLock();
/**
2018-08-09 18:06:22 +02:00
* @brief Lock the file.
* @param timeoutSec Lock timeout. If -1 try until success or fatal error.
*/
2015-04-27 22:25:09 +02:00
cmFileLockResult Lock(const std::string& filename, unsigned long timeoutSec);
/**
2018-08-09 18:06:22 +02:00
* @brief Unlock the file.
*/
2015-04-27 22:25:09 +02:00
cmFileLockResult Release();
/**
2018-08-09 18:06:22 +02:00
* @brief Check file is locked by this class.
* @details This function helps to find double locks (deadlocks) and to do
* explicit unlocks.
*/
2015-04-27 22:25:09 +02:00
bool IsLocked(const std::string& filename) const;
2016-07-09 11:21:54 +02:00
private:
2015-04-27 22:25:09 +02:00
cmFileLockResult OpenFile();
cmFileLockResult LockWithoutTimeout();
cmFileLockResult LockWithTimeout(unsigned long timeoutSec);
#if defined(_WIN32)
typedef HANDLE FileId;
BOOL LockFile(DWORD flags);
#else
typedef int FileId;
int LockFile(int cmd, int type);
#endif
FileId File;
std::string Filename;
};
#endif // cmFileLock_h