OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 85 } |
86 | 86 |
87 // The two values that ActivateHighResolutionTimer uses to set the systemwide | 87 // The two values that ActivateHighResolutionTimer uses to set the systemwide |
88 // timer interrupt frequency on Windows. It controls how precise timers are | 88 // timer interrupt frequency on Windows. It controls how precise timers are |
89 // but also has a big impact on battery life. | 89 // but also has a big impact on battery life. |
90 const int kMinTimerIntervalHighResMs = 1; | 90 const int kMinTimerIntervalHighResMs = 1; |
91 const int kMinTimerIntervalLowResMs = 4; | 91 const int kMinTimerIntervalLowResMs = 4; |
92 // Track if kMinTimerIntervalHighResMs or kMinTimerIntervalLowResMs is active. | 92 // Track if kMinTimerIntervalHighResMs or kMinTimerIntervalLowResMs is active. |
93 bool g_high_res_timer_enabled = false; | 93 bool g_high_res_timer_enabled = false; |
94 // How many times the high resolution timer has been called. | 94 // How many times the high resolution timer has been called. |
95 int g_high_res_timer_count = 0; | 95 uint32_t g_high_res_timer_count = 0; |
96 // The lock to control access to the above two variables. | 96 // The lock to control access to the above two variables. |
97 base::LazyInstance<base::Lock>::Leaky g_high_res_lock = | 97 base::LazyInstance<base::Lock>::Leaky g_high_res_lock = |
98 LAZY_INSTANCE_INITIALIZER; | 98 LAZY_INSTANCE_INITIALIZER; |
99 | 99 |
100 } // namespace | 100 } // namespace |
101 | 101 |
102 // Time ----------------------------------------------------------------------- | 102 // Time ----------------------------------------------------------------------- |
103 | 103 |
104 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 | 104 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 |
105 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the | 105 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 timeEndPeriod(kMinTimerIntervalHighResMs); | 190 timeEndPeriod(kMinTimerIntervalHighResMs); |
191 timeBeginPeriod(kMinTimerIntervalLowResMs); | 191 timeBeginPeriod(kMinTimerIntervalLowResMs); |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 // static | 195 // static |
196 bool Time::ActivateHighResolutionTimer(bool activating) { | 196 bool Time::ActivateHighResolutionTimer(bool activating) { |
197 // We only do work on the transition from zero to one or one to zero so we | 197 // We only do work on the transition from zero to one or one to zero so we |
198 // can easily undo the effect (if necessary) when EnableHighResolutionTimer is | 198 // can easily undo the effect (if necessary) when EnableHighResolutionTimer is |
199 // called. | 199 // called. |
| 200 const uint32_t max = std::numeric_limits<uint32_t>::max(); |
| 201 |
200 base::AutoLock lock(g_high_res_lock.Get()); | 202 base::AutoLock lock(g_high_res_lock.Get()); |
201 UINT period = g_high_res_timer_enabled ? kMinTimerIntervalHighResMs | 203 UINT period = g_high_res_timer_enabled ? kMinTimerIntervalHighResMs |
202 : kMinTimerIntervalLowResMs; | 204 : kMinTimerIntervalLowResMs; |
203 int high_res_count = | |
204 activating ? ++g_high_res_timer_count : --g_high_res_timer_count; | |
205 | |
206 if (activating) { | 205 if (activating) { |
207 if (high_res_count == 1) | 206 DCHECK(g_high_res_timer_count != max); |
| 207 ++g_high_res_timer_count; |
| 208 if (g_high_res_timer_count == 1) |
208 timeBeginPeriod(period); | 209 timeBeginPeriod(period); |
209 } else { | 210 } else { |
210 if (high_res_count == 0) | 211 DCHECK(g_high_res_timer_count != 0); |
| 212 --g_high_res_timer_count; |
| 213 if (g_high_res_timer_count == 0) |
211 timeEndPeriod(period); | 214 timeEndPeriod(period); |
212 } | 215 } |
213 return (period == kMinTimerIntervalHighResMs); | 216 return (period == kMinTimerIntervalHighResMs); |
214 } | 217 } |
215 | 218 |
216 // static | 219 // static |
217 bool Time::IsHighResolutionTimerInUse() { | 220 bool Time::IsHighResolutionTimerInUse() { |
218 base::AutoLock lock(g_high_res_lock.Get()); | 221 base::AutoLock lock(g_high_res_lock.Get()); |
219 return g_high_res_timer_enabled && g_high_res_timer_count > 0; | 222 return g_high_res_timer_enabled && g_high_res_timer_count > 0; |
220 } | 223 } |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); | 548 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); |
546 } | 549 } |
547 } | 550 } |
548 | 551 |
549 // TimeDelta ------------------------------------------------------------------ | 552 // TimeDelta ------------------------------------------------------------------ |
550 | 553 |
551 // static | 554 // static |
552 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { | 555 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { |
553 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); | 556 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); |
554 } | 557 } |
OLD | NEW |