2023-06-20 04:33:09 +00:00
|
|
|
#include "StarLockFile.hpp"
|
|
|
|
#include "StarTime.hpp"
|
|
|
|
#include "StarThread.hpp"
|
|
|
|
|
|
|
|
#include "StarString_windows.hpp"
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
namespace Star {
|
|
|
|
|
|
|
|
int64_t const LockFile::MaximumSleepMillis;
|
|
|
|
|
|
|
|
Maybe<LockFile> LockFile::acquireLock(String const& filename, int64_t lockTimeout) {
|
2024-02-19 15:55:19 +00:00
|
|
|
LockFile lock(std::move(filename));
|
2023-06-20 04:33:09 +00:00
|
|
|
if (lock.lock(lockTimeout))
|
2024-02-19 15:55:19 +00:00
|
|
|
return std::move(lock);
|
2023-06-20 04:33:09 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-19 15:55:19 +00:00
|
|
|
LockFile::LockFile(String const& filename) : m_filename(std::move(filename)) {}
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
LockFile::LockFile(LockFile&& lockFile) {
|
2024-02-19 15:55:19 +00:00
|
|
|
operator=(std::move(lockFile));
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LockFile::~LockFile() {
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
LockFile& LockFile::operator=(LockFile&& lockFile) {
|
2024-02-19 15:55:19 +00:00
|
|
|
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) -> shared_ptr<HANDLE> {
|
|
|
|
HANDLE handle = CreateFileW(
|
|
|
|
stringToUtf16(filename).get(), GENERIC_READ, 0, nullptr, OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, nullptr);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
if (GetLastError() == ERROR_SHARING_VIOLATION)
|
|
|
|
return {};
|
2023-06-27 10:23:44 +00:00
|
|
|
throw StarException(strf("Could not open lock file {}, error code {}\n", filename, GetLastError()));
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return make_shared<HANDLE>(handle);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (timeout == 0) {
|
|
|
|
m_handle = doFLock(m_filename);
|
|
|
|
return (bool)m_handle;
|
|
|
|
} else {
|
|
|
|
int64_t startTime = Time::monotonicMilliseconds();
|
|
|
|
while (true) {
|
|
|
|
m_handle = doFLock(m_filename);
|
|
|
|
if (m_handle)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (timeout > 0 && Time::monotonicMilliseconds() - startTime > timeout)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Thread::sleep(min(timeout / 4, MaximumSleepMillis));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockFile::unlock() {
|
|
|
|
if (m_handle) {
|
|
|
|
HANDLE handle = *(HANDLE*)m_handle.get();
|
|
|
|
CloseHandle(handle);
|
|
|
|
m_handle.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LockFile::isLocked() const {
|
|
|
|
return (bool)m_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|