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 
11 using namespace lldb;
12 using namespace lldb_private;
13 
14 static Status AlreadyLocked() { return Status("Already locked"); }
15 
16 static Status NotLocked() { return Status("Not locked"); }
17 
18 LockFileBase::LockFileBase(int fd)
19  : m_fd(fd), m_locked(false), m_start(0), m_len(0) {}
20 
21 bool LockFileBase::IsLocked() const { return m_locked; }
22 
23 Status LockFileBase::WriteLock(const uint64_t start, const uint64_t len) {
24  return DoLock([&](const uint64_t start,
25  const uint64_t len) { return DoWriteLock(start, len); },
26  start, len);
27 }
28 
29 Status LockFileBase::TryWriteLock(const uint64_t start, const uint64_t len) {
30  return DoLock([&](const uint64_t start,
31  const uint64_t len) { return DoTryWriteLock(start, len); },
32  start, len);
33 }
34 
35 Status LockFileBase::ReadLock(const uint64_t start, const uint64_t len) {
36  return DoLock([&](const uint64_t start,
37  const uint64_t len) { return DoReadLock(start, len); },
38  start, len);
39 }
40 
41 Status LockFileBase::TryReadLock(const uint64_t start, const uint64_t len) {
42  return DoLock([&](const uint64_t start,
43  const uint64_t len) { return DoTryReadLock(start, len); },
44  start, len);
45 }
46 
48  if (!IsLocked())
49  return NotLocked();
50 
51  const auto error = DoUnlock();
52  if (error.Success()) {
53  m_locked = false;
54  m_start = 0;
55  m_len = 0;
56  }
57  return error;
58 }
59 
60 bool LockFileBase::IsValidFile() const { return m_fd != -1; }
61 
62 Status LockFileBase::DoLock(const Locker &locker, const uint64_t start,
63  const uint64_t len) {
64  if (!IsValidFile())
65  return Status("File is invalid");
66 
67  if (IsLocked())
68  return AlreadyLocked();
69 
70  const auto error = locker(start, len);
71  if (error.Success()) {
72  m_locked = true;
73  m_start = start;
74  m_len = len;
75  }
76 
77  return error;
78 }
lldb_private::LockFileBase::IsLocked
bool IsLocked() const
Definition: LockFileBase.cpp:21
lldb_private::LockFileBase::DoReadLock
virtual Status DoReadLock(const uint64_t start, const uint64_t len)=0
lldb_private::LockFileBase::m_locked
bool m_locked
Definition: LockFileBase.h:50
lldb_private::LockFileBase::m_start
uint64_t m_start
Definition: LockFileBase.h:51
lldb_private::LockFileBase::Unlock
Status Unlock()
Definition: LockFileBase.cpp:47
NotLocked
static Status NotLocked()
Definition: LockFileBase.cpp:16
lldb_private::LockFileBase::DoTryWriteLock
virtual Status DoTryWriteLock(const uint64_t start, const uint64_t len)=0
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::LockFileBase::DoLock
Status DoLock(const Locker &locker, const uint64_t start, const uint64_t len)
Definition: LockFileBase.cpp:62
lldb_private::LockFileBase::IsValidFile
virtual bool IsValidFile() const
Definition: LockFileBase.cpp:60
lldb_private::LockFileBase::DoTryReadLock
virtual Status DoTryReadLock(const uint64_t start, const uint64_t len)=0
lldb_private::LockFileBase::m_len
uint64_t m_len
Definition: LockFileBase.h:52
AlreadyLocked
static Status AlreadyLocked()
Definition: LockFileBase.cpp:14
LockFileBase.h
lldb_private::LockFileBase::ReadLock
Status ReadLock(const uint64_t start, const uint64_t len)
Definition: LockFileBase.cpp:35
lldb_private::LockFileBase::m_fd
int m_fd
Definition: LockFileBase.h:49
lldb_private::Status
Definition: Status.h:44
lldb_private::LockFileBase::Locker
std::function< Status(const uint64_t, const uint64_t)> Locker
Definition: LockFileBase.h:33
lldb_private::LockFileBase::WriteLock
Status WriteLock(const uint64_t start, const uint64_t len)
Definition: LockFileBase.cpp:23
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::LockFileBase::TryWriteLock
Status TryWriteLock(const uint64_t start, const uint64_t len)
Definition: LockFileBase.cpp:29
lldb_private::LockFileBase::DoUnlock
virtual Status DoUnlock()=0
lldb_private::LockFileBase::TryReadLock
Status TryReadLock(const uint64_t start, const uint64_t len)
Definition: LockFileBase.cpp:41
lldb
Definition: SBAddress.h:15
lldb_private::LockFileBase::DoWriteLock
virtual Status DoWriteLock(const uint64_t start, const uint64_t len)=0