Implement the TickTime::Now for mac using mach_absolute_time which is consistent even the user changes the system time.

Review URL: https://webrtc-codereview.appspot.com/431004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1879 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
wu@webrtc.org 2012-03-12 19:42:22 +00:00
parent c197b12d21
commit 6e6ea04f9b

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
* *
* Use of this source code is governed by a BSD-style license * Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source * that can be found in the LICENSE file in the root of the source
@ -19,6 +19,9 @@
#include <mmsystem.h> #include <mmsystem.h>
#elif WEBRTC_LINUX #elif WEBRTC_LINUX
#include <ctime> #include <ctime>
#elif WEBRTC_MAC
#include <mach/mach_time.h>
#include <string.h>
#else #else
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
@ -133,6 +136,7 @@ inline TickTime TickTime::Now()
{ {
TickTime result; TickTime result;
#if _WIN32 #if _WIN32
// TODO(wu): Remove QueryPerformanceCounter implementation.
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
// QueryPerformanceCounter returns the value from the TSC which is // QueryPerformanceCounter returns the value from the TSC which is
// incremented at the CPU frequency. The algorithm used requires // incremented at the CPU frequency. The algorithm used requires
@ -164,12 +168,27 @@ inline TickTime TickTime::Now()
#endif #endif
#elif defined(WEBRTC_LINUX) #elif defined(WEBRTC_LINUX)
struct timespec ts; struct timespec ts;
// TODO(wu): Remove CLOCK_REALTIME implementation.
#ifdef WEBRTC_CLOCK_TYPE_REALTIME #ifdef WEBRTC_CLOCK_TYPE_REALTIME
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
#else #else
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
#endif #endif
result._ticks = 1000000000LL * static_cast<WebRtc_Word64>(ts.tv_sec) + static_cast<WebRtc_Word64>(ts.tv_nsec); result._ticks = 1000000000LL * static_cast<WebRtc_Word64>(ts.tv_sec) + static_cast<WebRtc_Word64>(ts.tv_nsec);
#elif defined(WEBRTC_MAC)
static mach_timebase_info_data_t timebase;
if (timebase.denom == 0) {
// Get the timebase if this is the first time we run.
// Recommended by Apple's QA1398.
kern_return_t retval = mach_timebase_info(&timebase);
if (retval != KERN_SUCCESS) {
// TODO(wu): Implement CHECK similar to chrome for all the platforms.
// Then replace this with a CHECK(retval == KERN_SUCCESS);
asm("int3");
}
}
// Use timebase to convert absolute time tick units into nanoseconds.
result._ticks = mach_absolute_time() * timebase.numer / timebase.denom;
#else #else
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
@ -189,7 +208,7 @@ inline WebRtc_Word64 TickTime::MillisecondTimestamp()
#else #else
return now._ticks; return now._ticks;
#endif #endif
#elif WEBRTC_LINUX #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
return now._ticks / 1000000LL; return now._ticks / 1000000LL;
#else #else
return now._ticks / 1000LL; return now._ticks / 1000LL;
@ -208,7 +227,7 @@ inline WebRtc_Word64 TickTime::MicrosecondTimestamp()
#else #else
return now._ticks *1000LL; return now._ticks *1000LL;
#endif #endif
#elif WEBRTC_LINUX #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
return now._ticks / 1000LL; return now._ticks / 1000LL;
#else #else
return now._ticks; return now._ticks;
@ -230,7 +249,7 @@ inline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms)
#else #else
return ms; return ms;
#endif #endif
#elif WEBRTC_LINUX #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
return ms * 1000000LL; return ms * 1000000LL;
#else #else
return ms * 1000LL; return ms * 1000LL;
@ -247,7 +266,7 @@ inline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks)
#else #else
return ticks; return ticks;
#endif #endif
#elif WEBRTC_LINUX #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
return ticks / 1000000LL; return ticks / 1000000LL;
#else #else
return ticks / 1000LL; return ticks / 1000LL;
@ -280,7 +299,7 @@ inline WebRtc_Word64 TickInterval::Milliseconds() const
// _interval is in ms // _interval is in ms
return _interval; return _interval;
#endif #endif
#elif WEBRTC_LINUX #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
// _interval is in ns // _interval is in ns
return _interval / 1000000; return _interval / 1000000;
#else #else
@ -300,7 +319,7 @@ inline WebRtc_Word64 TickInterval::Microseconds() const
// _interval is in ms // _interval is in ms
return _interval *1000LL; return _interval *1000LL;
#endif #endif
#elif WEBRTC_LINUX #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
// _interval is in ns // _interval is in ns
return _interval / 1000; return _interval / 1000;
#else #else