Skip to content

Commit c0b647c

Browse files
[lldb] Implement bidirectional access for backing<->backed thread relationship (llvm#125300)
This enables finding the backed thread from the backing thread without going through the thread list, and it will be useful for subsequent commits. (cherry picked from commit 90a51a4)
1 parent 6447c07 commit c0b647c

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

lldb/include/lldb/Target/Thread.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,26 @@ class Thread : public std::enable_shared_from_this<Thread>,
465465

466466
virtual void ClearStackFrames();
467467

468+
/// Sets the thread that is backed by this thread.
469+
/// If backed_thread.GetBackedThread() is null, this method also calls
470+
/// backed_thread.SetBackingThread(this).
471+
/// If backed_thread.GetBackedThread() is non-null, asserts that it is equal
472+
/// to `this`.
473+
void SetBackedThread(Thread &backed_thread) {
474+
m_backed_thread = backed_thread.shared_from_this();
475+
476+
// Ensure the bidrectional relationship is preserved.
477+
Thread *backing_thread = backed_thread.GetBackingThread().get();
478+
assert(backing_thread == nullptr || backing_thread == this);
479+
if (backing_thread == nullptr)
480+
backed_thread.SetBackingThread(shared_from_this());
481+
}
482+
483+
void ClearBackedThread() { m_backed_thread.reset(); }
484+
485+
/// Returns the thread that is backed by this thread, if any.
486+
lldb::ThreadSP GetBackedThread() const { return m_backed_thread.lock(); }
487+
468488
virtual bool SetBackingThread(const lldb::ThreadSP &thread_sp) {
469489
return false;
470490
}
@@ -1376,6 +1396,9 @@ class Thread : public std::enable_shared_from_this<Thread>,
13761396
LazyBool m_override_should_notify;
13771397
mutable std::unique_ptr<ThreadPlanStack> m_null_plan_stack_up;
13781398

1399+
/// The Thread backed by this thread, if any.
1400+
lldb::ThreadWP m_backed_thread;
1401+
13791402
private:
13801403
bool m_extended_info_fetched; // Have we tried to retrieve the m_extended_info
13811404
// for this thread?

lldb/include/lldb/Target/ThreadList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ class ThreadList : public ThreadCollection {
101101

102102
lldb::ThreadSP GetThreadSPForThreadPtr(Thread *thread_ptr);
103103

104-
lldb::ThreadSP GetBackingThread(const lldb::ThreadSP &real_thread);
105-
106104
bool ShouldStop(Event *event_ptr);
107105

108106
Vote ShouldReportStop(Event *event_ptr);

lldb/source/Plugins/Process/Utility/ThreadMemory.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ class ThreadMemory : public lldb_private::Thread {
7272

7373
void ClearStackFrames() override;
7474

75-
void ClearBackingThread() override { m_backing_thread_sp.reset(); }
75+
void ClearBackingThread() override {
76+
if (m_backing_thread_sp)
77+
m_backing_thread_sp->ClearBackedThread();
78+
m_backing_thread_sp.reset();
79+
}
7680

7781
bool SetBackingThread(const lldb::ThreadSP &thread_sp) override {
7882
// printf ("Thread 0x%llx is being backed by thread 0x%llx\n", GetID(),
7983
// thread_sp->GetID());
8084
m_backing_thread_sp = thread_sp;
85+
thread_sp->SetBackedThread(*this);
8186
return (bool)thread_sp;
8287
}
8388

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
17281728
thread_sp->SetStopInfo(StopInfoSP());
17291729
// If there's a memory thread backed by this thread, we need to use it to
17301730
// calculate StopInfo.
1731-
if (ThreadSP memory_thread_sp = m_thread_list.GetBackingThread(thread_sp))
1731+
if (ThreadSP memory_thread_sp = thread_sp->GetBackedThread())
17321732
thread_sp = memory_thread_sp;
17331733

17341734
if (exc_type != 0) {

lldb/source/Target/ThreadList.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,6 @@ ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
191191
return thread_sp;
192192
}
193193

194-
ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
195-
std::lock_guard<std::recursive_mutex> guard(GetMutex());
196-
197-
ThreadSP thread_sp;
198-
const uint32_t num_threads = m_threads.size();
199-
for (uint32_t idx = 0; idx < num_threads; ++idx) {
200-
if (m_threads[idx]->GetBackingThread() == real_thread) {
201-
thread_sp = m_threads[idx];
202-
break;
203-
}
204-
}
205-
return thread_sp;
206-
}
207-
208194
ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
209195
std::lock_guard<std::recursive_mutex> guard(GetMutex());
210196

0 commit comments

Comments
 (0)