LLDB  mainline
LockFilePosix.cpp
Go to the documentation of this file.
1 //===-- LockFilePosix.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 
10 
11 #include "llvm/Support/Errno.h"
12 
13 #include <fcntl.h>
14 #include <unistd.h>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 static Status fileLock(int fd, int cmd, int lock_type, const uint64_t start,
20  const uint64_t len) {
21  struct flock fl;
22 
23  fl.l_type = lock_type;
24  fl.l_whence = SEEK_SET;
25  fl.l_start = start;
26  fl.l_len = len;
27  fl.l_pid = ::getpid();
28 
29  Status error;
30  if (llvm::sys::RetryAfterSignal(-1, ::fcntl, fd, cmd, &fl) == -1)
31  error.SetErrorToErrno();
32 
33  return error;
34 }
35 
36 LockFilePosix::LockFilePosix(int fd) : LockFileBase(fd) {}
37 
39 
40 Status LockFilePosix::DoWriteLock(const uint64_t start, const uint64_t len) {
41  return fileLock(m_fd, F_SETLKW, F_WRLCK, start, len);
42 }
43 
44 Status LockFilePosix::DoTryWriteLock(const uint64_t start, const uint64_t len) {
45  return fileLock(m_fd, F_SETLK, F_WRLCK, start, len);
46 }
47 
48 Status LockFilePosix::DoReadLock(const uint64_t start, const uint64_t len) {
49  return fileLock(m_fd, F_SETLKW, F_RDLCK, start, len);
50 }
51 
52 Status LockFilePosix::DoTryReadLock(const uint64_t start, const uint64_t len) {
53  return fileLock(m_fd, F_SETLK, F_RDLCK, start, len);
54 }
55 
57  return fileLock(m_fd, F_SETLK, F_UNLCK, m_start, m_len);
58 }
lldb_private::LockFilePosix::DoReadLock
Status DoReadLock(const uint64_t start, const uint64_t len) override
Definition: LockFilePosix.cpp:48
LockFilePosix.h
lldb_private::LockFilePosix::DoTryReadLock
Status DoTryReadLock(const uint64_t start, const uint64_t len) override
Definition: LockFilePosix.cpp:52
lldb_private::LockFilePosix::DoWriteLock
Status DoWriteLock(const uint64_t start, const uint64_t len) override
Definition: LockFilePosix.cpp:40
lldb_private::LockFilePosix::DoUnlock
Status DoUnlock() override
Definition: LockFilePosix.cpp:56
lldb_private::LockFileBase::m_start
uint64_t m_start
Definition: LockFileBase.h:51
lldb_private::LockFileBase::Unlock
Status Unlock()
Definition: LockFileBase.cpp:47
fileLock
static Status fileLock(int fd, int cmd, int lock_type, const uint64_t start, const uint64_t len)
Definition: LockFilePosix.cpp:19
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::LockFilePosix::~LockFilePosix
~LockFilePosix() override
Definition: LockFilePosix.cpp:38
lldb_private::LockFileBase::m_len
uint64_t m_len
Definition: LockFileBase.h:52
lldb_private::LockFileBase
Definition: LockFileBase.h:18
lldb_private::LockFilePosix::DoTryWriteLock
Status DoTryWriteLock(const uint64_t start, const uint64_t len) override
Definition: LockFilePosix.cpp:44
lldb_private::LockFileBase::m_fd
int m_fd
Definition: LockFileBase.h:49
lldb_private::Status
Definition: Status.h:44
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb
Definition: SBAddress.h:15