Author: Jonas Devlieghere Date: 2025-04-28T09:44:14-07:00 New Revision: 9f2bcc7a663dc36c3cd0b3d6c3616d9661a03c0f
URL: https://github.com/llvm/llvm-project/commit/9f2bcc7a663dc36c3cd0b3d6c3616d9661a03c0f DIFF: https://github.com/llvm/llvm-project/commit/9f2bcc7a663dc36c3cd0b3d6c3616d9661a03c0f.diff LOG: [debugserver] Migrate DNBTimer away from PThreadMutex (NFC) (#137540) 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. Added: Modified: lldb/tools/debugserver/source/DNBTimer.h Removed: ################################################################################ diff --git a/lldb/tools/debugserver/source/DNBTimer.h b/lldb/tools/debugserver/source/DNBTimer.h index 958c24140916c..2f3bad99cfbea 100644 --- a/lldb/tools/debugserver/source/DNBTimer.h +++ b/lldb/tools/debugserver/source/DNBTimer.h @@ -16,24 +16,23 @@ #include "DNBDefs.h" #include "PThreadMutex.h" #include <cstdint> -#include <memory> #include <mutex> #include <sys/time.h> 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.emplace(); 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.emplace(); m_timeval = rhs.m_timeval; } @@ -41,35 +40,41 @@ 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.emplace(); m_timeval = rhs.m_timeval; return *this; } ~DNBTimer() {} - bool IsThreadSafe() const { return m_mutexAP.get() != NULL; } + bool IsThreadSafe() const { return m_mutex.has_value(); } // Reset the time value to now void Reset() { - PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); + auto lock = m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex) + : std::unique_lock<std::recursive_mutex>(); gettimeofday(&m_timeval, NULL); } // Get the total microseconds since Jan 1, 1970 uint64_t TotalMicroSeconds() const { - PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); + std::unique_lock<std::recursive_mutex> lock = + m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex) + : std::unique_lock<std::recursive_mutex>(); 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 lock = m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex) + : std::unique_lock<std::recursive_mutex>(); 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()); + std::unique_lock<std::recursive_mutex> lock = + m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex) + : std::unique_lock<std::recursive_mutex>(); struct timeval now; gettimeofday(&now, NULL); uint64_t now_usec = @@ -128,7 +133,7 @@ class DNBTimer { protected: // Classes that inherit from DNBTimer can see and modify these - std::unique_ptr<PThreadMutex> m_mutexAP; + mutable std::optional<std::recursive_mutex> m_mutex; struct timeval m_timeval; }; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits