Skip to content

Commit d6ec83c

Browse files
JDevlieghereIanWood1
authored andcommitted
[debugserver] Migrate DNBTimer away from PThreadMutex (NFC) (llvm#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.
1 parent f1afab1 commit d6ec83c

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

lldb/tools/debugserver/source/DNBTimer.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,68 @@
1414
#define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBTIMER_H
1515

1616
#include "DNBDefs.h"
17-
#include "PThreadMutex.h"
1817
#include <cstdint>
1918
#include <memory>
2019
#include <sys/time.h>
2120

2221
class DNBTimer {
2322
public:
2423
// Constructors and Destructors
25-
DNBTimer(bool threadSafe) : m_mutexAP() {
24+
DNBTimer(bool threadSafe) {
2625
if (threadSafe)
27-
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
26+
m_mutex_up = std::make_unique<std::recursive_mutex>();
2827
Reset();
2928
}
3029

31-
DNBTimer(const DNBTimer &rhs) : m_mutexAP() {
30+
DNBTimer(const DNBTimer &rhs) {
3231
// Create a new mutex to make this timer thread safe as well if
3332
// the timer we are copying is thread safe
3433
if (rhs.IsThreadSafe())
35-
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
34+
m_mutex_up = std::make_unique<std::recursive_mutex>();
3635
m_timeval = rhs.m_timeval;
3736
}
3837

3938
DNBTimer &operator=(const DNBTimer &rhs) {
4039
// Create a new mutex to make this timer thread safe as well if
4140
// the timer we are copying is thread safe
4241
if (rhs.IsThreadSafe())
43-
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
42+
m_mutex_up = std::make_unique<std::recursive_mutex>();
4443
m_timeval = rhs.m_timeval;
4544
return *this;
4645
}
4746

4847
~DNBTimer() {}
4948

50-
bool IsThreadSafe() const { return m_mutexAP.get() != NULL; }
49+
bool IsThreadSafe() const { return static_cast<bool>(m_mutex_up); }
5150
// Reset the time value to now
5251
void Reset() {
53-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
52+
auto guard = m_mutex_up
53+
? std::unique_lock<std::recursive_mutex>()
54+
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
5455
gettimeofday(&m_timeval, NULL);
5556
}
5657
// Get the total microseconds since Jan 1, 1970
5758
uint64_t TotalMicroSeconds() const {
58-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
59+
auto guard = m_mutex_up
60+
? std::unique_lock<std::recursive_mutex>()
61+
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
5962
return (uint64_t)(m_timeval.tv_sec) * 1000000ull +
6063
(uint64_t)m_timeval.tv_usec;
6164
}
6265

6366
void GetTime(uint64_t &sec, uint32_t &usec) const {
64-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
67+
auto guard = m_mutex_up
68+
? std::unique_lock<std::recursive_mutex>()
69+
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
6570
sec = m_timeval.tv_sec;
6671
usec = m_timeval.tv_usec;
6772
}
6873
// Return the number of microseconds elapsed between now and the
6974
// m_timeval
7075
uint64_t ElapsedMicroSeconds(bool update) {
71-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
76+
auto guard = m_mutex_up
77+
? std::unique_lock<std::recursive_mutex>()
78+
: std::unique_lock<std::recursive_mutex>(*m_mutex_up);
7279
struct timeval now;
7380
gettimeofday(&now, NULL);
7481
uint64_t now_usec =
@@ -115,19 +122,16 @@ class DNBTimer {
115122
OffsetTimeOfDay(&now);
116123
if (now.tv_sec > ts.tv_sec)
117124
return true;
118-
else if (now.tv_sec < ts.tv_sec)
125+
if (now.tv_sec < ts.tv_sec)
119126
return false;
120-
else {
121-
if (now.tv_nsec > ts.tv_nsec)
122-
return true;
123-
else
124-
return false;
125-
}
127+
if (now.tv_nsec > ts.tv_nsec)
128+
return true;
129+
return false;
126130
}
127131

128132
protected:
129133
// Classes that inherit from DNBTimer can see and modify these
130-
std::unique_ptr<PThreadMutex> m_mutexAP;
134+
std::unique_ptr<std::recursive_mutex> m_mutex_up;
131135
struct timeval m_timeval;
132136
};
133137

0 commit comments

Comments
 (0)