blob: 64276917fc81cda70cf68a58b134d3cbfad8ed5d [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 +000013static PSRWLOCK GetLock(lldb::rwlock_t lock) {
14 return static_cast<PSRWLOCK>(lock);
Zachary Turner275806f2015-05-14 22:50:19 +000015}
16
Kate Stoneb9c1b512016-09-06 20:57:50 +000017static bool ReadLock(lldb::rwlock_t rwlock) {
18 ::AcquireSRWLockShared(GetLock(rwlock));
19 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000020}
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022static bool ReadUnlock(lldb::rwlock_t rwlock) {
23 ::ReleaseSRWLockShared(GetLock(rwlock));
24 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000025}
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027static bool WriteLock(lldb::rwlock_t rwlock) {
28 ::AcquireSRWLockExclusive(GetLock(rwlock));
29 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000030}
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032static bool WriteTryLock(lldb::rwlock_t rwlock) {
33 return !!::TryAcquireSRWLockExclusive(GetLock(rwlock));
Zachary Turner275806f2015-05-14 22:50:19 +000034}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036static bool WriteUnlock(lldb::rwlock_t rwlock) {
37 ::ReleaseSRWLockExclusive(GetLock(rwlock));
38 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000039}
40
41using namespace lldb_private;
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043ProcessRunLock::ProcessRunLock() : m_running(false) {
44 m_rwlock = new SRWLOCK;
45 InitializeSRWLock(GetLock(m_rwlock));
Zachary Turner275806f2015-05-14 22:50:19 +000046}
47
Zachary Turner5a8ad4592016-10-05 17:07:34 +000048ProcessRunLock::~ProcessRunLock() { delete static_cast<SRWLOCK *>(m_rwlock); }
Kate Stoneb9c1b512016-09-06 20:57:50 +000049
50bool ProcessRunLock::ReadTryLock() {
51 ::ReadLock(m_rwlock);
52 if (m_running == false)
53 return true;
54 ::ReadUnlock(m_rwlock);
55 return false;
Zachary Turner275806f2015-05-14 22:50:19 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058bool ProcessRunLock::ReadUnlock() { return ::ReadUnlock(m_rwlock); }
59
60bool ProcessRunLock::SetRunning() {
61 WriteLock(m_rwlock);
62 m_running = true;
63 WriteUnlock(m_rwlock);
64 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067bool ProcessRunLock::TrySetRunning() {
68 if (WriteTryLock(m_rwlock)) {
69 bool was_running = m_running;
Zachary Turner275806f2015-05-14 22:50:19 +000070 m_running = true;
71 WriteUnlock(m_rwlock);
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 return !was_running;
73 }
74 return false;
Zachary Turner275806f2015-05-14 22:50:19 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077bool ProcessRunLock::SetStopped() {
78 WriteLock(m_rwlock);
79 m_running = false;
80 WriteUnlock(m_rwlock);
81 return true;
Zachary Turner275806f2015-05-14 22:50:19 +000082}