LLDB mainline
LockFileBase.cpp
Go to the documentation of this file.
1//===-- LockFileBase.cpp --------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10
11using namespace lldb;
12using namespace lldb_private;
13
15 return Status::FromErrorString("Already locked");
16}
17
18static Status NotLocked() { return Status::FromErrorString("Not locked"); }
19
21 : m_fd(fd), m_locked(false), m_start(0), m_len(0) {}
22
23bool LockFileBase::IsLocked() const { return m_locked; }
24
25Status 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); },
28 start, len);
29}
30
31Status 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); },
34 start, len);
35}
36
37Status 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); },
40 start, len);
41}
42
43Status 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); },
46 start, len);
47}
48
50 if (!IsLocked())
51 return NotLocked();
52
54 if (error.Success()) {
55 m_locked = false;
56 m_start = 0;
57 m_len = 0;
58 }
59 return error;
60}
61
62bool LockFileBase::IsValidFile() const { return m_fd != -1; }
63
64Status LockFileBase::DoLock(const Locker &locker, const uint64_t start,
65 const uint64_t len) {
66 if (!IsValidFile())
67 return Status::FromErrorString("File is invalid");
68
69 if (IsLocked())
70 return AlreadyLocked();
71
72 Status error = locker(start, len);
73 if (error.Success()) {
74 m_locked = true;
75 m_start = start;
76 m_len = len;
77 }
78
79 return error;
80}
static llvm::raw_ostream & error(Stream &strm)
static Status AlreadyLocked()
static Status NotLocked()
std::function< Status(const uint64_t, const uint64_t)> Locker
Definition: LockFileBase.h:33
virtual bool IsValidFile() const
Status TryWriteLock(const uint64_t start, const uint64_t len)
virtual Status DoTryReadLock(const uint64_t start, const uint64_t len)=0
Status WriteLock(const uint64_t start, const uint64_t len)
Status TryReadLock(const uint64_t start, const uint64_t len)
virtual Status DoTryWriteLock(const uint64_t start, const uint64_t len)=0
Status ReadLock(const uint64_t start, const uint64_t len)
Status DoLock(const Locker &locker, const uint64_t start, const uint64_t len)
virtual Status DoUnlock()=0
virtual Status DoWriteLock(const uint64_t start, const uint64_t len)=0
virtual Status DoReadLock(const uint64_t start, const uint64_t len)=0
An error handling class.
Definition: Status.h:115
static Status FromErrorString(const char *str)
Definition: Status.h:138
A class that represents a running process on the host machine.
Definition: SBAddress.h:15