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
13using namespace lldb;
14using namespace lldb_private;
15
16static 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
35 : LockFileBase(fd), m_file(reinterpret_cast<HANDLE>(_get_osfhandle(fd))) {}
36
38
40 return LockFileBase::IsValidFile() && m_file != INVALID_HANDLE_VALUE;
41}
42
43Status 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
53Status 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}
static Status fileLock(int fd, int cmd, int lock_type, const uint64_t start, const uint64_t len)
static Status fileLock(HANDLE file_handle, DWORD flags, const uint64_t start, const uint64_t len)
virtual bool IsValidFile() const
Status DoTryReadLock(const uint64_t start, const uint64_t len) override
bool IsValidFile() const override
Status DoWriteLock(const uint64_t start, const uint64_t len) override
Status DoTryWriteLock(const uint64_t start, const uint64_t len) override
Status DoReadLock(const uint64_t start, const uint64_t len) override
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
@ eErrorTypeWin32
Standard Win32 error codes.