| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 // Windows Timer Primer | 6 // Windows Timer Primer |
| 7 // | 7 // |
| 8 // A good article: http://www.ddj.com/windows/184416651 | 8 // A good article: http://www.ddj.com/windows/184416651 |
| 9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 | 9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 |
| 10 // | 10 // |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 // Time ----------------------------------------------------------------------- | 92 // Time ----------------------------------------------------------------------- |
| 93 | 93 |
| 94 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 | 94 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 |
| 95 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the | 95 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the |
| 96 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding | 96 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding |
| 97 // 1700, 1800, and 1900. | 97 // 1700, 1800, and 1900. |
| 98 // static | 98 // static |
| 99 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000); | 99 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000); |
| 100 | 100 |
| 101 bool Time::high_resolution_timer_enabled_ = false; | 101 bool Time::high_resolution_timer_enabled_ = false; |
| 102 int Time::high_resolution_timer_activated_ = 0; | |
| 103 | 102 |
| 104 // static | 103 // static |
| 105 Time Time::Now() { | 104 Time Time::Now() { |
| 106 if (initial_time == 0) | 105 if (initial_time == 0) |
| 107 InitializeClock(); | 106 InitializeClock(); |
| 108 | 107 |
| 109 // We implement time using the high-resolution timers so that we can get | 108 // We implement time using the high-resolution timers so that we can get |
| 110 // timeouts which are smaller than 10-15ms. If we just used | 109 // timeouts which are smaller than 10-15ms. If we just used |
| 111 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. | 110 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. |
| 112 // | 111 // |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 static PlatformThreadId my_thread = PlatformThread::CurrentId(); | 155 static PlatformThreadId my_thread = PlatformThread::CurrentId(); |
| 157 DCHECK(PlatformThread::CurrentId() == my_thread); | 156 DCHECK(PlatformThread::CurrentId() == my_thread); |
| 158 | 157 |
| 159 if (high_resolution_timer_enabled_ == enable) | 158 if (high_resolution_timer_enabled_ == enable) |
| 160 return; | 159 return; |
| 161 | 160 |
| 162 high_resolution_timer_enabled_ = enable; | 161 high_resolution_timer_enabled_ = enable; |
| 163 } | 162 } |
| 164 | 163 |
| 165 // static | 164 // static |
| 166 bool Time::ActivateHighResolutionTimer(bool activating) { | 165 bool Time::ActivateHighResolutionTimer(bool activate) { |
| 167 if (!high_resolution_timer_enabled_ && activating) | 166 if (!high_resolution_timer_enabled_) |
| 168 return false; | 167 return false; |
| 169 | 168 |
| 170 // Using anything other than 1ms makes timers granular | 169 // Using anything other than 1ms makes timers granular |
| 171 // to that interval. | 170 // to that interval. |
| 172 const int kMinTimerIntervalMs = 1; | 171 const int kMinTimerIntervalMs = 1; |
| 173 MMRESULT result; | 172 MMRESULT result; |
| 174 if (activating) { | 173 if (activate) |
| 175 result = timeBeginPeriod(kMinTimerIntervalMs); | 174 result = timeBeginPeriod(kMinTimerIntervalMs); |
| 176 high_resolution_timer_activated_++; | 175 else |
| 177 } else { | |
| 178 result = timeEndPeriod(kMinTimerIntervalMs); | 176 result = timeEndPeriod(kMinTimerIntervalMs); |
| 179 high_resolution_timer_activated_--; | |
| 180 } | |
| 181 return result == TIMERR_NOERROR; | 177 return result == TIMERR_NOERROR; |
| 182 } | 178 } |
| 183 | 179 |
| 184 // static | 180 // static |
| 185 bool Time::IsHighResolutionTimerInUse() { | |
| 186 // Note: we should track the high_resolution_timer_activated_ value | |
| 187 // under a lock if we want it to be accurate in a system with multiple | |
| 188 // message loops. We don't do that - because we don't want to take the | |
| 189 // expense of a lock for this. We *only* track this value so that unit | |
| 190 // tests can see if the high resolution timer is on or off. | |
| 191 return high_resolution_timer_enabled_ && | |
| 192 high_resolution_timer_activated_ > 0; | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 181 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 197 // Create the system struct representing our exploded time. It will either be | 182 // Create the system struct representing our exploded time. It will either be |
| 198 // in local time or UTC. | 183 // in local time or UTC. |
| 199 SYSTEMTIME st; | 184 SYSTEMTIME st; |
| 200 st.wYear = exploded.year; | 185 st.wYear = exploded.year; |
| 201 st.wMonth = exploded.month; | 186 st.wMonth = exploded.month; |
| 202 st.wDayOfWeek = exploded.day_of_week; | 187 st.wDayOfWeek = exploded.day_of_week; |
| 203 st.wDay = exploded.day_of_month; | 188 st.wDay = exploded.day_of_month; |
| 204 st.wHour = exploded.hour; | 189 st.wHour = exploded.hour; |
| 205 st.wMinute = exploded.minute; | 190 st.wMinute = exploded.minute; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 | 399 |
| 415 // static | 400 // static |
| 416 int64 TimeTicks::GetQPCDriftMicroseconds() { | 401 int64 TimeTicks::GetQPCDriftMicroseconds() { |
| 417 return Singleton<HighResNowSingleton>::get()->GetQPCDriftMicroseconds(); | 402 return Singleton<HighResNowSingleton>::get()->GetQPCDriftMicroseconds(); |
| 418 } | 403 } |
| 419 | 404 |
| 420 // static | 405 // static |
| 421 bool TimeTicks::IsHighResClockWorking() { | 406 bool TimeTicks::IsHighResClockWorking() { |
| 422 return Singleton<HighResNowSingleton>::get()->IsUsingHighResClock(); | 407 return Singleton<HighResNowSingleton>::get()->IsUsingHighResClock(); |
| 423 } | 408 } |
| OLD | NEW |