blob: 40a321225405515e3643a9edf797487be2473415 [file] [log] [blame]
Alexander Shaposhnikov696bd632016-11-26 05:23:44 +00001//===-- 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 Bellob2f1fb22013-08-23 12:44:05 +000010#include "lldb/Host/ProcessRunLock.h"
Virgile Bello0a3b1512013-09-04 13:56:11 +000011#include "lldb/Host/windows/windows.h"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000012
Kate Stoneb9c1b512016-09-06 20:57:50 +000013namespace {
Virgile Bellob2f1fb22013-08-23 12:44:05 +000014#if defined(__MINGW32__)
Zachary Turner275806f2015-05-14 22:50:19 +000015// Taken from WinNT.h
Kate Stoneb9c1b512016-09-06 20:57:50 +000016typedef struct _RTL_SRWLOCK { PVOID Ptr; } RTL_SRWLOCK, *PRTL_SRWLOCK;
Virgile Bellob2f1fb22013-08-23 12:44:05 +000017
Zachary Turner275806f2015-05-14 22:50:19 +000018// Taken from WinBase.h
19typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
Virgile Bellob2f1fb22013-08-23 12:44:05 +000020#endif
Virgile Bellob2f1fb22013-08-23 12:44:05 +000021}
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023static PSRWLOCK GetLock(lldb::rwlock_t lock) {
24 return static_cast<PSRWLOCK>(lock);
Zachary Turner275806f2015-05-14 22:50:19 +000025}
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027static bool ReadLock(lldb::rwlock_t rwlock) {
28 ::AcquireSRWLockShared(GetLock(rwlock));
29 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000030}
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032static bool ReadUnlock(lldb::rwlock_t rwlock) {
33 ::ReleaseSRWLockShared(GetLock(rwlock));
34 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000035}
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037static bool WriteLock(lldb::rwlock_t rwlock) {
38 ::AcquireSRWLockExclusive(GetLock(rwlock));
39 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000040}
41
Kate Stoneb9c1b512016-09-06 20:57:50 +000042static bool WriteTryLock(lldb::rwlock_t rwlock) {
43 return !!::TryAcquireSRWLockExclusive(GetLock(rwlock));
Zachary Turner275806f2015-05-14 22:50:19 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046static bool WriteUnlock(lldb::rwlock_t rwlock) {
47 ::ReleaseSRWLockExclusive(GetLock(rwlock));
48 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000049}
50
51using namespace lldb_private;
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053ProcessRunLock::ProcessRunLock() : m_running(false) {
54 m_rwlock = new SRWLOCK;
55 InitializeSRWLock(GetLock(m_rwlock));
Zachary Turner275806f2015-05-14 22:50:19 +000056}
57
Zachary Turner5a8ad4592016-10-05 17:07:34 +000058ProcessRunLock::~ProcessRunLock() { delete static_cast<SRWLOCK *>(m_rwlock); }
Kate Stoneb9c1b512016-09-06 20:57:50 +000059
60bool ProcessRunLock::ReadTryLock() {
61 ::ReadLock(m_rwlock);
62 if (m_running == false)
63 return true;
64 ::ReadUnlock(m_rwlock);
65 return false;
Zachary Turner275806f2015-05-14 22:50:19 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068bool ProcessRunLock::ReadUnlock() { return ::ReadUnlock(m_rwlock); }
69
70bool ProcessRunLock::SetRunning() {
71 WriteLock(m_rwlock);
72 m_running = true;
73 WriteUnlock(m_rwlock);
74 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077bool ProcessRunLock::TrySetRunning() {
78 if (WriteTryLock(m_rwlock)) {
79 bool was_running = m_running;
Zachary Turner275806f2015-05-14 22:50:19 +000080 m_running = true;
81 WriteUnlock(m_rwlock);
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 return !was_running;
83 }
84 return false;
Zachary Turner275806f2015-05-14 22:50:19 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087bool ProcessRunLock::SetStopped() {
88 WriteLock(m_rwlock);
89 m_running = false;
90 WriteUnlock(m_rwlock);
91 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000092}