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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/timeutils.h ('k') | webrtc/base/timeutils_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/timeutils.cc
diff --git a/webrtc/base/timeutils.cc b/webrtc/base/timeutils.cc
index a9fe49d6075a2341a387247ccb8e941677ee3544..ecd0911a510dde654177bb368108d189ccc04645 100644
--- a/webrtc/base/timeutils.cc
+++ b/webrtc/base/timeutils.cc
@@ -30,8 +30,17 @@
namespace rtc {
+ClockInterface* g_clock = nullptr;
+
+void SetClock(ClockInterface* clock) {
+ g_clock = clock;
+}
+
uint64_t TimeNanos() {
- int64_t ticks = 0;
+ if (g_clock) {
+ return g_clock->TimeNanos();
+ }
+ int64_t ticks;
#if defined(WEBRTC_MAC)
static mach_timebase_info_data_t timebase;
if (timebase.denom == 0) {
@@ -45,8 +54,8 @@ uint64_t TimeNanos() {
ticks = mach_absolute_time() * timebase.numer / timebase.denom;
#elif defined(WEBRTC_POSIX)
struct timespec ts;
- // TODO: Do we need to handle the case when CLOCK_MONOTONIC
- // is not supported?
+ // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
+ // supported?
clock_gettime(CLOCK_MONOTONIC, &ts);
ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
static_cast<int64_t>(ts.tv_nsec);
@@ -58,8 +67,7 @@ uint64_t TimeNanos() {
// Atomically update the last gotten time
DWORD old = InterlockedExchange(last_timegettime_ptr, now);
if (now < old) {
- // If now is earlier than old, there may have been a race between
- // threads.
+ // If now is earlier than old, there may have been a race between threads.
// 0x0fffffff ~3.1 days, the code will not take that long to execute
// so it must have been a wrap around.
if (old > 0xf0000000 && now < 0x0fffffff) {
@@ -67,8 +75,8 @@ uint64_t TimeNanos() {
}
}
ticks = now + (num_wrap_timegettime << 32);
- // TODO: Calculate with nanosecond precision. Otherwise, we're just
- // wasting a multiply and divide when doing Time() on Windows.
+ // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
+ // just wasting a multiply and divide when doing Time() on Windows.
ticks = ticks * kNumNanosecsPerMillisec;
#else
#error Unsupported platform.
« 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