Alexander Shaposhnikov | 696bd63 | 2016-11-26 05:23:44 +0000 | [diff] [blame] | 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 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 10 | #include "lldb/Host/ProcessRunLock.h" |
Virgile Bello | 0a3b151 | 2013-09-04 13:56:11 +0000 | [diff] [blame] | 11 | #include "lldb/Host/windows/windows.h" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 12 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | static PSRWLOCK GetLock(lldb::rwlock_t lock) { |
| 14 | return static_cast<PSRWLOCK>(lock); |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 15 | } |
| 16 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | static bool ReadLock(lldb::rwlock_t rwlock) { |
| 18 | ::AcquireSRWLockShared(GetLock(rwlock)); |
| 19 | return true; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | static bool ReadUnlock(lldb::rwlock_t rwlock) { |
| 23 | ::ReleaseSRWLockShared(GetLock(rwlock)); |
| 24 | return true; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | static bool WriteLock(lldb::rwlock_t rwlock) { |
| 28 | ::AcquireSRWLockExclusive(GetLock(rwlock)); |
| 29 | return true; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | static bool WriteTryLock(lldb::rwlock_t rwlock) { |
| 33 | return !!::TryAcquireSRWLockExclusive(GetLock(rwlock)); |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | static bool WriteUnlock(lldb::rwlock_t rwlock) { |
| 37 | ::ReleaseSRWLockExclusive(GetLock(rwlock)); |
| 38 | return true; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | using namespace lldb_private; |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | ProcessRunLock::ProcessRunLock() : m_running(false) { |
| 44 | m_rwlock = new SRWLOCK; |
| 45 | InitializeSRWLock(GetLock(m_rwlock)); |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 48 | ProcessRunLock::~ProcessRunLock() { delete static_cast<SRWLOCK *>(m_rwlock); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | |
| 50 | bool ProcessRunLock::ReadTryLock() { |
| 51 | ::ReadLock(m_rwlock); |
| 52 | if (m_running == false) |
| 53 | return true; |
| 54 | ::ReadUnlock(m_rwlock); |
| 55 | return false; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | bool ProcessRunLock::ReadUnlock() { return ::ReadUnlock(m_rwlock); } |
| 59 | |
| 60 | bool ProcessRunLock::SetRunning() { |
| 61 | WriteLock(m_rwlock); |
| 62 | m_running = true; |
| 63 | WriteUnlock(m_rwlock); |
| 64 | return true; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | bool ProcessRunLock::TrySetRunning() { |
| 68 | if (WriteTryLock(m_rwlock)) { |
| 69 | bool was_running = m_running; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 70 | m_running = true; |
| 71 | WriteUnlock(m_rwlock); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | return !was_running; |
| 73 | } |
| 74 | return false; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | bool ProcessRunLock::SetStopped() { |
| 78 | WriteLock(m_rwlock); |
| 79 | m_running = false; |
| 80 | WriteUnlock(m_rwlock); |
| 81 | return true; |
Zachary Turner | 275806f | 2015-05-14 22:50:19 +0000 | [diff] [blame] | 82 | } |