Use rtc::time for all your timing needs!

Initial step of unifying so that base/timeutils.h and Clock/TimeTime
from system_wrappers use the same implementation.

BUG=webrtc:5463
R=pbos@webrtc.org, tommi@webrtc.org

Review URL: https://codereview.webrtc.org/1639543005 .

Cr-Commit-Position: refs/heads/master@{#11394}
This commit is contained in:
Erik Språng 2016-01-27 12:55:33 +01:00
parent d673b0fa5d
commit 1c3909899d
2 changed files with 8 additions and 94 deletions

View File

@ -74,6 +74,8 @@ uint64_t TimeNanos() {
// TODO: 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.
#endif
return ticks;
}

View File

@ -10,7 +10,7 @@
#include "webrtc/system_wrappers/include/tick_util.h"
#include <assert.h>
#include "webrtc/base/timeutils.h"
namespace webrtc {
@ -23,108 +23,20 @@ int64_t TickTime::MicrosecondTimestamp() {
}
int64_t TickTime::MillisecondsToTicks(const int64_t ms) {
#if _WIN32
return ms;
#elif defined(WEBRTC_LINUX)
return ms * 1000000LL;
#elif defined(WEBRTC_MAC)
// TODO(pbos): Fix unsafe use of static locals.
static double timebase_from_millisecond_fract = 0.0;
if (timebase_from_millisecond_fract == 0.0) {
mach_timebase_info_data_t timebase;
(void)mach_timebase_info(&timebase);
timebase_from_millisecond_fract = (timebase.denom * 1e6) / timebase.numer;
}
return ms * timebase_from_millisecond_fract;
#else
return ms * 1000LL;
#endif
return ms * rtc::kNumNanosecsPerMillisec;
}
int64_t TickTime::TicksToMilliseconds(const int64_t ticks) {
#if _WIN32
return ticks;
#elif defined(WEBRTC_LINUX)
return ticks / 1000000LL;
#elif defined(WEBRTC_MAC)
// TODO(pbos): Fix unsafe use of static locals.
static double timebase_microsecond_fract = 0.0;
if (timebase_microsecond_fract == 0.0) {
mach_timebase_info_data_t timebase;
(void)mach_timebase_info(&timebase);
timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e6);
}
return ticks * timebase_microsecond_fract;
#else
return ticks;
#endif
return ticks / rtc::kNumNanosecsPerMillisec;
}
int64_t TickTime::TicksToMicroseconds(const int64_t ticks) {
#if _WIN32
return ticks * 1000LL;
#elif defined(WEBRTC_LINUX)
return ticks / 1000LL;
#elif defined(WEBRTC_MAC)
// TODO(pbos): Fix unsafe use of static locals.
static double timebase_microsecond_fract = 0.0;
if (timebase_microsecond_fract == 0.0) {
mach_timebase_info_data_t timebase;
(void)mach_timebase_info(&timebase);
timebase_microsecond_fract = timebase.numer / (timebase.denom * 1e3);
}
return ticks * timebase_microsecond_fract;
#else
return ticks;
#endif
return ticks / rtc::kNumNanosecsPerMicrosec;
}
// Gets the native system tick count. The actual unit, resolution, and epoch
// varies by platform:
// Windows: Milliseconds of uptime with rollover count in the upper 32-bits.
// Linux/Android: Nanoseconds since the Unix epoch.
// Mach (Mac/iOS): "absolute" time since first call.
// Unknown POSIX: Microseconds since the Unix epoch.
// Gets the native system tick count, converted to nanoseconds.
int64_t TickTime::QueryOsForTicks() {
#if _WIN32
static volatile LONG last_time_get_time = 0;
static volatile int64_t num_wrap_time_get_time = 0;
volatile LONG* last_time_get_time_ptr = &last_time_get_time;
DWORD now = timeGetTime();
// Atomically update the last gotten time
DWORD old = InterlockedExchange(last_time_get_time_ptr, now);
if (now < old) {
// 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) {
// TODO(pbos): Fix unsafe use of static locals.
num_wrap_time_get_time++;
}
}
return now + (num_wrap_time_get_time << 32);
#elif defined(WEBRTC_LINUX)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return 1000000000LL * ts.tv_sec + ts.tv_nsec;
#elif defined(WEBRTC_MAC)
// Return absolute time as an offset from the first call to this function, so
// that we can do floating-point (double) operations on it without losing
// precision. This holds true until the elapsed time is ~11 days,
// at which point we'll start to lose some precision, though not enough to
// matter for millisecond accuracy for another couple years after that.
// TODO(pbos): Fix unsafe use of static locals.
static uint64_t timebase_start = 0;
if (timebase_start == 0) {
timebase_start = mach_absolute_time();
}
return mach_absolute_time() - timebase_start;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return 1000000LL * tv.tv_sec + tv.tv_usec;
#endif
return rtc::TimeNanos();
}
} // namespace webrtc