|
14 | 14 | #define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBTIMER_H |
15 | 15 |
|
16 | 16 | #include "DNBDefs.h" |
17 | | -#include "PThreadMutex.h" |
18 | 17 | #include <cstdint> |
19 | 18 | #include <memory> |
20 | 19 | #include <sys/time.h> |
21 | 20 |
|
22 | 21 | class DNBTimer { |
23 | 22 | public: |
24 | 23 | // Constructors and Destructors |
25 | | - DNBTimer(bool threadSafe) : m_mutexAP() { |
| 24 | + DNBTimer(bool threadSafe) { |
26 | 25 | if (threadSafe) |
27 | | - m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 26 | + m_mutex_up = std::make_unique<std::recursive_mutex>(); |
28 | 27 | Reset(); |
29 | 28 | } |
30 | 29 |
|
31 | | - DNBTimer(const DNBTimer &rhs) : m_mutexAP() { |
| 30 | + DNBTimer(const DNBTimer &rhs) { |
32 | 31 | // Create a new mutex to make this timer thread safe as well if |
33 | 32 | // the timer we are copying is thread safe |
34 | 33 | if (rhs.IsThreadSafe()) |
35 | | - m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 34 | + m_mutex_up = std::make_unique<std::recursive_mutex>(); |
36 | 35 | m_timeval = rhs.m_timeval; |
37 | 36 | } |
38 | 37 |
|
39 | 38 | DNBTimer &operator=(const DNBTimer &rhs) { |
40 | 39 | // Create a new mutex to make this timer thread safe as well if |
41 | 40 | // the timer we are copying is thread safe |
42 | 41 | if (rhs.IsThreadSafe()) |
43 | | - m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 42 | + m_mutex_up = std::make_unique<std::recursive_mutex>(); |
44 | 43 | m_timeval = rhs.m_timeval; |
45 | 44 | return *this; |
46 | 45 | } |
47 | 46 |
|
48 | 47 | ~DNBTimer() {} |
49 | 48 |
|
50 | | - bool IsThreadSafe() const { return m_mutexAP.get() != NULL; } |
| 49 | + bool IsThreadSafe() const { return static_cast<bool>(m_mutex_up); } |
51 | 50 | // Reset the time value to now |
52 | 51 | 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); |
54 | 55 | gettimeofday(&m_timeval, NULL); |
55 | 56 | } |
56 | 57 | // Get the total microseconds since Jan 1, 1970 |
57 | 58 | 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); |
59 | 62 | return (uint64_t)(m_timeval.tv_sec) * 1000000ull + |
60 | 63 | (uint64_t)m_timeval.tv_usec; |
61 | 64 | } |
62 | 65 |
|
63 | 66 | 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); |
65 | 70 | sec = m_timeval.tv_sec; |
66 | 71 | usec = m_timeval.tv_usec; |
67 | 72 | } |
68 | 73 | // Return the number of microseconds elapsed between now and the |
69 | 74 | // m_timeval |
70 | 75 | 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); |
72 | 79 | struct timeval now; |
73 | 80 | gettimeofday(&now, NULL); |
74 | 81 | uint64_t now_usec = |
@@ -115,19 +122,16 @@ class DNBTimer { |
115 | 122 | OffsetTimeOfDay(&now); |
116 | 123 | if (now.tv_sec > ts.tv_sec) |
117 | 124 | return true; |
118 | | - else if (now.tv_sec < ts.tv_sec) |
| 125 | + if (now.tv_sec < ts.tv_sec) |
119 | 126 | 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; |
126 | 130 | } |
127 | 131 |
|
128 | 132 | protected: |
129 | 133 | // 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; |
131 | 135 | struct timeval m_timeval; |
132 | 136 | }; |
133 | 137 |
|
|
0 commit comments