1 //===-- LockFilePosix.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/posix/LockFilePosix.h"
11 #include "llvm/Support/Errno.h"
17 using namespace lldb_private
;
19 static Status
fileLock(int fd
, int cmd
, int lock_type
, const uint64_t start
,
23 fl
.l_type
= lock_type
;
24 fl
.l_whence
= SEEK_SET
;
27 fl
.l_pid
= ::getpid();
30 if (llvm::sys::RetryAfterSignal(-1, ::fcntl
, fd
, cmd
, &fl
) == -1)
31 error
.SetErrorToErrno();
36 LockFilePosix::LockFilePosix(int fd
) : LockFileBase(fd
) {}
38 LockFilePosix::~LockFilePosix() { Unlock(); }
40 Status
LockFilePosix::DoWriteLock(const uint64_t start
, const uint64_t len
) {
41 return fileLock(m_fd
, F_SETLKW
, F_WRLCK
, start
, len
);
44 Status
LockFilePosix::DoTryWriteLock(const uint64_t start
, const uint64_t len
) {
45 return fileLock(m_fd
, F_SETLK
, F_WRLCK
, start
, len
);
48 Status
LockFilePosix::DoReadLock(const uint64_t start
, const uint64_t len
) {
49 return fileLock(m_fd
, F_SETLKW
, F_RDLCK
, start
, len
);
52 Status
LockFilePosix::DoTryReadLock(const uint64_t start
, const uint64_t len
) {
53 return fileLock(m_fd
, F_SETLK
, F_RDLCK
, start
, len
);
56 Status
LockFilePosix::DoUnlock() {
57 return fileLock(m_fd
, F_SETLK
, F_UNLCK
, m_start
, m_len
);