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

This reverts commit ae71055e66.
This commit is contained in:
Jonas Devlieghere
2025-04-28 08:41:52 -07:00
parent c52fdbe697
commit 41ab76bf0a

View File

@@ -14,6 +14,7 @@
#define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBTIMER_H
#include "DNBDefs.h"
#include "PThreadMutex.h"
#include <cstdint>
#include <memory>
#include <mutex>
@@ -22,17 +23,17 @@
class DNBTimer {
public:
// Constructors and Destructors
DNBTimer(bool threadSafe) {
DNBTimer(bool threadSafe) : m_mutexAP() {
if (threadSafe)
m_mutex_up = std::make_unique<std::recursive_mutex>();
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
Reset();
}
DNBTimer(const DNBTimer &rhs) {
DNBTimer(const DNBTimer &rhs) : m_mutexAP() {
// 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_mutex_up = std::make_unique<std::recursive_mutex>();
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
m_timeval = rhs.m_timeval;
}
@@ -40,43 +41,35 @@ public:
// 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_mutex_up = std::make_unique<std::recursive_mutex>();
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
m_timeval = rhs.m_timeval;
return *this;
}
~DNBTimer() {}
bool IsThreadSafe() const { return static_cast<bool>(m_mutex_up); }
bool IsThreadSafe() const { return m_mutexAP.get() != NULL; }
// Reset the time value to now
void Reset() {
auto guard = m_mutex_up
? std::unique_lock<std::recursive_mutex>()
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
gettimeofday(&m_timeval, NULL);
}
// Get the total microseconds since Jan 1, 1970
uint64_t TotalMicroSeconds() const {
auto guard = m_mutex_up
? std::unique_lock<std::recursive_mutex>()
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
return (uint64_t)(m_timeval.tv_sec) * 1000000ull +
(uint64_t)m_timeval.tv_usec;
}
void GetTime(uint64_t &sec, uint32_t &usec) const {
auto guard = m_mutex_up
? std::unique_lock<std::recursive_mutex>()
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
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) {
auto guard = m_mutex_up
? std::unique_lock<std::recursive_mutex>()
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
struct timeval now;
gettimeofday(&now, NULL);
uint64_t now_usec =
@@ -123,16 +116,19 @@ public:
OffsetTimeOfDay(&now);
if (now.tv_sec > ts.tv_sec)
return true;
if (now.tv_sec < ts.tv_sec)
else if (now.tv_sec < ts.tv_sec)
return false;
if (now.tv_nsec > ts.tv_nsec)
return true;
return false;
else {
if (now.tv_nsec > ts.tv_nsec)
return true;
else
return false;
}
}
protected:
// Classes that inherit from DNBTimer can see and modify these
std::unique_ptr<std::recursive_mutex> m_mutex_up;
std::unique_ptr<PThreadMutex> m_mutexAP;
struct timeval m_timeval;
};