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; |
102 | 103 |
103 // static | 104 // static |
104 Time Time::Now() { | 105 Time Time::Now() { |
105 if (initial_time == 0) | 106 if (initial_time == 0) |
106 InitializeClock(); | 107 InitializeClock(); |
107 | 108 |
108 // We implement time using the high-resolution timers so that we can get | 109 // We implement time using the high-resolution timers so that we can get |
109 // timeouts which are smaller than 10-15ms. If we just used | 110 // timeouts which are smaller than 10-15ms. If we just used |
110 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. | 111 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. |
111 // | 112 // |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 static PlatformThreadId my_thread = PlatformThread::CurrentId(); | 156 static PlatformThreadId my_thread = PlatformThread::CurrentId(); |
156 DCHECK(PlatformThread::CurrentId() == my_thread); | 157 DCHECK(PlatformThread::CurrentId() == my_thread); |
157 | 158 |
158 if (high_resolution_timer_enabled_ == enable) | 159 if (high_resolution_timer_enabled_ == enable) |
159 return; | 160 return; |
160 | 161 |
161 high_resolution_timer_enabled_ = enable; | 162 high_resolution_timer_enabled_ = enable; |
162 } | 163 } |
163 | 164 |
164 // static | 165 // static |
165 bool Time::ActivateHighResolutionTimer(bool activate) { | 166 bool Time::ActivateHighResolutionTimer(bool activating) { |
166 if (!high_resolution_timer_enabled_) | 167 if (!high_resolution_timer_enabled_ && activating) |
167 return false; | 168 return false; |
168 | 169 |
169 // Using anything other than 1ms makes timers granular | 170 // Using anything other than 1ms makes timers granular |
170 // to that interval. | 171 // to that interval. |
171 const int kMinTimerIntervalMs = 1; | 172 const int kMinTimerIntervalMs = 1; |
172 MMRESULT result; | 173 MMRESULT result; |
173 if (activate) | 174 if (activating) { |
174 result = timeBeginPeriod(kMinTimerIntervalMs); | 175 result = timeBeginPeriod(kMinTimerIntervalMs); |
175 else | 176 high_resolution_timer_activated_++; |
| 177 } else { |
176 result = timeEndPeriod(kMinTimerIntervalMs); | 178 result = timeEndPeriod(kMinTimerIntervalMs); |
| 179 high_resolution_timer_activated_--; |
| 180 } |
177 return result == TIMERR_NOERROR; | 181 return result == TIMERR_NOERROR; |
178 } | 182 } |
179 | 183 |
180 // static | 184 // 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 |
181 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 196 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
182 // Create the system struct representing our exploded time. It will either be | 197 // Create the system struct representing our exploded time. It will either be |
183 // in local time or UTC. | 198 // in local time or UTC. |
184 SYSTEMTIME st; | 199 SYSTEMTIME st; |
185 st.wYear = exploded.year; | 200 st.wYear = exploded.year; |
186 st.wMonth = exploded.month; | 201 st.wMonth = exploded.month; |
187 st.wDayOfWeek = exploded.day_of_week; | 202 st.wDayOfWeek = exploded.day_of_week; |
188 st.wDay = exploded.day_of_month; | 203 st.wDay = exploded.day_of_month; |
189 st.wHour = exploded.hour; | 204 st.wHour = exploded.hour; |
190 st.wMinute = exploded.minute; | 205 st.wMinute = exploded.minute; |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 414 |
400 // static | 415 // static |
401 int64 TimeTicks::GetQPCDriftMicroseconds() { | 416 int64 TimeTicks::GetQPCDriftMicroseconds() { |
402 return Singleton<HighResNowSingleton>::get()->GetQPCDriftMicroseconds(); | 417 return Singleton<HighResNowSingleton>::get()->GetQPCDriftMicroseconds(); |
403 } | 418 } |
404 | 419 |
405 // static | 420 // static |
406 bool TimeTicks::IsHighResClockWorking() { | 421 bool TimeTicks::IsHighResClockWorking() { |
407 return Singleton<HighResNowSingleton>::get()->IsUsingHighResClock(); | 422 return Singleton<HighResNowSingleton>::get()->IsUsingHighResClock(); |
408 } | 423 } |
OLD | NEW |