1 //===-- LockFilePosix.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/posix/LockFilePosix.h" 11 12 #include <fcntl.h> 13 14 using namespace lldb; 15 using namespace lldb_private; 16 17 namespace 18 { 19 20 Error fileLock (int fd, int cmd, int lock_type, const uint64_t start, const uint64_t len) 21 { 22 struct flock fl; 23 24 fl.l_type = lock_type; 25 fl.l_whence = SEEK_SET; 26 fl.l_start = start; 27 fl.l_len = len; 28 fl.l_pid = ::getpid (); 29 30 Error error; 31 if (::fcntl (fd, cmd, &fl) == -1) 32 error.SetErrorToErrno (); 33 34 return error; 35 } 36 37 } // namespace 38 39 LockFilePosix::LockFilePosix (int fd) 40 : LockFileBase (fd) 41 { 42 } 43 44 LockFilePosix::~LockFilePosix () 45 { 46 Unlock (); 47 } 48 49 Error 50 LockFilePosix::DoWriteLock (const uint64_t start, const uint64_t len) 51 { 52 return fileLock (m_fd, F_SETLKW, F_WRLCK, start, len); 53 } 54 55 Error 56 LockFilePosix::DoTryWriteLock (const uint64_t start, const uint64_t len) 57 { 58 return fileLock (m_fd, F_SETLK, F_WRLCK, start, len); 59 } 60 61 Error 62 LockFilePosix::DoReadLock (const uint64_t start, const uint64_t len) 63 { 64 return fileLock (m_fd, F_SETLKW, F_RDLCK, start, len); 65 } 66 67 Error 68 LockFilePosix::DoTryReadLock (const uint64_t start, const uint64_t len) 69 { 70 return fileLock (m_fd, F_SETLK, F_RDLCK, start, len); 71 } 72 73 Error 74 LockFilePosix::DoUnlock () 75 { 76 return fileLock (m_fd, F_SETLK, F_UNLCK, m_start, m_len); 77 } 78