[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / lldb / source / Host / common / LockFileBase.cpp
blob6ef684e6d622c149f276aa5bcd472d9ffab97b8c
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() {
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); },
28 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); },
34 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); },
40 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); },
46 start, len);
49 Status LockFileBase::Unlock() {
50 if (!IsLocked())
51 return NotLocked();
53 Status error = DoUnlock();
54 if (error.Success()) {
55 m_locked = false;
56 m_start = 0;
57 m_len = 0;
59 return error;
62 bool LockFileBase::IsValidFile() const { return m_fd != -1; }
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");
69 if (IsLocked())
70 return AlreadyLocked();
72 Status error = locker(start, len);
73 if (error.Success()) {
74 m_locked = true;
75 m_start = start;
76 m_len = len;
79 return error;