Skip to content

Commit

Permalink
FileSystem: Make POSIXLock moveable
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Dec 3, 2024
1 parent 5b6e3a9 commit d93c713
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/common/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cstring>
#include <limits>
#include <numeric>
#include <utility>

#ifdef __APPLE__
#include <mach-o/dyld.h>
Expand Down Expand Up @@ -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))
Expand All @@ -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
6 changes: 6 additions & 0 deletions src/common/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit d93c713

Please sign in to comment.