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