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
Merged
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
42 changes: 23 additions & 19 deletions lldb/tools/debugserver/source/DNBTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,68 @@
#define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBTIMER_H

#include "DNBDefs.h"
#include "PThreadMutex.h"
#include <cstdint>
#include <memory>
#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_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;
}

DNBTimer &operator=(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;
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 =
Expand Down Expand Up @@ -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;
};

Expand Down
Loading