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