OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 124 |
125 | 125 |
126 #if V8_OS_WIN | 126 #if V8_OS_WIN |
127 | 127 |
128 // We implement time using the high-resolution timers so that we can get | 128 // We implement time using the high-resolution timers so that we can get |
129 // timeouts which are smaller than 10-15ms. To avoid any drift, we | 129 // timeouts which are smaller than 10-15ms. To avoid any drift, we |
130 // periodically resync the internal clock to the system clock. | 130 // periodically resync the internal clock to the system clock. |
131 class Clock V8_FINAL { | 131 class Clock V8_FINAL { |
132 public: | 132 public: |
133 Clock() : initial_time_(CurrentWallclockTime()), | 133 Clock() : initial_time_(CurrentWallclockTime()), |
134 initial_ticks_(TimeTicks::Now()), | 134 initial_ticks_(TimeTicks::Now()) {} |
135 mutex_(OS::CreateMutex()) {} | |
136 | |
137 ~Clock() { delete mutex_; } | |
138 | 135 |
139 Time Now() { | 136 Time Now() { |
140 // This must be executed under lock. | 137 // This must be executed under lock. |
141 ScopedLock sl(mutex_); | 138 LockGuard<Mutex> lock_guard(&mutex_); |
142 | 139 |
143 // Calculate the time elapsed since we started our timer. | 140 // Calculate the time elapsed since we started our timer. |
144 TimeDelta elapsed = TimeTicks::Now() - initial_ticks_; | 141 TimeDelta elapsed = TimeTicks::Now() - initial_ticks_; |
145 | 142 |
146 // Check if we don't need to synchronize with the wallclock yet. | 143 // Check if we don't need to synchronize with the wallclock yet. |
147 if (elapsed.InMicroseconds() <= kMaxMicrosecondsToAvoidDrift) { | 144 if (elapsed.InMicroseconds() <= kMaxMicrosecondsToAvoidDrift) { |
148 return initial_time_ + elapsed; | 145 return initial_time_ + elapsed; |
149 } | 146 } |
150 | 147 |
151 // Resynchronize with the wallclock. | 148 // Resynchronize with the wallclock. |
152 initial_ticks_ = TimeTicks::Now(); | 149 initial_ticks_ = TimeTicks::Now(); |
153 initial_time_ = CurrentWallclockTime(); | 150 initial_time_ = CurrentWallclockTime(); |
154 return initial_time_; | 151 return initial_time_; |
155 } | 152 } |
156 | 153 |
157 Time NowFromSystemTime() { | 154 Time NowFromSystemTime() { |
158 ScopedLock sl(mutex_); | 155 // This must be executed under lock. |
| 156 LockGuard<Mutex> lock_guard(&mutex_); |
| 157 |
| 158 // Resynchronize with the wallclock. |
159 initial_ticks_ = TimeTicks::Now(); | 159 initial_ticks_ = TimeTicks::Now(); |
160 initial_time_ = CurrentWallclockTime(); | 160 initial_time_ = CurrentWallclockTime(); |
161 return initial_time_; | 161 return initial_time_; |
162 } | 162 } |
163 | 163 |
164 private: | 164 private: |
165 // Time between resampling the un-granular clock for this API (1 minute). | 165 // Time between resampling the un-granular clock for this API (1 minute). |
166 static const int64_t kMaxMicrosecondsToAvoidDrift = | 166 static const int64_t kMaxMicrosecondsToAvoidDrift = |
167 Time::kMicrosecondsPerMinute; | 167 Time::kMicrosecondsPerMinute; |
168 | 168 |
169 static Time CurrentWallclockTime() { | 169 static Time CurrentWallclockTime() { |
170 FILETIME ft; | 170 FILETIME ft; |
171 ::GetSystemTimeAsFileTime(&ft); | 171 ::GetSystemTimeAsFileTime(&ft); |
172 return Time::FromFiletime(ft); | 172 return Time::FromFiletime(ft); |
173 } | 173 } |
174 | 174 |
175 TimeTicks initial_ticks_; | 175 TimeTicks initial_ticks_; |
176 Time initial_time_; | 176 Time initial_time_; |
177 Mutex* mutex_; | 177 Mutex mutex_; |
178 }; | 178 }; |
179 | 179 |
180 | 180 |
181 static LazyDynamicInstance<Clock, | 181 static LazyDynamicInstance<Clock, |
182 DefaultCreateTrait<Clock>, | 182 DefaultCreateTrait<Clock>, |
183 ThreadSafeInitOnceTrait>::type clock = LAZY_DYNAMIC_INSTANCE_INITIALIZER; | 183 ThreadSafeInitOnceTrait>::type clock = LAZY_DYNAMIC_INSTANCE_INITIALIZER; |
184 | 184 |
185 | 185 |
186 Time Time::Now() { | 186 Time Time::Now() { |
187 return clock.Pointer()->Now(); | 187 return clock.Pointer()->Now(); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 return (tick_count_ms * Time::kMicrosecondsPerMillisecond) + 1; | 389 return (tick_count_ms * Time::kMicrosecondsPerMillisecond) + 1; |
390 } | 390 } |
391 | 391 |
392 private: | 392 private: |
393 GETTICKCOUNT64PROC func_; | 393 GETTICKCOUNT64PROC func_; |
394 }; | 394 }; |
395 | 395 |
396 | 396 |
397 class RolloverProtectedTickClock V8_FINAL : public TickClock { | 397 class RolloverProtectedTickClock V8_FINAL : public TickClock { |
398 public: | 398 public: |
399 RolloverProtectedTickClock() | 399 // We initialize rollover_ms_ to 1 to ensure that we will never |
400 : mutex_(OS::CreateMutex()), last_seen_now_(0), rollover_ms_(1) { | 400 // return 0 from TimeTicks::HighResNow() and TimeTicks::Now() below. |
401 // We initialize rollover_ms_ to 1 to ensure that we will never | 401 RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {} |
402 // return 0 from TimeTicks::HighResNow() and TimeTicks::Now() below. | 402 virtual ~RolloverProtectedTickClock() {} |
403 } | |
404 virtual ~RolloverProtectedTickClock() { delete mutex_; } | |
405 | 403 |
406 virtual int64_t Now() V8_OVERRIDE { | 404 virtual int64_t Now() V8_OVERRIDE { |
407 ScopedLock sl(mutex_); | 405 LockGuard<Mutex> lock_guard(&mutex_); |
408 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over | 406 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over |
409 // every ~49.7 days. We try to track rollover ourselves, which works if | 407 // every ~49.7 days. We try to track rollover ourselves, which works if |
410 // TimeTicks::Now() is called at least every 49 days. | 408 // TimeTicks::Now() is called at least every 49 days. |
411 // Note that we do not use GetTickCount() here, since timeGetTime() gives | 409 // Note that we do not use GetTickCount() here, since timeGetTime() gives |
412 // more predictable delta values, as described here: | 410 // more predictable delta values, as described here: |
413 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-diffe
rence-between-gettickcount-and-timegettime.aspx | 411 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-diffe
rence-between-gettickcount-and-timegettime.aspx |
414 DWORD now = timeGetTime(); | 412 DWORD now = timeGetTime(); |
415 if (now < last_seen_now_) { | 413 if (now < last_seen_now_) { |
416 rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days. | 414 rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days. |
417 } | 415 } |
418 last_seen_now_ = now; | 416 last_seen_now_ = now; |
419 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond; | 417 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond; |
420 } | 418 } |
421 | 419 |
422 private: | 420 private: |
423 Mutex* mutex_; | 421 Mutex mutex_; |
424 DWORD last_seen_now_; | 422 DWORD last_seen_now_; |
425 int64_t rollover_ms_; | 423 int64_t rollover_ms_; |
426 }; | 424 }; |
427 | 425 |
428 | 426 |
429 struct CreateTickClockTrait { | 427 struct CreateTickClockTrait { |
430 static TickClock* Create() { | 428 static TickClock* Create() { |
431 // Try to load GetTickCount64() from kernel32.dll (available since Vista). | 429 // Try to load GetTickCount64() from kernel32.dll (available since Vista). |
432 HMODULE kernel32 = ::GetModuleHandleA("kernel32.dll"); | 430 HMODULE kernel32 = ::GetModuleHandleA("kernel32.dll"); |
433 ASSERT(kernel32 != NULL); | 431 ASSERT(kernel32 != NULL); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + | 517 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + |
520 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); | 518 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); |
521 #endif // V8_OS_MACOSX | 519 #endif // V8_OS_MACOSX |
522 // Make sure we never return 0 here. | 520 // Make sure we never return 0 here. |
523 return TimeTicks(ticks + 1); | 521 return TimeTicks(ticks + 1); |
524 } | 522 } |
525 | 523 |
526 #endif // V8_OS_WIN | 524 #endif // V8_OS_WIN |
527 | 525 |
528 } } // namespace v8::internal | 526 } } // namespace v8::internal |
OLD | NEW |