FileSystem: Make POSIXLock moveable

This commit is contained in:
Stenzek 2024-12-03 17:03:52 +10:00
parent 5b6e3a952c
commit d93c713fb7
No known key found for this signature in database
2 changed files with 22 additions and 0 deletions

View File

@ -16,6 +16,7 @@
#include <cstring>
#include <limits>
#include <numeric>
#include <utility>
#ifdef __APPLE__
#include <mach-o/dyld.h>
@ -2823,6 +2824,10 @@ static bool SetLock(int fd, bool lock)
return res;
}
FileSystem::POSIXLock::POSIXLock() : m_fd(-1)
{
}
FileSystem::POSIXLock::POSIXLock(int fd) : m_fd(fd)
{
if (!SetLock(m_fd, true))
@ -2835,10 +2840,21 @@ FileSystem::POSIXLock::POSIXLock(std::FILE* fp) : m_fd(fileno(fp))
m_fd = -1;
}
FileSystem::POSIXLock::POSIXLock(POSIXLock&& move)
{
m_fd = std::exchange(move.m_fd, -1);
}
FileSystem::POSIXLock::~POSIXLock()
{
if (m_fd >= 0)
SetLock(m_fd, false);
}
FileSystem::POSIXLock& FileSystem::POSIXLock::operator=(POSIXLock&& move)
{
m_fd = std::exchange(move.m_fd, -1);
return *this;
}
#endif

View File

@ -159,10 +159,16 @@ void DiscardAtomicRenamedFile(AtomicRenamedFile& file);
class POSIXLock
{
public:
POSIXLock();
POSIXLock(int fd);
POSIXLock(std::FILE* fp);
POSIXLock(POSIXLock&& move);
POSIXLock(const POSIXLock&) = delete;
~POSIXLock();
POSIXLock& operator=(POSIXLock&& move);
POSIXLock& operator=(const POSIXLock&) = delete;
private:
int m_fd;
};