LLDB mainline
ProcessRunLock.h
Go to the documentation of this file.
1//===-- ProcessRunLock.h ----------------------------------------*- 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 LLDB_HOST_PROCESSRUNLOCK_H
10#define LLDB_HOST_PROCESSRUNLOCK_H
11
12#include <cassert>
13#include <cstdint>
14#include <ctime>
15#include <mutex>
16
17#include "llvm/ADT/DenseMap.h"
18
19#include "lldb/lldb-defines.h"
20#include "lldb/lldb-types.h"
21
22/// Enumerations for broadcasting.
23namespace lldb_private {
24
25/// \class ProcessRunLock ProcessRunLock.h "lldb/Host/ProcessRunLock.h"
26/// Read/write lock around the process running/stopped state.
27///
28/// POSIX rwlocks aren't reader-recursive: a thread holding the read
29/// lock can deadlock against a pending writer if it re-acquires
30/// directly. ProcessRunLocker tracks a per-thread recursion count so
31/// re-entry skips the rwlock; the raw primitives are private.
32
34public:
37
38 /// Set the process to running. Returns true if the process was stopped.
39 /// Return false if the process was running.
40 bool SetRunning();
41
42 /// Set the process to stopped. Returns true if the process was running.
43 /// Returns false if the process was stopped.
44 bool SetStopped();
45
46 /// RAII helper around the read-lock side of ProcessRunLock. Supports
47 /// same-thread recursion (see class doc).
48 ///
49 /// Move-assignment unlocks the destination first, then takes the
50 /// source's lock. Cross-thread move of a held locker is fatal — the
51 /// same thread that called rdlock must call unlock.
53 public:
54 ProcessRunLocker() = default;
58
59 bool IsLocked() const { return m_lock; }
60
61 /// Try to acquire the read lock. If this thread already holds the
62 /// read lock on this ProcessRunLock, the underlying rwlock is bypassed
63 /// and the per-instance recursion count for this thread is bumped
64 /// instead.
65 bool TryLock(ProcessRunLock *lock);
66
67 protected:
68 void Unlock();
69
71 uint64_t m_thread = 0;
72
73 private:
76 };
77
78protected:
80 bool m_running = false;
81
82private:
83 ProcessRunLock(const ProcessRunLock &) = delete;
84 const ProcessRunLock &operator=(const ProcessRunLock &) = delete;
85
86 bool ReadTryLock();
87 bool ReadUnlock();
88 friend class ProcessRunLocker;
89
91 llvm::DenseMap<uint64_t, uint32_t> m_recursion;
92};
93
94} // namespace lldb_private
95
96#endif // LLDB_HOST_PROCESSRUNLOCK_H
RAII helper around the read-lock side of ProcessRunLock.
ProcessRunLocker & operator=(ProcessRunLocker &&other)
ProcessRunLocker(const ProcessRunLocker &)=delete
const ProcessRunLocker & operator=(const ProcessRunLocker &)=delete
bool TryLock(ProcessRunLock *lock)
Try to acquire the read lock.
bool SetStopped()
Set the process to stopped.
ProcessRunLock(const ProcessRunLock &)=delete
bool SetRunning()
Set the process to running.
llvm::DenseMap< uint64_t, uint32_t > m_recursion
const ProcessRunLock & operator=(const ProcessRunLock &)=delete
A class that represents a running process on the host machine.
pthread_rwlock_t rwlock_t
Definition lldb-types.h:56