Skip to content

Commit 46575a6

Browse files
jiminghamkastiglione
authored andcommitted
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 fc0ab39 commit 46575a6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lldb/include/lldb/Target/ThreadPlanStack.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class ThreadPlanStackMap {
132132
bool check_for_new = true);
133133

134134
void AddThread(Thread &thread) {
135+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
135136
lldb::tid_t tid = thread.GetID();
136137
// If we already have a ThreadPlanStack for this thread, use it.
137138
if (m_plans_list.find(tid) != m_plans_list.end())
@@ -143,6 +144,7 @@ class ThreadPlanStackMap {
143144
}
144145

145146
bool RemoveTID(lldb::tid_t tid) {
147+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
146148
auto result = m_plans_list.find(tid);
147149
if (result == m_plans_list.end())
148150
return false;
@@ -165,6 +167,7 @@ class ThreadPlanStackMap {
165167
}
166168

167169
ThreadPlanStack *Find(lldb::tid_t tid) {
170+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
168171
auto result = m_plans_list.find(tid);
169172
if (result == m_plans_list.end())
170173
return nullptr;
@@ -177,12 +180,14 @@ class ThreadPlanStackMap {
177180
/// This is useful in situations like when a new Thread list is being
178181
/// generated.
179182
void ClearThreadCache() {
183+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
180184
for (auto &plan_list : m_plans_list)
181185
plan_list.second->ClearThreadCache();
182186
}
183187

184188
// rename to Reactivate?
185189
void Activate(ThreadPlanStack &stack) {
190+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
186191
// Remove this from the detached plan list:
187192
auto end = m_detached_plans.end();
188193
auto iter = std::find_if(m_detached_plans.begin(), end,
@@ -198,6 +203,7 @@ class ThreadPlanStackMap {
198203
}
199204

200205
void ScanForDetachedPlanStacks() {
206+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
201207
llvm::SmallVector<lldb::tid_t, 2> invalidated_tids;
202208
for (auto &pair : m_plans_list)
203209
if (pair.second->GetTID() != pair.first)
@@ -217,10 +223,12 @@ class ThreadPlanStackMap {
217223
// scheduled.
218224
// The vector will never have null ThreadPlanStacks in it.
219225
std::vector<ThreadPlanStack *> &GetDetachedPlanStacks() {
226+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
220227
return m_detached_plans;
221228
}
222229

223230
void Clear() {
231+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
224232
for (auto &plan : m_plans_list)
225233
plan.second->ThreadDestroyed(nullptr);
226234
m_plans_list.clear();
@@ -255,6 +263,7 @@ class ThreadPlanStackMap {
255263
PlansStore m_plans_up_container;
256264
std::vector<ThreadPlanStack *> m_detached_plans;
257265

266+
mutable std::recursive_mutex m_stack_map_mutex;
258267
using PlansList = std::unordered_map<lldb::tid_t, ThreadPlanStack *>;
259268
PlansList m_plans_list;
260269
};

lldb/source/Target/ThreadPlanStack.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ void ThreadPlanStackMap::Update(ThreadList &current_threads,
411411
bool delete_missing,
412412
bool check_for_new) {
413413

414+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
414415
// Now find all the new threads and add them to the map:
415416
if (check_for_new) {
416417
for (auto thread : current_threads.Threads()) {
@@ -445,6 +446,7 @@ void ThreadPlanStackMap::DumpPlans(Stream &strm,
445446
lldb::DescriptionLevel desc_level,
446447
bool internal, bool condense_if_trivial,
447448
bool skip_unreported) {
449+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
448450
for (auto &elem : m_plans_list) {
449451
lldb::tid_t tid = elem.first;
450452
uint32_t index_id = 0;
@@ -481,6 +483,7 @@ bool ThreadPlanStackMap::DumpPlansForTID(Stream &strm, lldb::tid_t tid,
481483
bool internal,
482484
bool condense_if_trivial,
483485
bool skip_unreported) {
486+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
484487
uint32_t index_id = 0;
485488
ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(tid);
486489

@@ -520,6 +523,7 @@ bool ThreadPlanStackMap::DumpPlansForTID(Stream &strm, lldb::tid_t tid,
520523

521524
bool ThreadPlanStackMap::PrunePlansForTID(lldb::tid_t tid) {
522525
// We only remove the plans for unreported TID's.
526+
std::lock_guard<std::recursive_mutex> guard(m_stack_map_mutex);
523527
ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(tid);
524528
if (thread_sp)
525529
return false;

0 commit comments

Comments
 (0)