niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
wu@webrtc.org | 6e6ea04 | 2012-03-12 19:42:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // System independant wrapper for polling elapsed time in ms and us. |
| 12 | // The implementation works in the tick domain which can be mapped over to the |
| 13 | // time domain. |
| 14 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ |
| 15 | #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ |
| 16 | |
| 17 | #if _WIN32 |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 18 | // Note: The Windows header must always be included before mmsystem.h |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | #include <windows.h> |
| 20 | #include <mmsystem.h> |
| 21 | #elif WEBRTC_LINUX |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame^] | 22 | #include <time.h> |
wu@webrtc.org | 6e6ea04 | 2012-03-12 19:42:22 +0000 | [diff] [blame] | 23 | #elif WEBRTC_MAC |
| 24 | #include <mach/mach_time.h> |
| 25 | #include <string.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | #else |
| 27 | #include <sys/time.h> |
| 28 | #include <time.h> |
| 29 | #endif |
| 30 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 31 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 34 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | class TickInterval; |
| 36 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 37 | // Class representing the current time. |
| 38 | class TickTime { |
| 39 | public: |
| 40 | TickTime(); |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 41 | explicit TickTime(int64_t ticks); |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 42 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 43 | // Current time in the tick domain. |
| 44 | static TickTime Now(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 46 | // Now in the time domain in ms. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 47 | static int64_t MillisecondTimestamp(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 49 | // Now in the time domain in us. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 50 | static int64_t MicrosecondTimestamp(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 52 | // Returns the number of ticks in the tick domain. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 53 | int64_t Ticks() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 55 | static int64_t MillisecondsToTicks(const int64_t ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 57 | static int64_t TicksToMilliseconds(const int64_t ticks); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 59 | // Returns a TickTime that is ticks later than the passed TickTime. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 60 | friend TickTime operator+(const TickTime lhs, const int64_t ticks); |
| 61 | TickTime& operator+=(const int64_t& ticks); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 63 | // Returns a TickInterval that is the difference in ticks beween rhs and lhs. |
| 64 | friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs); |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 65 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 66 | // Call to engage the fake clock. This is useful for tests since relying on |
| 67 | // a real clock often makes the test flaky. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 68 | static void UseFakeClock(int64_t start_millisecond); |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 69 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 70 | // Advance the fake clock. Must be called after UseFakeClock. |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 71 | static void AdvanceFakeClock(int64_t milliseconds); |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 72 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 73 | private: |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 74 | static int64_t QueryOsForTicks(); |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 75 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 76 | static bool use_fake_clock_; |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 77 | static int64_t fake_ticks_; |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 78 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 79 | int64_t ticks_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 82 | // Represents a time delta in ticks. |
| 83 | class TickInterval { |
| 84 | public: |
| 85 | TickInterval(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 87 | int64_t Milliseconds() const; |
| 88 | int64_t Microseconds() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 90 | // Returns the sum of two TickIntervals as a TickInterval. |
| 91 | friend TickInterval operator+(const TickInterval& lhs, |
| 92 | const TickInterval& rhs); |
| 93 | TickInterval& operator+=(const TickInterval& rhs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 95 | // Returns a TickInterval corresponding to rhs - lhs. |
| 96 | friend TickInterval operator-(const TickInterval& lhs, |
| 97 | const TickInterval& rhs); |
| 98 | TickInterval& operator-=(const TickInterval& rhs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 100 | friend bool operator>(const TickInterval& lhs, const TickInterval& rhs); |
| 101 | friend bool operator<=(const TickInterval& lhs, const TickInterval& rhs); |
| 102 | friend bool operator<(const TickInterval& lhs, const TickInterval& rhs); |
| 103 | friend bool operator>=(const TickInterval& lhs, const TickInterval& rhs); |
kjellander@webrtc.org | 9e7774f | 2011-09-23 11:33:31 +0000 | [diff] [blame] | 104 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 105 | private: |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 106 | explicit TickInterval(int64_t interval); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 108 | friend class TickTime; |
| 109 | friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 111 | private: |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 112 | int64_t interval_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 115 | inline TickInterval operator+(const TickInterval& lhs, |
| 116 | const TickInterval& rhs) { |
| 117 | return TickInterval(lhs.interval_ + rhs.interval_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 120 | inline TickInterval operator-(const TickInterval& lhs, |
| 121 | const TickInterval& rhs) { |
| 122 | return TickInterval(lhs.interval_ - rhs.interval_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | } |
| 124 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 125 | inline TickInterval operator-(const TickTime& lhs, const TickTime& rhs) { |
| 126 | return TickInterval(lhs.ticks_ - rhs.ticks_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 129 | inline TickTime operator+(const TickTime lhs, const int64_t ticks) { |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 130 | TickTime time = lhs; |
| 131 | time.ticks_ += ticks; |
| 132 | return time; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 134 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 135 | inline bool operator>(const TickInterval& lhs, const TickInterval& rhs) { |
| 136 | return lhs.interval_ > rhs.interval_; |
kjellander@webrtc.org | 9e7774f | 2011-09-23 11:33:31 +0000 | [diff] [blame] | 137 | } |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 138 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 139 | inline bool operator<=(const TickInterval& lhs, const TickInterval& rhs) { |
| 140 | return lhs.interval_ <= rhs.interval_; |
kjellander@webrtc.org | 9e7774f | 2011-09-23 11:33:31 +0000 | [diff] [blame] | 141 | } |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 142 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 143 | inline bool operator<(const TickInterval& lhs, const TickInterval& rhs) { |
| 144 | return lhs.interval_ <= rhs.interval_; |
kjellander@webrtc.org | 9e7774f | 2011-09-23 11:33:31 +0000 | [diff] [blame] | 145 | } |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 146 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 147 | inline bool operator>=(const TickInterval& lhs, const TickInterval& rhs) { |
| 148 | return lhs.interval_ >= rhs.interval_; |
kjellander@webrtc.org | 9e7774f | 2011-09-23 11:33:31 +0000 | [diff] [blame] | 149 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 151 | inline TickTime::TickTime() |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 152 | : ticks_(0) { |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 155 | inline TickTime::TickTime(int64_t ticks) |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 156 | : ticks_(ticks) { |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 157 | } |
| 158 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 159 | inline TickTime TickTime::Now() { |
| 160 | if (use_fake_clock_) |
| 161 | return TickTime(fake_ticks_); |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 162 | else |
| 163 | return TickTime(QueryOsForTicks()); |
| 164 | } |
| 165 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 166 | inline int64_t TickTime::MillisecondTimestamp() { |
| 167 | int64_t ticks = TickTime::Now().Ticks(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 169 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 170 | LARGE_INTEGER qpfreq; |
| 171 | QueryPerformanceFrequency(&qpfreq); |
| 172 | return (ticks * 1000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 174 | return ticks; |
| 175 | #endif |
| 176 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 177 | return ticks / 1000000LL; |
| 178 | #else |
| 179 | return ticks / 1000LL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | #endif |
| 181 | } |
| 182 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 183 | inline int64_t TickTime::MicrosecondTimestamp() { |
| 184 | int64_t ticks = TickTime::Now().Ticks(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 186 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 187 | LARGE_INTEGER qpfreq; |
| 188 | QueryPerformanceFrequency(&qpfreq); |
| 189 | return (ticks * 1000) / (qpfreq.QuadPart / 1000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 191 | return ticks * 1000LL; |
| 192 | #endif |
| 193 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 194 | return ticks / 1000LL; |
| 195 | #else |
| 196 | return ticks; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | #endif |
| 198 | } |
| 199 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 200 | inline int64_t TickTime::Ticks() const { |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 201 | return ticks_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | } |
| 203 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 204 | inline int64_t TickTime::MillisecondsToTicks(const int64_t ms) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 206 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 207 | LARGE_INTEGER qpfreq; |
| 208 | QueryPerformanceFrequency(&qpfreq); |
| 209 | return (qpfreq.QuadPart * ms) / 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 211 | return ms; |
| 212 | #endif |
| 213 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 214 | return ms * 1000000LL; |
| 215 | #else |
| 216 | return ms * 1000LL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | #endif |
| 218 | } |
| 219 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 220 | inline int64_t TickTime::TicksToMilliseconds(const int64_t ticks) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 222 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 223 | LARGE_INTEGER qpfreq; |
| 224 | QueryPerformanceFrequency(&qpfreq); |
| 225 | return (ticks * 1000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 227 | return ticks; |
| 228 | #endif |
| 229 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 230 | return ticks / 1000000LL; |
| 231 | #else |
| 232 | return ticks / 1000LL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | #endif |
| 234 | } |
| 235 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 236 | inline TickTime& TickTime::operator+=(const int64_t& ticks) { |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 237 | ticks_ += ticks; |
| 238 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | } |
| 240 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 241 | inline TickInterval::TickInterval() : interval_(0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | } |
| 243 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 244 | inline TickInterval::TickInterval(const int64_t interval) |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 245 | : interval_(interval) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | } |
| 247 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 248 | inline int64_t TickInterval::Milliseconds() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 250 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 251 | LARGE_INTEGER qpfreq; |
| 252 | QueryPerformanceFrequency(&qpfreq); |
| 253 | return (interval_ * 1000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 255 | // interval_ is in ms |
| 256 | return interval_; |
| 257 | #endif |
| 258 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 259 | // interval_ is in ns |
| 260 | return interval_ / 1000000; |
| 261 | #else |
| 262 | // interval_ is usecs |
| 263 | return interval_ / 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | #endif |
| 265 | } |
| 266 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 267 | inline int64_t TickInterval::Microseconds() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 268 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 269 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 270 | LARGE_INTEGER qpfreq; |
| 271 | QueryPerformanceFrequency(&qpfreq); |
| 272 | return (interval_ * 1000000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 274 | // interval_ is in ms |
| 275 | return interval_ * 1000LL; |
| 276 | #endif |
| 277 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 278 | // interval_ is in ns |
| 279 | return interval_ / 1000; |
| 280 | #else |
| 281 | // interval_ is usecs |
| 282 | return interval_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | #endif |
| 284 | } |
| 285 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 286 | inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) { |
| 287 | interval_ += rhs.interval_; |
| 288 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 291 | inline TickInterval& TickInterval::operator-=(const TickInterval& rhs) { |
| 292 | interval_ -= rhs.interval_; |
| 293 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | } |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 295 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 296 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 298 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ |