/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/LockFileBase.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
14 | 0 | static Status AlreadyLocked() { return Status("Already locked"); } |
15 | | |
16 | 0 | static Status NotLocked() { return Status("Not locked"); } |
17 | | |
18 | | LockFileBase::LockFileBase(int fd) |
19 | 18 | : m_fd(fd), m_locked(false), m_start(0), m_len(0) {} |
20 | | |
21 | 36 | bool LockFileBase::IsLocked() const { return m_locked; } |
22 | | |
23 | 18 | Status LockFileBase::WriteLock(const uint64_t start, const uint64_t len) { |
24 | 18 | return DoLock([&](const uint64_t start, |
25 | 18 | const uint64_t len) { return DoWriteLock(start, len); }, |
26 | 18 | start, len); |
27 | 18 | } |
28 | | |
29 | 0 | Status LockFileBase::TryWriteLock(const uint64_t start, const uint64_t len) { |
30 | 0 | return DoLock([&](const uint64_t start, |
31 | 0 | const uint64_t len) { return DoTryWriteLock(start, len); }, |
32 | 0 | start, len); |
33 | 0 | } |
34 | | |
35 | 0 | Status LockFileBase::ReadLock(const uint64_t start, const uint64_t len) { |
36 | 0 | return DoLock([&](const uint64_t start, |
37 | 0 | const uint64_t len) { return DoReadLock(start, len); }, |
38 | 0 | start, len); |
39 | 0 | } |
40 | | |
41 | 0 | Status LockFileBase::TryReadLock(const uint64_t start, const uint64_t len) { |
42 | 0 | return DoLock([&](const uint64_t start, |
43 | 0 | const uint64_t len) { return DoTryReadLock(start, len); }, |
44 | 0 | start, len); |
45 | 0 | } |
46 | | |
47 | 18 | Status LockFileBase::Unlock() { |
48 | 18 | if (!IsLocked()) |
49 | 0 | return NotLocked(); |
50 | | |
51 | 18 | const auto error = DoUnlock(); |
52 | 18 | if (error.Success()) { |
53 | 18 | m_locked = false; |
54 | 18 | m_start = 0; |
55 | 18 | m_len = 0; |
56 | 18 | } |
57 | 18 | return error; |
58 | 18 | } |
59 | | |
60 | 18 | bool LockFileBase::IsValidFile() const { return m_fd != -1; } |
61 | | |
62 | | Status LockFileBase::DoLock(const Locker &locker, const uint64_t start, |
63 | 18 | const uint64_t len) { |
64 | 18 | if (!IsValidFile()) |
65 | 0 | return Status("File is invalid"); |
66 | | |
67 | 18 | if (IsLocked()) |
68 | 0 | return AlreadyLocked(); |
69 | | |
70 | 18 | const auto error = locker(start, len); |
71 | 18 | if (error.Success()) { |
72 | 18 | m_locked = true; |
73 | 18 | m_start = start; |
74 | 18 | m_len = len; |
75 | 18 | } |
76 | | |
77 | 18 | return error; |
78 | 18 | } |