xref: /src/contrib/llvm-project/lldb/source/Host/common/ProcessRunLock.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1cfca06d7SDimitry Andric //===-- ProcessRunLock.cpp ------------------------------------------------===//
214f1b3e8SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
614f1b3e8SDimitry Andric //
714f1b3e8SDimitry Andric //===----------------------------------------------------------------------===//
8f21a844fSEd Maste 
914f1b3e8SDimitry Andric #ifndef _WIN32
10f21a844fSEd Maste #include "lldb/Host/ProcessRunLock.h"
11f21a844fSEd Maste 
12f21a844fSEd Maste namespace lldb_private {
13f21a844fSEd Maste 
ProcessRunLock()14344a3780SDimitry Andric ProcessRunLock::ProcessRunLock() {
155f29bb8aSDimitry Andric   int err = ::pthread_rwlock_init(&m_rwlock, nullptr);
1614f1b3e8SDimitry Andric   (void)err;
17f21a844fSEd Maste }
18f21a844fSEd Maste 
~ProcessRunLock()1914f1b3e8SDimitry Andric ProcessRunLock::~ProcessRunLock() {
2014f1b3e8SDimitry Andric   int err = ::pthread_rwlock_destroy(&m_rwlock);
2114f1b3e8SDimitry Andric   (void)err;
22f21a844fSEd Maste }
23f21a844fSEd Maste 
ReadTryLock()2414f1b3e8SDimitry Andric bool ProcessRunLock::ReadTryLock() {
25f21a844fSEd Maste   ::pthread_rwlock_rdlock(&m_rwlock);
2694994d37SDimitry Andric   if (!m_running) {
27e3b55780SDimitry Andric     // coverity[missing_unlock]
28f21a844fSEd Maste     return true;
29f21a844fSEd Maste   }
30f21a844fSEd Maste   ::pthread_rwlock_unlock(&m_rwlock);
31f21a844fSEd Maste   return false;
32f21a844fSEd Maste }
33f21a844fSEd Maste 
ReadUnlock()3414f1b3e8SDimitry Andric bool ProcessRunLock::ReadUnlock() {
35f21a844fSEd Maste   return ::pthread_rwlock_unlock(&m_rwlock) == 0;
36f21a844fSEd Maste }
37f21a844fSEd Maste 
SetRunning()3814f1b3e8SDimitry Andric bool ProcessRunLock::SetRunning() {
39f21a844fSEd Maste   ::pthread_rwlock_wrlock(&m_rwlock);
40f21a844fSEd Maste   m_running = true;
41f21a844fSEd Maste   ::pthread_rwlock_unlock(&m_rwlock);
42f21a844fSEd Maste   return true;
43f21a844fSEd Maste }
44f21a844fSEd Maste 
TrySetRunning()4514f1b3e8SDimitry Andric bool ProcessRunLock::TrySetRunning() {
46f21a844fSEd Maste   bool r;
47f21a844fSEd Maste 
4814f1b3e8SDimitry Andric   if (::pthread_rwlock_trywrlock(&m_rwlock) == 0) {
49f21a844fSEd Maste     r = !m_running;
50f21a844fSEd Maste     m_running = true;
51f21a844fSEd Maste     ::pthread_rwlock_unlock(&m_rwlock);
52f21a844fSEd Maste     return r;
53f21a844fSEd Maste   }
54f21a844fSEd Maste   return false;
55f21a844fSEd Maste }
56f21a844fSEd Maste 
SetStopped()5714f1b3e8SDimitry Andric bool ProcessRunLock::SetStopped() {
58f21a844fSEd Maste   ::pthread_rwlock_wrlock(&m_rwlock);
59f21a844fSEd Maste   m_running = false;
60f21a844fSEd Maste   ::pthread_rwlock_unlock(&m_rwlock);
61f21a844fSEd Maste   return true;
62f21a844fSEd Maste }
63f21a844fSEd Maste }
64f21a844fSEd Maste 
65f21a844fSEd Maste #endif
66