LLDB mainline
Policy.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_TARGET_POLICY_H
10#define LLDB_TARGET_POLICY_H
11
12#include "llvm/ADT/SmallVector.h"
13
14#include <cassert>
15
16namespace lldb_private {
17
18class Stream;
19
20/// Describes what view of the process a thread should see and what
21/// operations it is allowed to perform.
22///
23/// This replaces ad-hoc checks like CurrentThreadIsPrivateStateThread() with
24/// a unified, composable mechanism. Code consults the current policy on the
25/// per-thread PolicyStack instead of comparing host thread identities.
26///
27/// One motivating case is frame providers, which layer a public illusion on
28/// top of the private unwinder stack. The private state thread must see the
29/// raw unwinder frames, while public clients see the augmented view. Rather
30/// than checking thread identity at every callsite, the private state thread
31/// pushes Policy::PrivateState() and the rest follows from the policy.
32struct Policy {
33 /// What view of the process this thread sees.
34 enum class View {
35 Public, ///< Provider-augmented frames, public state, public run lock.
36 Private, ///< Parent (unwinder) frames, private state, private run lock.
37 };
38
39 /// What operations this thread is allowed to perform.
40 /// Enforced at specific callsites, not by the policy itself.
41 struct Capabilities {
43 /// Whether expression evaluation may resume all threads to avoid
44 /// deadlocks (e.g. when a lock is held by another thread).
46 /// Whether the expression runner may fall back to running all threads
47 /// after a single-thread attempt times out.
52 };
53
56
57 static Policy PublicState() { return {}; }
58
60 Policy p;
64 return p;
65 }
66
68 Policy p;
70 return p;
71 }
72
73 void Dump(Stream &s) const;
74};
75
76/// Per-thread policy stack.
77///
78/// The stack lives in thread_local storage. Each thread has its own stack,
79/// initialized with a default-constructed base entry that is never popped.
80/// RAII guards (Guard) push and pop policies.
81///
82/// For thread pool workers that don't inherit thread_local storage, the
83/// policy must be passed into the lambda and pushed onto the worker
84/// thread's stack when the task starts.
86public:
87 static PolicyStack &Get() {
88 static thread_local PolicyStack s_stack;
89 return s_stack;
90 }
91
92 Policy Current() const;
93
94 void Push(Policy policy) { m_stack.push_back(std::move(policy)); }
95
96 void Pop() {
97 assert(!m_stack.empty() && "can't pop the base policy");
98 m_stack.pop_back();
99 }
100
101 void Dump(Stream &s) const;
102
103 /// RAII guard that pushes a policy on construction and pops on destruction.
104 class Guard {
105 public:
106 explicit Guard(Policy policy) { Get().Push(std::move(policy)); }
107 ~Guard() { Get().Pop(); }
108
109 Guard(const Guard &) = delete;
110 Guard &operator=(const Guard &) = delete;
111 };
112
113private:
114 llvm::SmallVector<Policy> m_stack = {Policy{}};
115};
116
117} // namespace lldb_private
118
119#endif // LLDB_TARGET_POLICY_H
Guard & operator=(const Guard &)=delete
Guard(const Guard &)=delete
Per-thread policy stack.
Definition Policy.h:85
void Dump(Stream &s) const
Definition Policy.cpp:39
void Push(Policy policy)
Definition Policy.h:94
static PolicyStack & Get()
Definition Policy.h:87
llvm::SmallVector< Policy > m_stack
Definition Policy.h:114
Policy Current() const
Definition Policy.cpp:17
A stream class that can stream formatted output to a file.
Definition Stream.h:28
A class that represents a running process on the host machine.
What operations this thread is allowed to perform.
Definition Policy.h:41
bool can_run_all_threads
Whether expression evaluation may resume all threads to avoid deadlocks (e.g.
Definition Policy.h:45
bool can_try_all_threads
Whether the expression runner may fall back to running all threads after a single-thread attempt time...
Definition Policy.h:48
Describes what view of the process a thread should see and what operations it is allowed to perform.
Definition Policy.h:32
Capabilities capabilities
Definition Policy.h:55
static Policy PublicStateRunningExpression()
Definition Policy.h:67
View
What view of the process this thread sees.
Definition Policy.h:34
@ Public
Provider-augmented frames, public state, public run lock.
Definition Policy.h:35
@ Private
Parent (unwinder) frames, private state, private run lock.
Definition Policy.h:36
void Dump(Stream &s) const
Definition Policy.cpp:27
static Policy PrivateState()
Definition Policy.h:59
static Policy PublicState()
Definition Policy.h:57