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
12namespace lldb_private {
13
14ProcessRunLock::ProcessRunLock() : m_running(false) {
15  int err = ::pthread_rwlock_init(&m_rwlock, nullptr);
16  (void)err;
17}
18
19ProcessRunLock::~ProcessRunLock() {
20  int err = ::pthread_rwlock_destroy(&m_rwlock);
21  (void)err;
22}
23
24bool ProcessRunLock::ReadTryLock() {
25  ::pthread_rwlock_rdlock(&m_rwlock);
26  if (!m_running) {
27    return true;
28  }
29  ::pthread_rwlock_unlock(&m_rwlock);
30  return false;
31}
32
33bool ProcessRunLock::ReadUnlock() {
34  return ::pthread_rwlock_unlock(&m_rwlock) == 0;
35}
36
37bool ProcessRunLock::SetRunning() {
38  ::pthread_rwlock_wrlock(&m_rwlock);
39  m_running = true;
40  ::pthread_rwlock_unlock(&m_rwlock);
41  return true;
42}
43
44bool ProcessRunLock::TrySetRunning() {
45  bool r;
46
47  if (::pthread_rwlock_trywrlock(&m_rwlock) == 0) {
48    r = !m_running;
49    m_running = true;
50    ::pthread_rwlock_unlock(&m_rwlock);
51    return r;
52  }
53  return false;
54}
55
56bool ProcessRunLock::SetStopped() {
57  ::pthread_rwlock_wrlock(&m_rwlock);
58  m_running = false;
59  ::pthread_rwlock_unlock(&m_rwlock);
60  return true;
61}
62}
63
64#endif
65