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 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 18 | // Note: These includes must be in this order since mmsystem depends on windows. |
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 |
| 22 | #include <ctime> |
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(); |
| 41 | explicit TickTime(WebRtc_Word64 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. |
| 47 | static WebRtc_Word64 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. |
| 50 | static WebRtc_Word64 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. |
| 53 | WebRtc_Word64 Ticks() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 55 | static WebRtc_Word64 MillisecondsToTicks(const WebRtc_Word64 ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 57 | static WebRtc_Word64 TicksToMilliseconds(const WebRtc_Word64 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. |
| 60 | friend TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks); |
| 61 | TickTime& operator+=(const WebRtc_Word64& 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. |
| 68 | static void UseFakeClock(WebRtc_Word64 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. |
| 71 | static void AdvanceFakeClock(WebRtc_Word64 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: |
| 74 | static WebRtc_Word64 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_; |
| 77 | static WebRtc_Word64 fake_ticks_; |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 78 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 79 | WebRtc_Word64 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 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 87 | WebRtc_Word64 Milliseconds() const; |
| 88 | WebRtc_Word64 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: |
| 106 | explicit TickInterval(WebRtc_Word64 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: |
| 112 | WebRtc_Word64 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 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 129 | inline TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks) { |
| 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 | |
| 155 | inline TickTime::TickTime(WebRtc_Word64 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 | |
| 166 | inline WebRtc_Word64 TickTime::QueryOsForTicks() { |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 167 | TickTime result; |
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 | // TODO(wu): Remove QueryPerformanceCounter implementation. |
| 170 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 171 | // QueryPerformanceCounter returns the value from the TSC which is |
| 172 | // incremented at the CPU frequency. The algorithm used requires |
| 173 | // the CPU frequency to be constant. Technology like speed stepping |
| 174 | // which has variable CPU frequency will therefore yield unpredictable, |
| 175 | // incorrect time estimations. |
| 176 | LARGE_INTEGER qpcnt; |
| 177 | QueryPerformanceCounter(&qpcnt); |
| 178 | result.ticks_ = qpcnt.QuadPart; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 179 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 180 | static volatile LONG last_time_get_time = 0; |
| 181 | static volatile WebRtc_Word64 num_wrap_time_get_time = 0; |
| 182 | volatile LONG* last_time_get_time_ptr = &last_time_get_time; |
| 183 | DWORD now = timeGetTime(); |
| 184 | // Atomically update the last gotten time |
| 185 | DWORD old = InterlockedExchange(last_time_get_time_ptr, now); |
| 186 | if (now < old) { |
| 187 | // If now is earlier than old, there may have been a race between |
| 188 | // threads. |
| 189 | // 0x0fffffff ~3.1 days, the code will not take that long to execute |
| 190 | // so it must have been a wrap around. |
| 191 | if (old > 0xf0000000 && now < 0x0fffffff) { |
| 192 | num_wrap_time_get_time++; |
wu@webrtc.org | 6e6ea04 | 2012-03-12 19:42:22 +0000 | [diff] [blame] | 193 | } |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 194 | } |
| 195 | result.ticks_ = now + (num_wrap_time_get_time << 32); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | #endif |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 197 | #elif defined(WEBRTC_LINUX) |
| 198 | struct timespec ts; |
| 199 | // TODO(wu): Remove CLOCK_REALTIME implementation. |
| 200 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
| 201 | clock_gettime(CLOCK_REALTIME, &ts); |
| 202 | #else |
| 203 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 204 | #endif |
| 205 | result.ticks_ = 1000000000LL * static_cast<WebRtc_Word64>(ts.tv_sec) + |
| 206 | static_cast<WebRtc_Word64>(ts.tv_nsec); |
| 207 | #elif defined(WEBRTC_MAC) |
| 208 | static mach_timebase_info_data_t timebase; |
| 209 | if (timebase.denom == 0) { |
| 210 | // Get the timebase if this is the first time we run. |
| 211 | // Recommended by Apple's QA1398. |
| 212 | kern_return_t retval = mach_timebase_info(&timebase); |
| 213 | if (retval != KERN_SUCCESS) { |
| 214 | // TODO(wu): Implement CHECK similar to chrome for all the platforms. |
| 215 | // Then replace this with a CHECK(retval == KERN_SUCCESS); |
| 216 | #ifndef WEBRTC_IOS |
| 217 | asm("int3"); |
| 218 | #else |
| 219 | __builtin_trap(); |
| 220 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ |
| 221 | } |
| 222 | } |
| 223 | // Use timebase to convert absolute time tick units into nanoseconds. |
| 224 | result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom; |
| 225 | #else |
| 226 | struct timeval tv; |
| 227 | gettimeofday(&tv, NULL); |
| 228 | result.ticks_ = 1000000LL * static_cast<WebRtc_Word64>(tv.tv_sec) + |
| 229 | static_cast<WebRtc_Word64>(tv.tv_usec); |
| 230 | #endif |
| 231 | return result.ticks_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | } |
| 233 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 234 | inline WebRtc_Word64 TickTime::MillisecondTimestamp() { |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 235 | WebRtc_Word64 ticks = TickTime::Now().Ticks(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 237 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 238 | LARGE_INTEGER qpfreq; |
| 239 | QueryPerformanceFrequency(&qpfreq); |
| 240 | return (ticks * 1000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 242 | return ticks; |
| 243 | #endif |
| 244 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 245 | return ticks / 1000000LL; |
| 246 | #else |
| 247 | return ticks / 1000LL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | #endif |
| 249 | } |
| 250 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 251 | inline WebRtc_Word64 TickTime::MicrosecondTimestamp() { |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 252 | WebRtc_Word64 ticks = TickTime::Now().Ticks(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 254 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 255 | LARGE_INTEGER qpfreq; |
| 256 | QueryPerformanceFrequency(&qpfreq); |
| 257 | return (ticks * 1000) / (qpfreq.QuadPart / 1000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 258 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 259 | return ticks * 1000LL; |
| 260 | #endif |
| 261 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 262 | return ticks / 1000LL; |
| 263 | #else |
| 264 | return ticks; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | #endif |
| 266 | } |
| 267 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 268 | inline WebRtc_Word64 TickTime::Ticks() const { |
| 269 | return ticks_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | } |
| 271 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 272 | inline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 274 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 275 | LARGE_INTEGER qpfreq; |
| 276 | QueryPerformanceFrequency(&qpfreq); |
| 277 | return (qpfreq.QuadPart * ms) / 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 279 | return ms; |
| 280 | #endif |
| 281 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 282 | return ms * 1000000LL; |
| 283 | #else |
| 284 | return ms * 1000LL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 285 | #endif |
| 286 | } |
| 287 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 288 | inline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 290 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 291 | LARGE_INTEGER qpfreq; |
| 292 | QueryPerformanceFrequency(&qpfreq); |
| 293 | return (ticks * 1000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 295 | return ticks; |
| 296 | #endif |
| 297 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 298 | return ticks / 1000000LL; |
| 299 | #else |
| 300 | return ticks / 1000LL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | #endif |
| 302 | } |
| 303 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 304 | inline TickTime& TickTime::operator+=(const WebRtc_Word64& ticks) { |
| 305 | ticks_ += ticks; |
| 306 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | } |
| 308 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 309 | inline TickInterval::TickInterval() : interval_(0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | inline TickInterval::TickInterval(const WebRtc_Word64 interval) |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 313 | : interval_(interval) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | } |
| 315 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 316 | inline WebRtc_Word64 TickInterval::Milliseconds() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 318 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 319 | LARGE_INTEGER qpfreq; |
| 320 | QueryPerformanceFrequency(&qpfreq); |
| 321 | return (interval_ * 1000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 323 | // interval_ is in ms |
| 324 | return interval_; |
| 325 | #endif |
| 326 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 327 | // interval_ is in ns |
| 328 | return interval_ / 1000000; |
| 329 | #else |
| 330 | // interval_ is usecs |
| 331 | return interval_ / 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 332 | #endif |
| 333 | } |
| 334 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 335 | inline WebRtc_Word64 TickInterval::Microseconds() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 336 | #if _WIN32 |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 337 | #ifdef USE_QUERY_PERFORMANCE_COUNTER |
| 338 | LARGE_INTEGER qpfreq; |
| 339 | QueryPerformanceFrequency(&qpfreq); |
| 340 | return (interval_ * 1000000) / qpfreq.QuadPart; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | #else |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 342 | // interval_ is in ms |
| 343 | return interval_ * 1000LL; |
| 344 | #endif |
| 345 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 346 | // interval_ is in ns |
| 347 | return interval_ / 1000; |
| 348 | #else |
| 349 | // interval_ is usecs |
| 350 | return interval_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | #endif |
| 352 | } |
| 353 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 354 | inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) { |
| 355 | interval_ += rhs.interval_; |
| 356 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | } |
| 358 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 359 | inline TickInterval& TickInterval::operator-=(const TickInterval& rhs) { |
| 360 | interval_ -= rhs.interval_; |
| 361 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 362 | } |
phoglund@webrtc.org | 4cebe6c | 2012-11-07 13:37:19 +0000 | [diff] [blame] | 363 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 364 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | |
phoglund@webrtc.org | 5c8d9d3 | 2013-01-03 09:50:17 +0000 | [diff] [blame] | 366 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ |