1//===-- ProcessRunLock.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
9#include "lldb/Host/ProcessRunLock.h"
10#include "lldb/Host/windows/windows.h"
11
12static PSRWLOCK GetLock(lldb::rwlock_t lock) {
13 return static_cast<PSRWLOCK>(lock);
14}
15
16static bool ReadLock(lldb::rwlock_t rwlock) {
17 ::AcquireSRWLockShared(GetLock(rwlock));
18 return true;
19}
20
21static bool ReadUnlock(lldb::rwlock_t rwlock) {
22 ::ReleaseSRWLockShared(GetLock(rwlock));
23 return true;
24}
25
26static bool WriteLock(lldb::rwlock_t rwlock) {
27 ::AcquireSRWLockExclusive(GetLock(rwlock));
28 return true;
29}
30
31static bool WriteTryLock(lldb::rwlock_t rwlock) {
32 return !!::TryAcquireSRWLockExclusive(GetLock(rwlock));
33}
34
35static bool WriteUnlock(lldb::rwlock_t rwlock) {
36 ::ReleaseSRWLockExclusive(GetLock(rwlock));
37 return true;
38}
39
40using namespace lldb_private;
41
42ProcessRunLock::ProcessRunLock() : m_running(false) {
43 m_rwlock = new SRWLOCK;
44 InitializeSRWLock(GetLock(m_rwlock));
45}
46
47ProcessRunLock::~ProcessRunLock() { delete static_cast<SRWLOCK *>(m_rwlock); }
48
49bool ProcessRunLock::ReadTryLock() {
50 ::ReadLock(rwlock: m_rwlock);
51 if (m_running == false)
52 return true;
53 ::ReadUnlock(rwlock: m_rwlock);
54 return false;
55}
56
57bool ProcessRunLock::ReadUnlock() { return ::ReadUnlock(rwlock: m_rwlock); }
58
59bool ProcessRunLock::SetRunning() {
60 WriteLock(rwlock: m_rwlock);
61 m_running = true;
62 WriteUnlock(rwlock: m_rwlock);
63 return true;
64}
65
66bool ProcessRunLock::TrySetRunning() {
67 if (WriteTryLock(rwlock: m_rwlock)) {
68 bool was_running = m_running;
69 m_running = true;
70 WriteUnlock(rwlock: m_rwlock);
71 return !was_running;
72 }
73 return false;
74}
75
76bool ProcessRunLock::SetStopped() {
77 WriteLock(rwlock: m_rwlock);
78 m_running = false;
79 WriteUnlock(rwlock: m_rwlock);
80 return true;
81}
82

source code of lldb/source/Host/windows/ProcessRunLock.cpp