summaryrefslogtreecommitdiffstats
path: root/base/time_win.cc
diff options
context:
space:
mode:
authordeanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-11 14:06:48 +0000
committerdeanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-11 14:06:48 +0000
commit7903e0262e2dda44eaa186d126cf88e2cc1470eb (patch)
treea44957a8d1a49f0c590f797c0b36de8928bd5646 /base/time_win.cc
parent32346a04167c5abcc16fd92bea50ec232b68e87d (diff)
downloadchromium_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/time_win.cc')
-rw-r--r--base/time_win.cc49
1 files changed, 45 insertions, 4 deletions
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