Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: base/time/time_win.cc

Issue 541203002: fix for high resolution timer on windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/message_loop/message_loop_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 unsigned int g_high_res_timer_count = 0;
jamesr 2014/09/04 22:56:03 uint32_t 'unsigned int' is redundant and we don't
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
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 unsigned int max = std::numeric_limits<unsigned int>::max();
jamesr 2014/09/04 22:56:03 uint32_t
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 = 205
206 // Check for overflow or underflow.
207 DCHECK((g_high_res_timer_count != 0) || activating);
208 DCHECK((g_high_res_timer_count != max) || !activating);
209
210 unsigned int high_res_count =
204 activating ? ++g_high_res_timer_count : --g_high_res_timer_count; 211 activating ? ++g_high_res_timer_count : --g_high_res_timer_count;
205 212
206 if (activating) { 213 if (activating) {
207 if (high_res_count == 1) 214 if (high_res_count == 1)
208 timeBeginPeriod(period); 215 timeBeginPeriod(period);
209 } else { 216 } else {
210 if (high_res_count == 0) 217 if (high_res_count == 0)
211 timeEndPeriod(period); 218 timeEndPeriod(period);
212 } 219 }
213 return (period == kMinTimerIntervalHighResMs); 220 return (period == kMinTimerIntervalHighResMs);
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); 552 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime());
546 } 553 }
547 } 554 }
548 555
549 // TimeDelta ------------------------------------------------------------------ 556 // TimeDelta ------------------------------------------------------------------
550 557
551 // static 558 // static
552 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { 559 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) {
553 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); 560 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value));
554 } 561 }
OLDNEW
« no previous file with comments | « base/message_loop/message_loop_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698