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