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