1 //===-- LockFileBase.cpp --------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/LockFileBase.h"
12 using namespace lldb_private
;
14 static Status
AlreadyLocked() {
15 return Status::FromErrorString("Already locked");
18 static Status
NotLocked() { return Status::FromErrorString("Not locked"); }
20 LockFileBase::LockFileBase(int fd
)
21 : m_fd(fd
), m_locked(false), m_start(0), m_len(0) {}
23 bool LockFileBase::IsLocked() const { return m_locked
; }
25 Status
LockFileBase::WriteLock(const uint64_t start
, const uint64_t len
) {
26 return DoLock([&](const uint64_t start
,
27 const uint64_t len
) { return DoWriteLock(start
, len
); },
31 Status
LockFileBase::TryWriteLock(const uint64_t start
, const uint64_t len
) {
32 return DoLock([&](const uint64_t start
,
33 const uint64_t len
) { return DoTryWriteLock(start
, len
); },
37 Status
LockFileBase::ReadLock(const uint64_t start
, const uint64_t len
) {
38 return DoLock([&](const uint64_t start
,
39 const uint64_t len
) { return DoReadLock(start
, len
); },
43 Status
LockFileBase::TryReadLock(const uint64_t start
, const uint64_t len
) {
44 return DoLock([&](const uint64_t start
,
45 const uint64_t len
) { return DoTryReadLock(start
, len
); },
49 Status
LockFileBase::Unlock() {
53 Status error
= DoUnlock();
54 if (error
.Success()) {
62 bool LockFileBase::IsValidFile() const { return m_fd
!= -1; }
64 Status
LockFileBase::DoLock(const Locker
&locker
, const uint64_t start
,
67 return Status::FromErrorString("File is invalid");
70 return AlreadyLocked();
72 Status error
= locker(start
, len
);
73 if (error
.Success()) {