]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Host/posix/LockFilePosix.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Host / posix / LockFilePosix.cpp
1 //===-- LockFilePosix.cpp ---------------------------------------*- C++ -*-===//
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/posix/LockFilePosix.h"
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 namespace {
20
21 Status fileLock(int fd, int cmd, int lock_type, const uint64_t start,
22                 const uint64_t len) {
23   struct flock fl;
24
25   fl.l_type = lock_type;
26   fl.l_whence = SEEK_SET;
27   fl.l_start = start;
28   fl.l_len = len;
29   fl.l_pid = ::getpid();
30
31   Status error;
32   if (llvm::sys::RetryAfterSignal(-1, ::fcntl, fd, cmd, &fl) == -1)
33     error.SetErrorToErrno();
34
35   return error;
36 }
37
38 } // namespace
39
40 LockFilePosix::LockFilePosix(int fd) : LockFileBase(fd) {}
41
42 LockFilePosix::~LockFilePosix() { Unlock(); }
43
44 Status LockFilePosix::DoWriteLock(const uint64_t start, const uint64_t len) {
45   return fileLock(m_fd, F_SETLKW, F_WRLCK, start, len);
46 }
47
48 Status LockFilePosix::DoTryWriteLock(const uint64_t start, const uint64_t len) {
49   return fileLock(m_fd, F_SETLK, F_WRLCK, start, len);
50 }
51
52 Status LockFilePosix::DoReadLock(const uint64_t start, const uint64_t len) {
53   return fileLock(m_fd, F_SETLKW, F_RDLCK, start, len);
54 }
55
56 Status LockFilePosix::DoTryReadLock(const uint64_t start, const uint64_t len) {
57   return fileLock(m_fd, F_SETLK, F_RDLCK, start, len);
58 }
59
60 Status LockFilePosix::DoUnlock() {
61   return fileLock(m_fd, F_SETLK, F_UNLCK, m_start, m_len);
62 }