Reformatted tick_util.

BUG=
TEST=Trybots.

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3330 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
phoglund@webrtc.org 2013-01-03 09:50:17 +00:00
parent daabfd25a6
commit 5c8d9d30e2
2 changed files with 237 additions and 256 deletions

View File

@ -15,6 +15,7 @@
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
#if _WIN32 #if _WIN32
// Note: These includes must be in this order since mmsystem depends on windows.
#include <windows.h> #include <windows.h>
#include <mmsystem.h> #include <mmsystem.h>
#elif WEBRTC_LINUX #elif WEBRTC_LINUX
@ -27,14 +28,14 @@
#include <time.h> #include <time.h>
#endif #endif
#include "typedefs.h" #include "webrtc/typedefs.h"
namespace webrtc { namespace webrtc {
class TickInterval; class TickInterval;
// Class representing the current time. This class is immutable. // Class representing the current time.
class TickTime class TickTime {
{
public: public:
TickTime(); TickTime();
explicit TickTime(WebRtc_Word64 ticks); explicit TickTime(WebRtc_Word64 ticks);
@ -55,11 +56,11 @@ public:
static WebRtc_Word64 TicksToMilliseconds(const WebRtc_Word64 ticks); static WebRtc_Word64 TicksToMilliseconds(const WebRtc_Word64 ticks);
// Returns a TickTime that is ticks later than the passed TickTime // Returns a TickTime that is ticks later than the passed TickTime.
friend TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks); friend TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks);
TickTime& operator+=(const WebRtc_Word64& ticks); TickTime& operator+=(const WebRtc_Word64& ticks);
// Returns a TickInterval that is the difference in ticks beween rhs and lhs // Returns a TickInterval that is the difference in ticks beween rhs and lhs.
friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs); friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs);
// Call to engage the fake clock. This is useful for tests since relying on // Call to engage the fake clock. This is useful for tests since relying on
@ -72,27 +73,26 @@ public:
private: private:
static WebRtc_Word64 QueryOsForTicks(); static WebRtc_Word64 QueryOsForTicks();
static bool _use_fake_clock; static bool use_fake_clock_;
static WebRtc_Word64 _fake_ticks; static WebRtc_Word64 fake_ticks_;
WebRtc_Word64 _ticks; WebRtc_Word64 ticks_;
}; };
// Reperesents a time delta in ticks. This class is immutable. // Represents a time delta in ticks.
class TickInterval class TickInterval {
{
public: public:
TickInterval(); TickInterval();
WebRtc_Word64 Milliseconds() const; WebRtc_Word64 Milliseconds() const;
WebRtc_Word64 Microseconds() const; WebRtc_Word64 Microseconds() const;
// Returns the sum of two TickIntervals as a TickInterval // Returns the sum of two TickIntervals as a TickInterval.
friend TickInterval operator+(const TickInterval& lhs, friend TickInterval operator+(const TickInterval& lhs,
const TickInterval& rhs); const TickInterval& rhs);
TickInterval& operator+=(const TickInterval& rhs); TickInterval& operator+=(const TickInterval& rhs);
// Returns a TickInterval corresponding to rhs - lhs // Returns a TickInterval corresponding to rhs - lhs.
friend TickInterval operator-(const TickInterval& lhs, friend TickInterval operator-(const TickInterval& lhs,
const TickInterval& rhs); const TickInterval& rhs);
TickInterval& operator-=(const TickInterval& rhs); TickInterval& operator-=(const TickInterval& rhs);
@ -103,69 +103,62 @@ public:
friend bool operator>=(const TickInterval& lhs, const TickInterval& rhs); friend bool operator>=(const TickInterval& lhs, const TickInterval& rhs);
private: private:
TickInterval(WebRtc_Word64 interval); explicit TickInterval(WebRtc_Word64 interval);
friend class TickTime; friend class TickTime;
friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs); friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs);
private: private:
WebRtc_Word64 _interval; WebRtc_Word64 interval_;
}; };
inline TickInterval operator+(const TickInterval& lhs, const TickInterval& rhs) inline TickInterval operator+(const TickInterval& lhs,
{ const TickInterval& rhs) {
return TickInterval(lhs._interval + rhs._interval); return TickInterval(lhs.interval_ + rhs.interval_);
} }
inline TickInterval operator-(const TickInterval& lhs, const TickInterval& rhs) inline TickInterval operator-(const TickInterval& lhs,
{ const TickInterval& rhs) {
return TickInterval(lhs._interval - rhs._interval); return TickInterval(lhs.interval_ - rhs.interval_);
} }
inline TickInterval operator-(const TickTime& lhs,const TickTime& rhs) inline TickInterval operator-(const TickTime& lhs, const TickTime& rhs) {
{ return TickInterval(lhs.ticks_ - rhs.ticks_);
return TickInterval(lhs._ticks - rhs._ticks);
} }
inline TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks) inline TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks) {
{
TickTime time = lhs; TickTime time = lhs;
time._ticks += ticks; time.ticks_ += ticks;
return time; return time;
} }
inline bool operator>(const TickInterval& lhs, const TickInterval& rhs) inline bool operator>(const TickInterval& lhs, const TickInterval& rhs) {
{ return lhs.interval_ > rhs.interval_;
return lhs._interval > rhs._interval;
} }
inline bool operator<=(const TickInterval& lhs, const TickInterval& rhs) inline bool operator<=(const TickInterval& lhs, const TickInterval& rhs) {
{ return lhs.interval_ <= rhs.interval_;
return lhs._interval <= rhs._interval;
} }
inline bool operator<(const TickInterval& lhs, const TickInterval& rhs) inline bool operator<(const TickInterval& lhs, const TickInterval& rhs) {
{ return lhs.interval_ <= rhs.interval_;
return lhs._interval <= rhs._interval;
} }
inline bool operator>=(const TickInterval& lhs, const TickInterval& rhs) inline bool operator>=(const TickInterval& lhs, const TickInterval& rhs) {
{ return lhs.interval_ >= rhs.interval_;
return lhs._interval >= rhs._interval;
} }
inline TickTime::TickTime() inline TickTime::TickTime()
: _ticks(0) { : ticks_(0) {
} }
inline TickTime::TickTime(WebRtc_Word64 ticks) inline TickTime::TickTime(WebRtc_Word64 ticks)
: _ticks(ticks) { : ticks_(ticks) {
} }
inline TickTime TickTime::Now() inline TickTime TickTime::Now() {
{ if (use_fake_clock_)
if (_use_fake_clock) return TickTime(fake_ticks_);
return TickTime(_fake_ticks);
else else
return TickTime(QueryOsForTicks()); return TickTime(QueryOsForTicks());
} }
@ -182,26 +175,24 @@ inline WebRtc_Word64 TickTime::QueryOsForTicks() {
// incorrect time estimations. // incorrect time estimations.
LARGE_INTEGER qpcnt; LARGE_INTEGER qpcnt;
QueryPerformanceCounter(&qpcnt); QueryPerformanceCounter(&qpcnt);
result._ticks = qpcnt.QuadPart; result.ticks_ = qpcnt.QuadPart;
#else #else
static volatile LONG lastTimeGetTime = 0; static volatile LONG last_time_get_time = 0;
static volatile WebRtc_Word64 numWrapTimeGetTime = 0; static volatile WebRtc_Word64 num_wrap_time_get_time = 0;
volatile LONG* lastTimeGetTimePtr = &lastTimeGetTime; volatile LONG* last_time_get_time_ptr = &last_time_get_time;
DWORD now = timeGetTime(); DWORD now = timeGetTime();
// Atomically update the last gotten time // Atomically update the last gotten time
DWORD old = InterlockedExchange(lastTimeGetTimePtr, now); DWORD old = InterlockedExchange(last_time_get_time_ptr, now);
if(now < old) if (now < old) {
{
// If now is earlier than old, there may have been a race between // If now is earlier than old, there may have been a race between
// threads. // threads.
// 0x0fffffff ~3.1 days, the code will not take that long to execute // 0x0fffffff ~3.1 days, the code will not take that long to execute
// so it must have been a wrap around. // so it must have been a wrap around.
if(old > 0xf0000000 && now < 0x0fffffff) if (old > 0xf0000000 && now < 0x0fffffff) {
{ num_wrap_time_get_time++;
numWrapTimeGetTime++;
} }
} }
result._ticks = now + (numWrapTimeGetTime<<32); result.ticks_ = now + (num_wrap_time_get_time << 32);
#endif #endif
#elif defined(WEBRTC_LINUX) #elif defined(WEBRTC_LINUX)
struct timespec ts; struct timespec ts;
@ -211,7 +202,8 @@ inline WebRtc_Word64 TickTime::QueryOsForTicks() {
#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) #elif defined(WEBRTC_MAC)
static mach_timebase_info_data_t timebase; static mach_timebase_info_data_t timebase;
if (timebase.denom == 0) { if (timebase.denom == 0) {
@ -225,21 +217,21 @@ inline WebRtc_Word64 TickTime::QueryOsForTicks() {
asm("int3"); asm("int3");
#else #else
__builtin_trap(); __builtin_trap();
#endif // WEBRTC_IOS #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
} }
} }
// Use timebase to convert absolute time tick units into nanoseconds. // Use timebase to convert absolute time tick units into nanoseconds.
result._ticks = mach_absolute_time() * timebase.numer / timebase.denom; result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom;
#else #else
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
result._ticks = 1000000LL * static_cast<WebRtc_Word64>(tv.tv_sec) + static_cast<WebRtc_Word64>(tv.tv_usec); result.ticks_ = 1000000LL * static_cast<WebRtc_Word64>(tv.tv_sec) +
static_cast<WebRtc_Word64>(tv.tv_usec);
#endif #endif
return result._ticks; return result.ticks_;
} }
inline WebRtc_Word64 TickTime::MillisecondTimestamp() inline WebRtc_Word64 TickTime::MillisecondTimestamp() {
{
WebRtc_Word64 ticks = TickTime::Now().Ticks(); WebRtc_Word64 ticks = TickTime::Now().Ticks();
#if _WIN32 #if _WIN32
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
@ -256,8 +248,7 @@ inline WebRtc_Word64 TickTime::MillisecondTimestamp()
#endif #endif
} }
inline WebRtc_Word64 TickTime::MicrosecondTimestamp() inline WebRtc_Word64 TickTime::MicrosecondTimestamp() {
{
WebRtc_Word64 ticks = TickTime::Now().Ticks(); WebRtc_Word64 ticks = TickTime::Now().Ticks();
#if _WIN32 #if _WIN32
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
@ -274,13 +265,11 @@ inline WebRtc_Word64 TickTime::MicrosecondTimestamp()
#endif #endif
} }
inline WebRtc_Word64 TickTime::Ticks() const inline WebRtc_Word64 TickTime::Ticks() const {
{ return ticks_;
return _ticks;
} }
inline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms) inline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms) {
{
#if _WIN32 #if _WIN32
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
LARGE_INTEGER qpfreq; LARGE_INTEGER qpfreq;
@ -296,8 +285,7 @@ inline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms)
#endif #endif
} }
inline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks) inline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks) {
{
#if _WIN32 #if _WIN32
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
LARGE_INTEGER qpfreq; LARGE_INTEGER qpfreq;
@ -313,70 +301,63 @@ inline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks)
#endif #endif
} }
inline TickTime& TickTime::operator+=(const WebRtc_Word64& ticks) inline TickTime& TickTime::operator+=(const WebRtc_Word64& ticks) {
{ ticks_ += ticks;
_ticks += ticks;
return *this; return *this;
} }
inline TickInterval::TickInterval() : _interval(0) inline TickInterval::TickInterval() : interval_(0) {
{
} }
inline TickInterval::TickInterval(const WebRtc_Word64 interval) inline TickInterval::TickInterval(const WebRtc_Word64 interval)
: _interval(interval) : interval_(interval) {
{
} }
inline WebRtc_Word64 TickInterval::Milliseconds() const inline WebRtc_Word64 TickInterval::Milliseconds() const {
{
#if _WIN32 #if _WIN32
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
LARGE_INTEGER qpfreq; LARGE_INTEGER qpfreq;
QueryPerformanceFrequency(&qpfreq); QueryPerformanceFrequency(&qpfreq);
return (_interval * 1000) / qpfreq.QuadPart; return (interval_ * 1000) / qpfreq.QuadPart;
#else #else
// _interval is in ms // interval_ is in ms
return _interval; return interval_;
#endif #endif
#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
// _interval is in ns // interval_ is in ns
return _interval / 1000000; return interval_ / 1000000;
#else #else
// _interval is usecs // interval_ is usecs
return _interval / 1000; return interval_ / 1000;
#endif #endif
} }
inline WebRtc_Word64 TickInterval::Microseconds() const inline WebRtc_Word64 TickInterval::Microseconds() const {
{
#if _WIN32 #if _WIN32
#ifdef USE_QUERY_PERFORMANCE_COUNTER #ifdef USE_QUERY_PERFORMANCE_COUNTER
LARGE_INTEGER qpfreq; LARGE_INTEGER qpfreq;
QueryPerformanceFrequency(&qpfreq); QueryPerformanceFrequency(&qpfreq);
return (_interval * 1000000) / qpfreq.QuadPart; return (interval_ * 1000000) / qpfreq.QuadPart;
#else #else
// _interval is in ms // interval_ is in ms
return _interval *1000LL; return interval_ * 1000LL;
#endif #endif
#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
// _interval is in ns // interval_ is in ns
return _interval / 1000; return interval_ / 1000;
#else #else
// _interval is usecs // interval_ is usecs
return _interval; return interval_;
#endif #endif
} }
inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) {
{ interval_ += rhs.interval_;
_interval += rhs._interval;
return *this; return *this;
} }
inline TickInterval& TickInterval::operator-=(const TickInterval& rhs) inline TickInterval& TickInterval::operator-=(const TickInterval& rhs) {
{ interval_ -= rhs.interval_;
_interval -= rhs._interval;
return *this; return *this;
} }

View File

@ -8,23 +8,23 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include "system_wrappers/interface/tick_util.h" #include "webrtc/system_wrappers/interface/tick_util.h"
#include <cassert> #include <cassert>
namespace webrtc { namespace webrtc {
bool TickTime::_use_fake_clock = false; bool TickTime::use_fake_clock_ = false;
WebRtc_Word64 TickTime::_fake_ticks = 0; WebRtc_Word64 TickTime::fake_ticks_ = 0;
void TickTime::UseFakeClock(WebRtc_Word64 start_millisecond) { void TickTime::UseFakeClock(WebRtc_Word64 start_millisecond) {
_use_fake_clock = true; use_fake_clock_ = true;
_fake_ticks = MillisecondsToTicks(start_millisecond); fake_ticks_ = MillisecondsToTicks(start_millisecond);
} }
void TickTime::AdvanceFakeClock(WebRtc_Word64 milliseconds) { void TickTime::AdvanceFakeClock(WebRtc_Word64 milliseconds) {
assert(_use_fake_clock); assert(use_fake_clock_);
_fake_ticks += MillisecondsToTicks(milliseconds); fake_ticks_ += MillisecondsToTicks(milliseconds);
} }
} // namespace webrtc } // namespace webrtc