]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/LockFileBase.cpp
Import mandoc 1.4.1rc2
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / LockFileBase.cpp
1 //===-- LockFileBase.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/LockFileBase.h"
11
12 using namespace lldb;
13 using namespace lldb_private;
14
15 namespace
16 {
17
18 Error
19 AlreadyLocked ()
20 {
21     return Error ("Already locked");
22 }
23
24 Error
25 NotLocked ()
26 {
27     return Error ("Not locked");
28 }
29
30 }
31
32 LockFileBase::LockFileBase (int fd) :
33     m_fd (fd),
34     m_locked (false),
35     m_start (0),
36     m_len (0)
37 {
38
39 }
40
41 bool
42 LockFileBase::IsLocked () const
43 {
44     return m_locked;
45 }
46
47 Error
48 LockFileBase::WriteLock (const uint64_t start, const uint64_t len)
49 {
50     return DoLock ([&] (const uint64_t start, const uint64_t len)
51                    {
52                        return DoWriteLock (start, len);
53                    }, start, len);
54 }
55
56 Error
57 LockFileBase::TryWriteLock (const uint64_t start, const uint64_t len)
58 {
59     return DoLock ([&] (const uint64_t start, const uint64_t len)
60                    {
61                         return DoTryWriteLock (start, len);
62                    }, start, len);
63 }
64
65 Error
66 LockFileBase::ReadLock (const uint64_t start, const uint64_t len)
67 {
68     return DoLock ([&] (const uint64_t start, const uint64_t len)
69                    {
70                         return DoReadLock (start, len);
71                    }, start, len);
72 }
73
74 Error
75 LockFileBase::TryReadLock (const uint64_t start, const uint64_t len)
76 {
77     return DoLock ([&] (const uint64_t start, const uint64_t len)
78                    {
79                        return DoTryReadLock (start, len);
80                    }, start, len);
81
82 }
83
84 Error
85 LockFileBase::Unlock ()
86 {
87     if (!IsLocked ())
88         return NotLocked ();
89
90     const auto error = DoUnlock ();
91     if (error.Success ())
92     {
93         m_locked = false;
94         m_start = 0;
95         m_len = 0;
96     }
97     return error;
98 }
99
100 bool
101 LockFileBase::IsValidFile () const
102 {
103     return m_fd != -1;
104 }
105
106 Error
107 LockFileBase::DoLock (const Locker &locker, const uint64_t start, const uint64_t len)
108 {
109     if (!IsValidFile ())
110         return Error("File is invalid");
111
112     if (IsLocked ())
113         return AlreadyLocked ();
114
115     const auto error = locker (start, len);
116     if (error.Success ())
117     {
118         m_locked = true;
119         m_start = start;
120         m_len = len;
121     }
122
123     return error;
124 }