Skip to content

Commit 90a51a4

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.
1 parent 622ee03 commit 90a51a4

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
@@ -470,6 +470,26 @@ class Thread : public std::enable_shared_from_this<Thread>,
470470

471471
virtual void ClearStackFrames();
472472

473+
/// Sets the thread that is backed by this thread.
474+
/// If backed_thread.GetBackedThread() is null, this method also calls
475+
/// backed_thread.SetBackingThread(this).
476+
/// If backed_thread.GetBackedThread() is non-null, asserts that it is equal
477+
/// to `this`.
478+
void SetBackedThread(Thread &backed_thread) {
479+
m_backed_thread = backed_thread.shared_from_this();
480+
481+
// Ensure the bidrectional relationship is preserved.
482+
Thread *backing_thread = backed_thread.GetBackingThread().get();
483+
assert(backing_thread == nullptr || backing_thread == this);
484+
if (backing_thread == nullptr)
485+
backed_thread.SetBackingThread(shared_from_this());
486+
}
487+
488+
void ClearBackedThread() { m_backed_thread.reset(); }
489+
490+
/// Returns the thread that is backed by this thread, if any.
491+
lldb::ThreadSP GetBackedThread() const { return m_backed_thread.lock(); }
492+
473493
virtual bool SetBackingThread(const lldb::ThreadSP &thread_sp) {
474494
return false;
475495
}
@@ -1349,6 +1369,9 @@ class Thread : public std::enable_shared_from_this<Thread>,
13491369
LazyBool m_override_should_notify;
13501370
mutable std::unique_ptr<ThreadPlanStack> m_null_plan_stack_up;
13511371

1372+
/// The Thread backed by this thread, if any.
1373+
lldb::ThreadWP m_backed_thread;
1374+
13521375
private:
13531376
bool m_extended_info_fetched; // Have we tried to retrieve the m_extended_info
13541377
// 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)