Skip to content

[debugserver] Migrate DNBTimer away from PThreadMutex (NFC) #137540

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 DNBTimer 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
DNBTimer 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 DNBTimer away from that class in preparation for removing PThreadMutex.


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

1 Files Affected:

  • (modified) lldb/tools/debugserver/source/DNBTimer.h (+23-19)
diff --git a/lldb/tools/debugserver/source/DNBTimer.h b/lldb/tools/debugserver/source/DNBTimer.h
index 2251c50fb8768..78b64c6583d3d 100644
--- a/lldb/tools/debugserver/source/DNBTimer.h
+++ b/lldb/tools/debugserver/source/DNBTimer.h
@@ -14,7 +14,6 @@
 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBTIMER_H
 
 #include "DNBDefs.h"
-#include "PThreadMutex.h"
 #include <cstdint>
 #include <memory>
 #include <sys/time.h>
@@ -22,17 +21,17 @@
 class DNBTimer {
 public:
   // Constructors and Destructors
-  DNBTimer(bool threadSafe) : m_mutexAP() {
+  DNBTimer(bool threadSafe) {
     if (threadSafe)
-      m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
+      m_mutex_up = std::make_unique<std::recursive_mutex>();
     Reset();
   }
 
-  DNBTimer(const DNBTimer &rhs) : m_mutexAP() {
+  DNBTimer(const DNBTimer &rhs) {
     // Create a new mutex to make this timer thread safe as well if
     // the timer we are copying is thread safe
     if (rhs.IsThreadSafe())
-      m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
+      m_mutex_up = std::make_unique<std::recursive_mutex>();
     m_timeval = rhs.m_timeval;
   }
 
@@ -40,35 +39,43 @@ class DNBTimer {
     // Create a new mutex to make this timer thread safe as well if
     // the timer we are copying is thread safe
     if (rhs.IsThreadSafe())
-      m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
+      m_mutex_up = std::make_unique<std::recursive_mutex>();
     m_timeval = rhs.m_timeval;
     return *this;
   }
 
   ~DNBTimer() {}
 
-  bool IsThreadSafe() const { return m_mutexAP.get() != NULL; }
+  bool IsThreadSafe() const { return static_cast<bool>(m_mutex_up); }
   // Reset the time value to now
   void Reset() {
-    PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
+    auto guard = m_mutex_up
+                     ? std::unique_lock<std::recursive_mutex>()
+                     : std::unique_lock<std::recursive_mutex>(*m_mutex_up);
     gettimeofday(&m_timeval, NULL);
   }
   // Get the total microseconds since Jan 1, 1970
   uint64_t TotalMicroSeconds() const {
-    PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
+    auto guard = m_mutex_up
+                     ? std::unique_lock<std::recursive_mutex>()
+                     : std::unique_lock<std::recursive_mutex>(*m_mutex_up);
     return (uint64_t)(m_timeval.tv_sec) * 1000000ull +
            (uint64_t)m_timeval.tv_usec;
   }
 
   void GetTime(uint64_t &sec, uint32_t &usec) const {
-    PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
+    auto guard = m_mutex_up
+                     ? std::unique_lock<std::recursive_mutex>()
+                     : std::unique_lock<std::recursive_mutex>(*m_mutex_up);
     sec = m_timeval.tv_sec;
     usec = m_timeval.tv_usec;
   }
   // Return the number of microseconds elapsed between now and the
   // m_timeval
   uint64_t ElapsedMicroSeconds(bool update) {
-    PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
+    auto guard = m_mutex_up
+                     ? std::unique_lock<std::recursive_mutex>()
+                     : std::unique_lock<std::recursive_mutex>(*m_mutex_up);
     struct timeval now;
     gettimeofday(&now, NULL);
     uint64_t now_usec =
@@ -115,19 +122,16 @@ class DNBTimer {
     OffsetTimeOfDay(&now);
     if (now.tv_sec > ts.tv_sec)
       return true;
-    else if (now.tv_sec < ts.tv_sec)
+    if (now.tv_sec < ts.tv_sec)
       return false;
-    else {
-      if (now.tv_nsec > ts.tv_nsec)
-        return true;
-      else
-        return false;
-    }
+    if (now.tv_nsec > ts.tv_nsec)
+      return true;
+    return false;
   }
 
 protected:
   // Classes that inherit from DNBTimer can see and modify these
-  std::unique_ptr<PThreadMutex> m_mutexAP;
+  std::unique_ptr<std::recursive_mutex> m_mutex_up;
   struct timeval m_timeval;
 };
 

@JDevlieghere JDevlieghere merged commit ae71055 into llvm:main Apr 27, 2025
12 checks passed
@JDevlieghere JDevlieghere deleted the debugserver-DNBTimer branch April 27, 2025 20:07
jyli0116 pushed a commit to jyli0116/llvm-project that referenced this pull request Apr 28, 2025
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer away from that class in preparation for removing PThreadMutex.
JDevlieghere added a commit that referenced this pull request Apr 28, 2025
The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
)

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

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
)

The debugserver code predates modern C++, but with C++11 and later
there's no need to have something like PThreadMutex. This migrates
DNBTimer 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