Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: webrtc/base/timeutils.cc

Issue 1895933003: Adding the ability to use a simulated clock for unit tests. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixing compile warning. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/timeutils.h ('k') | webrtc/base/timeutils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 12 matching lines...) Expand all
23 #endif 23 #endif
24 #include <windows.h> 24 #include <windows.h>
25 #include <mmsystem.h> 25 #include <mmsystem.h>
26 #endif 26 #endif
27 27
28 #include "webrtc/base/checks.h" 28 #include "webrtc/base/checks.h"
29 #include "webrtc/base/timeutils.h" 29 #include "webrtc/base/timeutils.h"
30 30
31 namespace rtc { 31 namespace rtc {
32 32
33 ClockInterface* g_clock = nullptr;
34
35 void SetClock(ClockInterface* clock) {
36 g_clock = clock;
37 }
38
33 uint64_t TimeNanos() { 39 uint64_t TimeNanos() {
34 int64_t ticks = 0; 40 if (g_clock) {
41 return g_clock->TimeNanos();
42 }
43 int64_t ticks;
35 #if defined(WEBRTC_MAC) 44 #if defined(WEBRTC_MAC)
36 static mach_timebase_info_data_t timebase; 45 static mach_timebase_info_data_t timebase;
37 if (timebase.denom == 0) { 46 if (timebase.denom == 0) {
38 // Get the timebase if this is the first time we run. 47 // Get the timebase if this is the first time we run.
39 // Recommended by Apple's QA1398. 48 // Recommended by Apple's QA1398.
40 if (mach_timebase_info(&timebase) != KERN_SUCCESS) { 49 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
41 RTC_DCHECK(false); 50 RTC_DCHECK(false);
42 } 51 }
43 } 52 }
44 // Use timebase to convert absolute time tick units into nanoseconds. 53 // Use timebase to convert absolute time tick units into nanoseconds.
45 ticks = mach_absolute_time() * timebase.numer / timebase.denom; 54 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
46 #elif defined(WEBRTC_POSIX) 55 #elif defined(WEBRTC_POSIX)
47 struct timespec ts; 56 struct timespec ts;
48 // TODO: Do we need to handle the case when CLOCK_MONOTONIC 57 // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
49 // is not supported? 58 // supported?
50 clock_gettime(CLOCK_MONOTONIC, &ts); 59 clock_gettime(CLOCK_MONOTONIC, &ts);
51 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + 60 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
52 static_cast<int64_t>(ts.tv_nsec); 61 static_cast<int64_t>(ts.tv_nsec);
53 #elif defined(WEBRTC_WIN) 62 #elif defined(WEBRTC_WIN)
54 static volatile LONG last_timegettime = 0; 63 static volatile LONG last_timegettime = 0;
55 static volatile int64_t num_wrap_timegettime = 0; 64 static volatile int64_t num_wrap_timegettime = 0;
56 volatile LONG* last_timegettime_ptr = &last_timegettime; 65 volatile LONG* last_timegettime_ptr = &last_timegettime;
57 DWORD now = timeGetTime(); 66 DWORD now = timeGetTime();
58 // Atomically update the last gotten time 67 // Atomically update the last gotten time
59 DWORD old = InterlockedExchange(last_timegettime_ptr, now); 68 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
60 if (now < old) { 69 if (now < old) {
61 // If now is earlier than old, there may have been a race between 70 // If now is earlier than old, there may have been a race between threads.
62 // threads.
63 // 0x0fffffff ~3.1 days, the code will not take that long to execute 71 // 0x0fffffff ~3.1 days, the code will not take that long to execute
64 // so it must have been a wrap around. 72 // so it must have been a wrap around.
65 if (old > 0xf0000000 && now < 0x0fffffff) { 73 if (old > 0xf0000000 && now < 0x0fffffff) {
66 num_wrap_timegettime++; 74 num_wrap_timegettime++;
67 } 75 }
68 } 76 }
69 ticks = now + (num_wrap_timegettime << 32); 77 ticks = now + (num_wrap_timegettime << 32);
70 // TODO: Calculate with nanosecond precision. Otherwise, we're just 78 // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
71 // wasting a multiply and divide when doing Time() on Windows. 79 // just wasting a multiply and divide when doing Time() on Windows.
72 ticks = ticks * kNumNanosecsPerMillisec; 80 ticks = ticks * kNumNanosecsPerMillisec;
73 #else 81 #else
74 #error Unsupported platform. 82 #error Unsupported platform.
75 #endif 83 #endif
76 return ticks; 84 return ticks;
77 } 85 }
78 86
79 uint32_t Time32() { 87 uint32_t Time32() {
80 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec); 88 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
81 } 89 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. 169 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
162 day -= 1; 170 day -= 1;
163 171
164 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| 172 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
165 // which was accumulated into |day| above). 173 // which was accumulated into |day| above).
166 return (((static_cast<int64_t> 174 return (((static_cast<int64_t>
167 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; 175 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec;
168 } 176 }
169 177
170 } // namespace rtc 178 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/timeutils.h ('k') | webrtc/base/timeutils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698