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

Conversation

JDevlieghere
Copy link
Member

The debugserver code predates modern C++, but with C++11 and later there's no need to have something like PThreadMutex. This migrates MachThreadList away from that class in preparation for removing PThreadMutex.

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
MachThreadList away from that class in preparation for removing
PThreadMutex.
@llvmbot
Copy link
Member

llvmbot commented Apr 27, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

The debugserver code predates modern C++, but with C++11 and later there's no need to have something like PThreadMutex. This migrates MachThreadList away from that class in preparation for removing PThreadMutex.


Full diff: https://github.com/llvm/llvm-project/pull/137542.diff

2 Files Affected:

  • (modified) lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp (+18-20)
  • (modified) lldb/tools/debugserver/source/MacOSX/MachThreadList.h (+1-1)
diff --git a/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp b/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp
index cf3687985173f..36e6c1942b35f 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp
+++ b/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp
@@ -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;
 
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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;
@@ -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();
 }
 
@@ -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()};
@@ -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
@@ -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);
@@ -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.
@@ -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) {
@@ -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) {
@@ -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);
@@ -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
@@ -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.
@@ -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) {
diff --git a/lldb/tools/debugserver/source/MacOSX/MachThreadList.h b/lldb/tools/debugserver/source/MacOSX/MachThreadList.h
index 8544c3ab73160..20af84fb3522a 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachThreadList.h
+++ b/lldb/tools/debugserver/source/MacOSX/MachThreadList.h
@@ -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;
 };

@JDevlieghere JDevlieghere merged commit 503ebad into llvm:main Apr 27, 2025
12 checks passed
@JDevlieghere JDevlieghere deleted the debugserver-MachThreadList branch April 27, 2025 20:08
jyli0116 pushed a commit to jyli0116/llvm-project that referenced this pull request Apr 28, 2025
…vm#137542)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
MachThreadList away from that class in preparation for removing
PThreadMutex.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…vm#137542)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
MachThreadList away from that class in preparation for removing
PThreadMutex.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…vm#137542)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
MachThreadList away from that class in preparation for removing
PThreadMutex.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…vm#137542)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
MachThreadList away from that class in preparation for removing
PThreadMutex.
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
…vm#137542)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
MachThreadList away from that class in preparation for removing
PThreadMutex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants