1 //===-- ProcessRunLock.cpp --------------------------------------*- C++ -*-===//
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 #ifndef _WIN32
10 #include "lldb/Host/ProcessRunLock.h"
11 
12 namespace lldb_private {
13 
14 ProcessRunLock::ProcessRunLock() : m_running(false) {
15   int err = ::pthread_rwlock_init(&m_rwlock, NULL);
16   (void)err;
17   //#if LLDB_CONFIGURATION_DEBUG
18   //        assert(err == 0);
19   //#endif
20 }
21 
22 ProcessRunLock::~ProcessRunLock() {
23   int err = ::pthread_rwlock_destroy(&m_rwlock);
24   (void)err;
25   //#if LLDB_CONFIGURATION_DEBUG
26   //        assert(err == 0);
27   //#endif
28 }
29 
30 bool ProcessRunLock::ReadTryLock() {
31   ::pthread_rwlock_rdlock(&m_rwlock);
32   if (!m_running) {
33     return true;
34   }
35   ::pthread_rwlock_unlock(&m_rwlock);
36   return false;
37 }
38 
39 bool ProcessRunLock::ReadUnlock() {
40   return ::pthread_rwlock_unlock(&m_rwlock) == 0;
41 }
42 
43 bool ProcessRunLock::SetRunning() {
44   ::pthread_rwlock_wrlock(&m_rwlock);
45   m_running = true;
46   ::pthread_rwlock_unlock(&m_rwlock);
47   return true;
48 }
49 
50 bool ProcessRunLock::TrySetRunning() {
51   bool r;
52 
53   if (::pthread_rwlock_trywrlock(&m_rwlock) == 0) {
54     r = !m_running;
55     m_running = true;
56     ::pthread_rwlock_unlock(&m_rwlock);
57     return r;
58   }
59   return false;
60 }
61 
62 bool ProcessRunLock::SetStopped() {
63   ::pthread_rwlock_wrlock(&m_rwlock);
64   m_running = false;
65   ::pthread_rwlock_unlock(&m_rwlock);
66   return true;
67 }
68 }
69 
70 #endif
71