osb/source/core/StarLockFile_unix.cpp

95 lines
2.1 KiB
C++
Raw Permalink Normal View History

2023-06-20 04:33:09 +00:00
#include "StarLockFile.hpp"
#include "StarTime.hpp"
#include "StarThread.hpp"
#include <sys/file.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
namespace Star {
int64_t const LockFile::MaximumSleepMillis;
Maybe<LockFile> LockFile::acquireLock(String const& filename, int64_t lockTimeout) {
LockFile lock(std::move(filename));
2023-06-20 04:33:09 +00:00
if (lock.lock(lockTimeout))
return lock;
2023-06-20 04:33:09 +00:00
return {};
}
LockFile::LockFile(String const& filename) : m_filename(std::move(filename)) {}
2023-06-20 04:33:09 +00:00
LockFile::LockFile(LockFile&& lockFile) {
operator=(std::move(lockFile));
2023-06-20 04:33:09 +00:00
}
LockFile::~LockFile() {
unlock();
}
LockFile& LockFile::operator=(LockFile&& lockFile) {
m_filename = std::move(lockFile.m_filename);
m_handle = std::move(lockFile.m_handle);
2023-06-20 04:33:09 +00:00
return *this;
}
bool LockFile::lock(int64_t timeout) {
auto doFLock = [](String const& filename, bool block) -> shared_ptr<int> {
int fd = open(filename.utf8Ptr(), O_RDONLY | O_CREAT, 0644);
if (fd < 0)
2023-06-27 10:23:44 +00:00
throw StarException(strf("Could not open lock file {}, {}\n", filename, strerror(errno)));
2023-06-20 04:33:09 +00:00
int ret;
if (block)
ret = flock(fd, LOCK_EX);
else
ret = flock(fd, LOCK_EX | LOCK_NB);
if (ret != 0) {
close(fd);
if (errno != EWOULDBLOCK)
2023-06-27 10:23:44 +00:00
throw StarException(strf("Could not lock file {}, {}\n", filename, strerror(errno)));
2023-06-20 04:33:09 +00:00
return {};
}
return make_shared<int>(fd);
};
if (timeout < 0) {
m_handle = doFLock(m_filename, true);
return true;
} else if (timeout == 0) {
m_handle = doFLock(m_filename, false);
return (bool)m_handle;
} else {
int64_t startTime = Time::monotonicMilliseconds();
while (true) {
m_handle = doFLock(m_filename, false);
if (m_handle)
return true;
if (Time::monotonicMilliseconds() - startTime > timeout)
return false;
Thread::sleep(min(timeout / 4, MaximumSleepMillis));
}
}
}
void LockFile::unlock() {
if (m_handle) {
int fd = *(int*)m_handle.get();
unlink(m_filename.utf8Ptr());
close(fd);
m_handle.reset();
}
}
bool LockFile::isLocked() const {
return (bool)m_handle;
}
}