Skip to content

Commit 9f2bcc7

Browse files
committed
[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.
1 parent 044ff78 commit 9f2bcc7

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lldb/tools/debugserver/source/DNBTimer.h

+17-12
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,65 @@
1616
#include "DNBDefs.h"
1717
#include "PThreadMutex.h"
1818
#include <cstdint>
19-
#include <memory>
2019
#include <mutex>
2120
#include <sys/time.h>
2221

2322
class DNBTimer {
2423
public:
2524
// Constructors and Destructors
26-
DNBTimer(bool threadSafe) : m_mutexAP() {
25+
DNBTimer(bool threadSafe) {
2726
if (threadSafe)
28-
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
27+
m_mutex.emplace();
2928
Reset();
3029
}
3130

32-
DNBTimer(const DNBTimer &rhs) : m_mutexAP() {
31+
DNBTimer(const DNBTimer &rhs) {
3332
// Create a new mutex to make this timer thread safe as well if
3433
// the timer we are copying is thread safe
3534
if (rhs.IsThreadSafe())
36-
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
35+
m_mutex.emplace();
3736
m_timeval = rhs.m_timeval;
3837
}
3938

4039
DNBTimer &operator=(const DNBTimer &rhs) {
4140
// Create a new mutex to make this timer thread safe as well if
4241
// the timer we are copying is thread safe
4342
if (rhs.IsThreadSafe())
44-
m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
43+
m_mutex.emplace();
4544
m_timeval = rhs.m_timeval;
4645
return *this;
4746
}
4847

4948
~DNBTimer() {}
5049

51-
bool IsThreadSafe() const { return m_mutexAP.get() != NULL; }
50+
bool IsThreadSafe() const { return m_mutex.has_value(); }
5251
// Reset the time value to now
5352
void Reset() {
54-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
53+
auto lock = m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex)
54+
: std::unique_lock<std::recursive_mutex>();
5555
gettimeofday(&m_timeval, NULL);
5656
}
5757
// Get the total microseconds since Jan 1, 1970
5858
uint64_t TotalMicroSeconds() const {
59-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
59+
std::unique_lock<std::recursive_mutex> lock =
60+
m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex)
61+
: std::unique_lock<std::recursive_mutex>();
6062
return (uint64_t)(m_timeval.tv_sec) * 1000000ull +
6163
(uint64_t)m_timeval.tv_usec;
6264
}
6365

6466
void GetTime(uint64_t &sec, uint32_t &usec) const {
65-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
67+
auto lock = m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex)
68+
: std::unique_lock<std::recursive_mutex>();
6669
sec = m_timeval.tv_sec;
6770
usec = m_timeval.tv_usec;
6871
}
6972
// Return the number of microseconds elapsed between now and the
7073
// m_timeval
7174
uint64_t ElapsedMicroSeconds(bool update) {
72-
PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
75+
std::unique_lock<std::recursive_mutex> lock =
76+
m_mutex ? std::unique_lock<std::recursive_mutex>(*m_mutex)
77+
: std::unique_lock<std::recursive_mutex>();
7378
struct timeval now;
7479
gettimeofday(&now, NULL);
7580
uint64_t now_usec =
@@ -128,7 +133,7 @@ class DNBTimer {
128133

129134
protected:
130135
// Classes that inherit from DNBTimer can see and modify these
131-
std::unique_ptr<PThreadMutex> m_mutexAP;
136+
mutable std::optional<std::recursive_mutex> m_mutex;
132137
struct timeval m_timeval;
133138
};
134139

0 commit comments

Comments
 (0)