Skip to content

[debugserver] Migrate MachThreadList away from PThreadMutex (NFC) #137542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include <memory>

MachThreadList::MachThreadList()
: m_threads(), m_threads_mutex(PTHREAD_MUTEX_RECURSIVE),
m_is_64_bit(false) {}
: m_threads(), m_threads_mutex(), m_is_64_bit(false) {}

MachThreadList::~MachThreadList() = default;

Expand Down Expand Up @@ -116,7 +115,7 @@ const char *MachThreadList::GetThreadInfo(nub_thread_t tid) const {
}

MachThreadSP MachThreadList::GetThreadByID(nub_thread_t tid) const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
MachThreadSP thread_sp;
const size_t num_threads = m_threads.size();
for (size_t idx = 0; idx < num_threads; ++idx) {
Expand All @@ -130,7 +129,7 @@ MachThreadSP MachThreadList::GetThreadByID(nub_thread_t tid) const {

MachThreadSP
MachThreadList::GetThreadByMachPortNumber(thread_t mach_port_number) const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
MachThreadSP thread_sp;
const size_t num_threads = m_threads.size();
for (size_t idx = 0; idx < num_threads; ++idx) {
Expand All @@ -144,7 +143,7 @@ MachThreadList::GetThreadByMachPortNumber(thread_t mach_port_number) const {

nub_thread_t
MachThreadList::GetThreadIDByMachPortNumber(thread_t mach_port_number) const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
MachThreadSP thread_sp;
const size_t num_threads = m_threads.size();
for (size_t idx = 0; idx < num_threads; ++idx) {
Expand All @@ -157,7 +156,7 @@ MachThreadList::GetThreadIDByMachPortNumber(thread_t mach_port_number) const {

thread_t MachThreadList::GetMachPortNumberByThreadID(
nub_thread_t globally_unique_id) const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
MachThreadSP thread_sp;
const size_t num_threads = m_threads.size();
for (size_t idx = 0; idx < num_threads; ++idx) {
Expand Down Expand Up @@ -219,12 +218,12 @@ bool MachThreadList::RestoreRegisterState(nub_thread_t tid, uint32_t save_id) {
}

nub_size_t MachThreadList::NumThreads() const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
return m_threads.size();
}

nub_thread_t MachThreadList::ThreadIDAtIndex(nub_size_t idx) const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
if (idx < m_threads.size())
return m_threads[idx]->ThreadID();
return INVALID_NUB_THREAD;
Expand All @@ -248,7 +247,7 @@ bool MachThreadList::NotifyException(MachException::Data &exc) {
}

void MachThreadList::Clear() {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
m_threads.clear();
}

Expand All @@ -259,7 +258,7 @@ MachThreadList::UpdateThreadList(MachProcess *process, bool update,
DNBLogThreadedIf(LOG_THREAD, "MachThreadList::UpdateThreadList (pid = %4.4x, "
"update = %u) process stop count = %u",
process->ProcessID(), update, process->StopCount());
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);

if (process->StopCount() == 0) {
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, process->ProcessID()};
Expand Down Expand Up @@ -346,8 +345,7 @@ MachThreadList::UpdateThreadList(MachProcess *process, bool update,
}

void MachThreadList::CurrentThread(MachThreadSP &thread_sp) {
// locker will keep a mutex locked until it goes out of scope
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
if (m_current_thread.get() == NULL) {
// Figure out which thread is going to be our current thread.
// This is currently done by finding the first thread in the list
Expand All @@ -364,7 +362,7 @@ void MachThreadList::CurrentThread(MachThreadSP &thread_sp) {
}

void MachThreadList::Dump() const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
const size_t num_threads = m_threads.size();
for (uint32_t idx = 0; idx < num_threads; ++idx) {
m_threads[idx]->Dump(idx);
Expand All @@ -373,7 +371,7 @@ void MachThreadList::Dump() const {

void MachThreadList::ProcessWillResume(
MachProcess *process, const DNBThreadResumeActions &thread_actions) {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);

// Update our thread list, because sometimes libdispatch or the kernel
// will spawn threads while a task is suspended.
Expand Down Expand Up @@ -447,7 +445,7 @@ void MachThreadList::ProcessWillResume(
}

uint32_t MachThreadList::ProcessDidStop(MachProcess *process) {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
// Update our thread list
const uint32_t num_threads = UpdateThreadList(process, true);
for (uint32_t idx = 0; idx < num_threads; ++idx) {
Expand All @@ -466,7 +464,7 @@ uint32_t MachThreadList::ProcessDidStop(MachProcess *process) {
// true if we should stop and notify our clients
// false if we should resume our child process and skip notification
bool MachThreadList::ShouldStop(bool &step_more) {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
uint32_t should_stop = false;
const size_t num_threads = m_threads.size();
for (uint32_t idx = 0; !should_stop && idx < num_threads; ++idx) {
Expand All @@ -476,7 +474,7 @@ bool MachThreadList::ShouldStop(bool &step_more) {
}

void MachThreadList::NotifyBreakpointChanged(const DNBBreakpoint *bp) {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
const size_t num_threads = m_threads.size();
for (uint32_t idx = 0; idx < num_threads; ++idx) {
m_threads[idx]->NotifyBreakpointChanged(bp);
Expand All @@ -489,7 +487,7 @@ uint32_t MachThreadList::DoHardwareBreakpointAction(
return INVALID_NUB_HW_INDEX;

uint32_t hw_index = INVALID_NUB_HW_INDEX;
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
const size_t num_threads = m_threads.size();
// On Mac OS X we have to prime the control registers for new threads. We do
// this using the control register data for the first thread, for lack of a
Expand Down Expand Up @@ -555,7 +553,7 @@ bool MachThreadList::DisableHardwareBreakpoint(const DNBBreakpoint *bp) const {
}

uint32_t MachThreadList::NumSupportedHardwareWatchpoints() const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
const size_t num_threads = m_threads.size();
// Use an arbitrary thread to retrieve the number of supported hardware
// watchpoints.
Expand All @@ -566,7 +564,7 @@ uint32_t MachThreadList::NumSupportedHardwareWatchpoints() const {

uint32_t MachThreadList::GetThreadIndexForThreadStoppedWithSignal(
const int signo) const {
PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
uint32_t should_stop = false;
const size_t num_threads = m_threads.size();
for (uint32_t idx = 0; !should_stop && idx < num_threads; ++idx) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/debugserver/source/MacOSX/MachThreadList.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class MachThreadList {
// const_iterator FindThreadByID (thread_t tid) const;

collection m_threads;
mutable PThreadMutex m_threads_mutex;
mutable std::recursive_mutex m_threads_mutex;
MachThreadSP m_current_thread;
bool m_is_64_bit;
};
Expand Down
Loading