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