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