diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-11 14:06:48 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-11 14:06:48 +0000 |
commit | 7903e0262e2dda44eaa186d126cf88e2cc1470eb (patch) | |
tree | a44957a8d1a49f0c590f797c0b36de8928bd5646 /base | |
parent | 32346a04167c5abcc16fd92bea50ec232b68e87d (diff) | |
download | chromium_src-7903e0262e2dda44eaa186d126cf88e2cc1470eb.zip chromium_src-7903e0262e2dda44eaa186d126cf88e2cc1470eb.tar.gz chromium_src-7903e0262e2dda44eaa186d126cf88e2cc1470eb.tar.bz2 |
Don't use the Windows high-resolution Time::Now hacks on Posix, the normal resultion from the time APIs there should be enough.
Review URL: http://codereview.chromium.org/2420
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2056 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/time.cc | 48 | ||||
-rw-r--r-- | base/time.h | 13 | ||||
-rw-r--r-- | base/time_posix.cc | 2 | ||||
-rw-r--r-- | base/time_win.cc | 49 |
4 files changed, 46 insertions, 66 deletions
diff --git a/base/time.cc b/base/time.cc index eeebdc1..38f732c 100644 --- a/base/time.cc +++ b/base/time.cc @@ -8,13 +8,6 @@ #include "base/logging.h" -namespace { - -// Time between resampling the un-granular clock for this API. 60 seconds. -const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond; - -} // namespace - // TimeDelta ------------------------------------------------------------------ // static @@ -81,47 +74,6 @@ int64 TimeDelta::InMicroseconds() const { // Time ----------------------------------------------------------------------- -int64 Time::initial_time_ = 0; -TimeTicks Time::initial_ticks_; - -// static -void Time::InitializeClock() -{ - initial_ticks_ = TimeTicks::Now(); - initial_time_ = CurrentWallclockMicroseconds(); -} - -// static -Time Time::Now() { - if (initial_time_ == 0) - InitializeClock(); - - // We implement time using the high-resolution timers so that we can get - // timeouts which are smaller than 10-15ms. If we just used - // CurrentWallclockMicroseconds(), we'd have the less-granular timer. - // - // To make this work, we initialize the clock (initial_time) and the - // counter (initial_ctr). To compute the initial time, we can check - // the number of ticks that have elapsed, and compute the delta. - // - // To avoid any drift, we periodically resync the counters to the system - // clock. - while(true) { - TimeTicks ticks = TimeTicks::Now(); - - // Calculate the time elapsed since we started our timer - TimeDelta elapsed = ticks - initial_ticks_; - - // Check if enough time has elapsed that we need to resync the clock. - if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) { - InitializeClock(); - continue; - } - - return elapsed + initial_time_; - } -} - // static Time Time::FromTimeT(time_t tt) { if (tt == 0) diff --git a/base/time.h b/base/time.h index 427289d..addb246 100644 --- a/base/time.h +++ b/base/time.h @@ -317,12 +317,6 @@ class Time { private: friend class TimeDelta; - // Platform-dependent wall clock interface - static int64 CurrentWallclockMicroseconds(); - - // Initialize or resynchronize the clock. - static void InitializeClock(); - // Explodes the given time to either local time |is_local = true| or UTC // |is_local = false|. void Explode(bool is_local, Exploded* exploded) const; @@ -340,12 +334,6 @@ class Time { // Time in microseconds in UTC. int64 us_; - - // The initial time sampled via this API. - static int64 initial_time_; - - // The initial clock counter sampled via this API. - static TimeTicks initial_ticks_; }; inline Time TimeDelta::operator+(Time t) const { @@ -451,4 +439,3 @@ inline TimeTicks TimeDelta::operator+(TimeTicks t) const { } #endif // BASE_TIME_H_ - diff --git a/base/time_posix.cc b/base/time_posix.cc index 793687e..88c49a2 100644 --- a/base/time_posix.cc +++ b/base/time_posix.cc @@ -25,7 +25,7 @@ const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); // static -int64 Time::CurrentWallclockMicroseconds() { +Time Time::Now() { struct timeval tv; struct timezone tz = { 0, 0 }; // UTC if (gettimeofday(&tv, &tz) != 0) { diff --git a/base/time_win.cc b/base/time_win.cc index 77c18ae..32f8333 100644 --- a/base/time_win.cc +++ b/base/time_win.cc @@ -33,6 +33,23 @@ void MicrosecondsToFileTime(int64 us, FILETIME* ft) { *ft = bit_cast<FILETIME, int64>(us * 10); } +int64 CurrentWallclockMicroseconds() { + FILETIME ft; + ::GetSystemTimeAsFileTime(&ft); + return FileTimeToMicroseconds(ft); +} + +// Time between resampling the un-granular clock for this API. 60 seconds. +const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond; + +int64 initial_time = 0; +TimeTicks initial_ticks; + +void InitializeClock() { + initial_ticks = TimeTicks::Now(); + initial_time = CurrentWallclockMicroseconds(); +} + } // namespace // Time ----------------------------------------------------------------------- @@ -45,10 +62,34 @@ void MicrosecondsToFileTime(int64 us, FILETIME* ft) { const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000); // static -int64 Time::CurrentWallclockMicroseconds() { - FILETIME ft; - ::GetSystemTimeAsFileTime(&ft); - return FileTimeToMicroseconds(ft); +Time Time::Now() { + if (initial_time == 0) + InitializeClock(); + + // We implement time using the high-resolution timers so that we can get + // timeouts which are smaller than 10-15ms. If we just used + // CurrentWallclockMicroseconds(), we'd have the less-granular timer. + // + // To make this work, we initialize the clock (initial_time) and the + // counter (initial_ctr). To compute the initial time, we can check + // the number of ticks that have elapsed, and compute the delta. + // + // To avoid any drift, we periodically resync the counters to the system + // clock. + while(true) { + TimeTicks ticks = TimeTicks::Now(); + + // Calculate the time elapsed since we started our timer + TimeDelta elapsed = ticks - initial_ticks; + + // Check if enough time has elapsed that we need to resync the clock. + if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) { + InitializeClock(); + continue; + } + + return elapsed + initial_time; + } } // static |