Skip to content

Commit dca2bc4

Browse files
committed
Add a mutex to the ThreadPlanStackMap class.
We've seen very occasional crashes that we can only explain by simultaneous access to the ThreadPlanStackMap, so I'm adding a mutex to protect it. Differential Revision: https://reviews.llvm.org/D124029
1 parent 6f79700 commit dca2bc4

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lldb/include/lldb/Target/ThreadPlanStack.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ class ThreadPlanStackMap {
123123
bool check_for_new = true);
124124

125125
void AddThread(Thread &thread) {
126+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
126127
lldb::tid_t tid = thread.GetID();
127128
m_plans_list.emplace(tid, thread);
128129
}
129130

130131
bool RemoveTID(lldb::tid_t tid) {
132+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
131133
auto result = m_plans_list.find(tid);
132134
if (result == m_plans_list.end())
133135
return false;
@@ -137,6 +139,7 @@ class ThreadPlanStackMap {
137139
}
138140

139141
ThreadPlanStack *Find(lldb::tid_t tid) {
142+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
140143
auto result = m_plans_list.find(tid);
141144
if (result == m_plans_list.end())
142145
return nullptr;
@@ -154,6 +157,7 @@ class ThreadPlanStackMap {
154157
}
155158

156159
void Clear() {
160+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
157161
for (auto &plan : m_plans_list)
158162
plan.second.ThreadDestroyed(nullptr);
159163
m_plans_list.clear();
@@ -172,8 +176,10 @@ class ThreadPlanStackMap {
172176

173177
private:
174178
Process &m_process;
179+
mutable std::recursive_mutex m_stack_map_mutex;
175180
using PlansList = std::unordered_map<lldb::tid_t, ThreadPlanStack>;
176181
PlansList m_plans_list;
182+
177183
};
178184

179185
} // namespace lldb_private

lldb/source/Target/ThreadPlanStack.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ void ThreadPlanStackMap::Update(ThreadList &current_threads,
400400
bool delete_missing,
401401
bool check_for_new) {
402402

403+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
403404
// Now find all the new threads and add them to the map:
404405
if (check_for_new) {
405406
for (auto thread : current_threads.Threads()) {
@@ -434,6 +435,7 @@ void ThreadPlanStackMap::DumpPlans(Stream &strm,
434435
lldb::DescriptionLevel desc_level,
435436
bool internal, bool condense_if_trivial,
436437
bool skip_unreported) {
438+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
437439
for (auto &elem : m_plans_list) {
438440
lldb::tid_t tid = elem.first;
439441
uint32_t index_id = 0;
@@ -470,6 +472,7 @@ bool ThreadPlanStackMap::DumpPlansForTID(Stream &strm, lldb::tid_t tid,
470472
bool internal,
471473
bool condense_if_trivial,
472474
bool skip_unreported) {
475+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
473476
uint32_t index_id = 0;
474477
ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(tid);
475478

@@ -509,6 +512,7 @@ bool ThreadPlanStackMap::DumpPlansForTID(Stream &strm, lldb::tid_t tid,
509512

510513
bool ThreadPlanStackMap::PrunePlansForTID(lldb::tid_t tid) {
511514
// We only remove the plans for unreported TID's.
515+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
512516
ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(tid);
513517
if (thread_sp)
514518
return false;

0 commit comments

Comments
 (0)