Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Host / common / LockFileBase.cpp
blob1c0de9e04e29ff551326ff9bcaa57ebb46c6a1bd
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 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/LockFileBase.h"
11 using namespace lldb;
12 using namespace lldb_private;
14 static Status AlreadyLocked() { return Status("Already locked"); }
16 static Status NotLocked() { return Status("Not locked"); }
18 LockFileBase::LockFileBase(int fd)
19 : m_fd(fd), m_locked(false), m_start(0), m_len(0) {}
21 bool LockFileBase::IsLocked() const { return m_locked; }
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);
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);
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);
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);
47 Status LockFileBase::Unlock() {
48 if (!IsLocked())
49 return NotLocked();
51 const auto error = DoUnlock();
52 if (error.Success()) {
53 m_locked = false;
54 m_start = 0;
55 m_len = 0;
57 return error;
60 bool LockFileBase::IsValidFile() const { return m_fd != -1; }
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");
67 if (IsLocked())
68 return AlreadyLocked();
70 const auto error = locker(start, len);
71 if (error.Success()) {
72 m_locked = true;
73 m_start = start;
74 m_len = len;
77 return error;