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
9#include "lldb/Host/posix/LockFilePosix.h"
10
11#include "llvm/Support/Errno.h"
12
13#include <fcntl.h>
14#include <unistd.h>
15
16using namespace lldb;
17using namespace lldb_private;
18
19static 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 if (llvm::sys::RetryAfterSignal(Fail: -1, F&: ::fcntl, As: fd, As: cmd, As: &fl) == -1)
30 return Status::FromErrno();
31
32 return Status();
33}
34
35LockFilePosix::LockFilePosix(int fd) : LockFileBase(fd) {}
36
37LockFilePosix::~LockFilePosix() { Unlock(); }
38
39Status LockFilePosix::DoWriteLock(const uint64_t start, const uint64_t len) {
40 return fileLock(fd: m_fd, F_SETLKW, F_WRLCK, start, len);
41}
42
43Status LockFilePosix::DoTryWriteLock(const uint64_t start, const uint64_t len) {
44 return fileLock(fd: m_fd, F_SETLK, F_WRLCK, start, len);
45}
46
47Status LockFilePosix::DoReadLock(const uint64_t start, const uint64_t len) {
48 return fileLock(fd: m_fd, F_SETLKW, F_RDLCK, start, len);
49}
50
51Status LockFilePosix::DoTryReadLock(const uint64_t start, const uint64_t len) {
52 return fileLock(fd: m_fd, F_SETLK, F_RDLCK, start, len);
53}
54
55Status LockFilePosix::DoUnlock() {
56 return fileLock(fd: m_fd, F_SETLK, F_UNLCK, start: m_start, len: m_len);
57}
58

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of lldb/source/Host/posix/LockFilePosix.cpp