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

Side by Side Diff: base/time_win.cc

Issue 6904117: Reland old fix that was reverted without my knowledge. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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
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
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_++;
jar (doing other things) 2011/05/01 17:42:34 Should you be concerned with atomic increment and
Mike Belshe 2011/05/01 21:58:00 Good point, I'll add atomics.
Mike Belshe 2011/05/04 07:23:13 Actually, Jim - I looked again. The comments in l
jar (doing other things) 2011/05/04 16:08:26 Ah... I see... sorry for my confusion. Maybe it w
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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 418
404 // static 419 // static
405 int64 TimeTicks::GetQPCDriftMicroseconds() { 420 int64 TimeTicks::GetQPCDriftMicroseconds() {
406 return HighResNowSingleton::GetInstance()->GetQPCDriftMicroseconds(); 421 return HighResNowSingleton::GetInstance()->GetQPCDriftMicroseconds();
407 } 422 }
408 423
409 // static 424 // static
410 bool TimeTicks::IsHighResClockWorking() { 425 bool TimeTicks::IsHighResClockWorking() {
411 return HighResNowSingleton::GetInstance()->IsUsingHighResClock(); 426 return HighResNowSingleton::GetInstance()->IsUsingHighResClock();
412 } 427 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698