blob: 0d9b296453b0aeb7e6e70aeb4844137763d5b68a (
plain) (
tree)
|
|
#include "file_lock.h"
#include "flock.h"
#include <util/generic/yexception.h>
#include <cerrno>
namespace {
int GetMode(const EFileLockType type) {
switch (type) {
case EFileLockType::Exclusive:
return LOCK_EX;
case EFileLockType::Shared:
return LOCK_SH;
default:
Y_UNREACHABLE();
}
Y_UNREACHABLE();
}
} // namespace
TFileLock::TFileLock(const TString& filename, const EFileLockType type)
: TFile(filename, OpenAlways | RdOnly)
, Type(type)
{
}
void TFileLock::Acquire() {
Flock(GetMode(Type));
}
bool TFileLock::TryAcquire() {
try {
Flock(GetMode(Type) | LOCK_NB);
return true;
} catch (const TSystemError& e) {
if (e.Status() != EWOULDBLOCK) {
throw;
}
return false;
}
}
void TFileLock::Release() {
Flock(LOCK_UN);
}
|