xref: /freebsd/contrib/llvm-project/lldb/source/Host/common/LockFileBase.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #include "lldb/Host/LockFileBase.h"
10 
11 using namespace lldb;
12 using namespace lldb_private;
13 
AlreadyLocked()14 static Status AlreadyLocked() {
15   return Status::FromErrorString("Already locked");
16 }
17 
NotLocked()18 static Status NotLocked() { return Status::FromErrorString("Not locked"); }
19 
LockFileBase(int fd)20 LockFileBase::LockFileBase(int fd)
21     : m_fd(fd), m_locked(false), m_start(0), m_len(0) {}
22 
IsLocked() const23 bool LockFileBase::IsLocked() const { return m_locked; }
24 
WriteLock(const uint64_t start,const uint64_t len)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); },
28                 start, len);
29 }
30 
TryWriteLock(const uint64_t start,const uint64_t 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); },
34                 start, len);
35 }
36 
ReadLock(const uint64_t start,const uint64_t 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); },
40                 start, len);
41 }
42 
TryReadLock(const uint64_t start,const uint64_t 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); },
46                 start, len);
47 }
48 
Unlock()49 Status LockFileBase::Unlock() {
50   if (!IsLocked())
51     return NotLocked();
52 
53   Status error = DoUnlock();
54   if (error.Success()) {
55     m_locked = false;
56     m_start = 0;
57     m_len = 0;
58   }
59   return error;
60 }
61 
IsValidFile() const62 bool LockFileBase::IsValidFile() const { return m_fd != -1; }
63 
DoLock(const Locker & locker,const uint64_t start,const uint64_t len)64 Status 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 }
81