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)
20 "Non-zero start lock regions are not supported");
21
22 OVERLAPPED overlapped = {};
23
24 if (!::LockFileEx(file_handle, flags, 0, len, 0, &overlapped) &&
25 ::GetLastError() != ERROR_IO_PENDING)
26 return Status(::GetLastError(), eErrorTypeWin32);
27
28 DWORD bytes;
29 if (!::GetOverlappedResult(file_handle, &overlapped, &bytes, TRUE))
30 return Status(::GetLastError(), eErrorTypeWin32);
31
32 return Status();
33}
34
36 : LockFileBase(fd), m_file(reinterpret_cast<HANDLE>(_get_osfhandle(fd))) {}
37
39
41 return LockFileBase::IsValidFile() && m_file != INVALID_HANDLE_VALUE;
42}
43
44Status LockFileWindows::DoWriteLock(const uint64_t start, const uint64_t len) {
45 return fileLock(m_file, LOCKFILE_EXCLUSIVE_LOCK, start, len);
46}
47
49 const uint64_t len) {
50 return fileLock(m_file, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
51 start, len);
52}
53
54Status LockFileWindows::DoReadLock(const uint64_t start, const uint64_t len) {
55 return fileLock(m_file, 0, start, len);
56}
57
59 const uint64_t len) {
60 return fileLock(m_file, LOCKFILE_FAIL_IMMEDIATELY, start, len);
61}
62
64 OVERLAPPED overlapped = {};
65
66 if (!::UnlockFileEx(m_file, 0, m_len, 0, &overlapped) &&
67 ::GetLastError() != ERROR_IO_PENDING)
68 return Status(::GetLastError(), eErrorTypeWin32);
69
70 DWORD bytes;
71 if (!::GetOverlappedResult(m_file, &overlapped, &bytes, TRUE))
72 return Status(::GetLastError(), eErrorTypeWin32);
73
74 return Status();
75}
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:115
static Status FromErrorString(const char *str)
Definition: Status.h:138
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
@ eErrorTypeWin32
Standard Win32 error codes.