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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
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; |
| 102 |
101 // static | 103 // static |
102 Time Time::Now() { | 104 Time Time::Now() { |
103 if (initial_time == 0) | 105 if (initial_time == 0) |
104 InitializeClock(); | 106 InitializeClock(); |
105 | 107 |
106 // 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 |
107 // timeouts which are smaller than 10-15ms. If we just used | 109 // timeouts which are smaller than 10-15ms. If we just used |
108 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. | 110 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. |
109 // | 111 // |
110 // To make this work, we initialize the clock (initial_time) and the | 112 // To make this work, we initialize the clock (initial_time) and the |
(...skipping 30 matching lines...) Expand all Loading... |
141 return Time(FileTimeToMicroseconds(ft)); | 143 return Time(FileTimeToMicroseconds(ft)); |
142 } | 144 } |
143 | 145 |
144 FILETIME Time::ToFileTime() const { | 146 FILETIME Time::ToFileTime() const { |
145 FILETIME utc_ft; | 147 FILETIME utc_ft; |
146 MicrosecondsToFileTime(us_, &utc_ft); | 148 MicrosecondsToFileTime(us_, &utc_ft); |
147 return utc_ft; | 149 return utc_ft; |
148 } | 150 } |
149 | 151 |
150 // static | 152 // static |
151 bool Time::UseHighResolutionTimer(bool use) { | 153 void Time::EnableHighResolutionTimer(bool enable) { |
152 // TODO(mbelshe): Make sure that switching the system timer resolution | 154 // Test for single-threaded access. |
153 // doesn't break Timer firing order etc. An example test would be to have | 155 static PlatformThreadId my_thread = PlatformThread::CurrentId(); |
154 // two threads. One would have a bunch of timers, and another would turn the | 156 DCHECK(PlatformThread::CurrentId() == my_thread); |
155 // high resolution timer on and off. | |
156 | 157 |
157 MMRESULT result; | 158 if (high_resolution_timer_enabled_ == enable) |
158 if (use) | 159 return; |
159 result = timeBeginPeriod(1); | 160 |
160 else | 161 high_resolution_timer_enabled_ = enable; |
161 result = timeEndPeriod(1); | |
162 return (result == TIMERR_NOERROR); | |
163 } | 162 } |
164 | 163 |
165 // static | 164 // static |
| 165 bool Time::ActivateHighResolutionTimer(bool activate) { |
| 166 if (!high_resolution_timer_enabled_) |
| 167 return false; |
| 168 |
| 169 // Using anything other than 1ms makes timers granular |
| 170 // to that interval. |
| 171 const int kMinTimerIntervalMs = 1; |
| 172 MMRESULT result; |
| 173 if (activate) |
| 174 result = timeBeginPeriod(kMinTimerIntervalMs); |
| 175 else |
| 176 result = timeEndPeriod(kMinTimerIntervalMs); |
| 177 return result == TIMERR_NOERROR; |
| 178 } |
| 179 |
| 180 // static |
166 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 181 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
167 // 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 |
168 // in local time or UTC. | 183 // in local time or UTC. |
169 SYSTEMTIME st; | 184 SYSTEMTIME st; |
170 st.wYear = exploded.year; | 185 st.wYear = exploded.year; |
171 st.wMonth = exploded.month; | 186 st.wMonth = exploded.month; |
172 st.wDayOfWeek = exploded.day_of_week; | 187 st.wDayOfWeek = exploded.day_of_week; |
173 st.wDay = exploded.day_of_month; | 188 st.wDay = exploded.day_of_month; |
174 st.wHour = exploded.hour; | 189 st.wHour = exploded.hour; |
175 st.wMinute = exploded.minute; | 190 st.wMinute = exploded.minute; |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 391 |
377 // static | 392 // static |
378 TimeTicks TimeTicks::Now() { | 393 TimeTicks TimeTicks::Now() { |
379 return TimeTicks() + RolloverProtectedNow(); | 394 return TimeTicks() + RolloverProtectedNow(); |
380 } | 395 } |
381 | 396 |
382 // static | 397 // static |
383 TimeTicks TimeTicks::HighResNow() { | 398 TimeTicks TimeTicks::HighResNow() { |
384 return TimeTicks() + Singleton<HighResNowSingleton>::get()->Now(); | 399 return TimeTicks() + Singleton<HighResNowSingleton>::get()->Now(); |
385 } | 400 } |
OLD | NEW |