blob: 45d91282c5b417ea00131da44edd7fa6befa5f82 (
plain) (
blame)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 | #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();
    }
}
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);
}
 |