LLDB  mainline
LockFileWindows.cpp
Go to the documentation of this file.
1 //===-- LockFileWindows.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 <io.h>
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 
16 static Status fileLock(HANDLE file_handle, DWORD flags, const uint64_t start,
17  const uint64_t len) {
18  if (start != 0)
19  return Status("Non-zero start lock regions are not supported");
20 
21  OVERLAPPED overlapped = {};
22 
23  if (!::LockFileEx(file_handle, flags, 0, len, 0, &overlapped) &&
24  ::GetLastError() != ERROR_IO_PENDING)
25  return Status(::GetLastError(), eErrorTypeWin32);
26 
27  DWORD bytes;
28  if (!::GetOverlappedResult(file_handle, &overlapped, &bytes, TRUE))
29  return Status(::GetLastError(), eErrorTypeWin32);
30 
31  return Status();
32 }
33 
34 LockFileWindows::LockFileWindows(int fd)
35  : LockFileBase(fd), m_file(reinterpret_cast<HANDLE>(_get_osfhandle(fd))) {}
36 
38 
40  return LockFileBase::IsValidFile() && m_file != INVALID_HANDLE_VALUE;
41 }
42 
43 Status LockFileWindows::DoWriteLock(const uint64_t start, const uint64_t len) {
44  return fileLock(m_file, LOCKFILE_EXCLUSIVE_LOCK, start, len);
45 }
46 
48  const uint64_t len) {
49  return fileLock(m_file, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
50  start, len);
51 }
52 
53 Status LockFileWindows::DoReadLock(const uint64_t start, const uint64_t len) {
54  return fileLock(m_file, 0, start, len);
55 }
56 
58  const uint64_t len) {
59  return fileLock(m_file, LOCKFILE_FAIL_IMMEDIATELY, start, len);
60 }
61 
63  OVERLAPPED overlapped = {};
64 
65  if (!::UnlockFileEx(m_file, 0, m_len, 0, &overlapped) &&
66  ::GetLastError() != ERROR_IO_PENDING)
67  return Status(::GetLastError(), eErrorTypeWin32);
68 
69  DWORD bytes;
70  if (!::GetOverlappedResult(m_file, &overlapped, &bytes, TRUE))
71  return Status(::GetLastError(), eErrorTypeWin32);
72 
73  return Status();
74 }
lldb_private::LockFileWindows::DoReadLock
Status DoReadLock(const uint64_t start, const uint64_t len) override
Definition: LockFileWindows.cpp:53
lldb_private::LockFileBase::Unlock
Status Unlock()
Definition: LockFileBase.cpp:47
LockFileWindows.h
lldb_private::LockFileWindows::DoUnlock
Status DoUnlock() override
Definition: LockFileWindows.cpp:62
lldb::eErrorTypeWin32
@ eErrorTypeWin32
Standard Win32 error codes.
Definition: lldb-enumerations.h:312
lldb_private::LockFileWindows::m_file
HANDLE m_file
Definition: LockFileWindows.h:36
lldb_private::LockFileWindows::DoTryWriteLock
Status DoTryWriteLock(const uint64_t start, const uint64_t len) override
Definition: LockFileWindows.cpp:47
lldb_private::LockFileWindows::DoWriteLock
Status DoWriteLock(const uint64_t start, const uint64_t len) override
Definition: LockFileWindows.cpp:43
lldb_private::LockFileBase::IsValidFile
virtual bool IsValidFile() const
Definition: LockFileBase.cpp:60
fileLock
static Status fileLock(HANDLE file_handle, DWORD flags, const uint64_t start, const uint64_t len)
Definition: LockFileWindows.cpp:16
lldb_private::LockFileBase::m_len
uint64_t m_len
Definition: LockFileBase.h:52
lldb_private::LockFileBase
Definition: LockFileBase.h:18
lldb_private::LockFileWindows::DoTryReadLock
Status DoTryReadLock(const uint64_t start, const uint64_t len) override
Definition: LockFileWindows.cpp:57
lldb_private::Status
Definition: Status.h:44
lldb_private::LockFileWindows::~LockFileWindows
~LockFileWindows()
Definition: LockFileWindows.cpp:37
lldb_private::LockFileWindows::IsValidFile
bool IsValidFile() const override
Definition: LockFileWindows.cpp:39
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb
Definition: SBAddress.h:15