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