Chromium Code Reviews| Index: base/time/time_win.cc |
| diff --git a/base/time/time_win.cc b/base/time/time_win.cc |
| index 5fa899d1f97f69be69d1df65ba854565d7240b53..78c7d7dd5460e5b006d318c249700d86d83f62a9 100644 |
| --- a/base/time/time_win.cc |
| +++ b/base/time/time_win.cc |
| @@ -79,10 +79,12 @@ int64 CurrentWallclockMicroseconds() { |
| // Time between resampling the un-granular clock for this API. 60 seconds. |
| const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond; |
| +base::Lock initialize_clock_lock; |
|
jar (doing other things)
2014/08/15 17:40:39
nit: We're not allowed to have such items at globa
fmeawad
2014/08/15 21:50:50
I copied the lock from rollover_lock in the same f
|
| int64 initial_time = 0; |
| TimeTicks initial_ticks; |
| void InitializeClock() { |
| + base::AutoLock locked(initialize_clock_lock); |
| initial_ticks = TimeTicks::Now(); |
| initial_time = CurrentWallclockMicroseconds(); |
| } |
| @@ -119,8 +121,16 @@ Time Time::Now() { |
| while (true) { |
| TimeTicks ticks = TimeTicks::Now(); |
| + int64 start_time = 0; |
| + TimeTicks start_ticks; |
| + { |
| + base::AutoLock locked(initialize_clock_lock); |
| + start_time = initial_time; |
| + start_ticks = initial_ticks; |
| + } |
| + |
| // Calculate the time elapsed since we started our timer |
| - TimeDelta elapsed = ticks - initial_ticks; |
| + TimeDelta elapsed = ticks - start_ticks; |
| // Check if enough time has elapsed that we need to resync the clock. |
| if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) { |
| @@ -128,7 +138,7 @@ Time Time::Now() { |
| continue; |
| } |
| - return Time(elapsed + Time(initial_time)); |
| + return Time(elapsed + Time(start_time)); |
| } |
| } |
| @@ -359,21 +369,24 @@ bool IsBuggyAthlon(const base::CPU& cpu) { |
| class HighResNowSingleton { |
| public: |
| HighResNowSingleton() |
| - : ticks_per_second_(0), |
| - skew_(0) { |
| - InitializeClock(); |
| + : ticks_per_second_(0), |
| + skew_(0) { |
| base::CPU cpu; |
| if (IsBuggyAthlon(cpu)) |
| - DisableHighResClock(); |
| - } |
| + return; |
| - bool IsUsingHighResClock() { |
| - return ticks_per_second_ != 0.0; |
| + // Synchronize the QPC clock with GetSystemTimeAsFileTime. |
| + LARGE_INTEGER ticks_per_sec = {0}; |
| + if (!QueryPerformanceFrequency(&ticks_per_sec)) |
| + return; // QPC is not available. |
| + ticks_per_second_ = ticks_per_sec.QuadPart; |
| + |
| + skew_ = UnreliableNow() - ReliableNow(); |
| } |
| - void DisableHighResClock() { |
| - ticks_per_second_ = 0.0; |
| + bool IsUsingHighResClock() { |
| + return ticks_per_second_ != 0; |
| } |
| TimeDelta Now() { |
| @@ -408,16 +421,6 @@ class HighResNowSingleton { |
| } |
| private: |
| - // Synchronize the QPC clock with GetSystemTimeAsFileTime. |
| - void InitializeClock() { |
| - LARGE_INTEGER ticks_per_sec = {0}; |
| - if (!QueryPerformanceFrequency(&ticks_per_sec)) |
| - return; // Broken, we don't guarantee this function works. |
| - ticks_per_second_ = ticks_per_sec.QuadPart; |
| - |
| - skew_ = UnreliableNow() - ReliableNow(); |
| - } |
| - |
| // Get the number of microseconds since boot in an unreliable fashion. |
| int64 UnreliableNow() { |
| LARGE_INTEGER now; |
| @@ -480,6 +483,7 @@ bool TimeTicks::SetNowIsHighResNowIfSupported() { |
| } |
| now_function = HighResNowWrapper; |
| + InitializeClock(); |
| return true; |
| } |