| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project 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 #ifndef V8_BASE_PLATFORM_TIME_H_ | 5 #ifndef V8_BASE_PLATFORM_TIME_H_ |
| 6 #define V8_BASE_PLATFORM_TIME_H_ | 6 #define V8_BASE_PLATFORM_TIME_H_ |
| 7 | 7 |
| 8 #include <ctime> | 8 #include <ctime> |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 #include <limits> | 10 #include <limits> |
| 11 | 11 |
| 12 #include "src/base/base-export.h" | |
| 13 #include "src/base/bits.h" | 12 #include "src/base/bits.h" |
| 14 #include "src/base/macros.h" | 13 #include "src/base/macros.h" |
| 15 #include "src/base/safe_math.h" | 14 #include "src/base/safe_math.h" |
| 16 #if V8_OS_WIN | 15 #if V8_OS_WIN |
| 17 #include "src/base/win32-headers.h" | 16 #include "src/base/win32-headers.h" |
| 18 #endif | 17 #endif |
| 19 | 18 |
| 20 // Forward declarations. | 19 // Forward declarations. |
| 21 extern "C" { | 20 extern "C" { |
| 22 struct _FILETIME; | 21 struct _FILETIME; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 template<class TimeClass> | 35 template<class TimeClass> |
| 37 class TimeBase; | 36 class TimeBase; |
| 38 } | 37 } |
| 39 | 38 |
| 40 // ----------------------------------------------------------------------------- | 39 // ----------------------------------------------------------------------------- |
| 41 // TimeDelta | 40 // TimeDelta |
| 42 // | 41 // |
| 43 // This class represents a duration of time, internally represented in | 42 // This class represents a duration of time, internally represented in |
| 44 // microseonds. | 43 // microseonds. |
| 45 | 44 |
| 46 class V8_BASE_EXPORT TimeDelta final { | 45 class TimeDelta final { |
| 47 public: | 46 public: |
| 48 TimeDelta() : delta_(0) {} | 47 TimeDelta() : delta_(0) {} |
| 49 | 48 |
| 50 // Converts units of time to TimeDeltas. | 49 // Converts units of time to TimeDeltas. |
| 51 static TimeDelta FromDays(int days); | 50 static TimeDelta FromDays(int days); |
| 52 static TimeDelta FromHours(int hours); | 51 static TimeDelta FromHours(int hours); |
| 53 static TimeDelta FromMinutes(int minutes); | 52 static TimeDelta FromMinutes(int minutes); |
| 54 static TimeDelta FromSeconds(int64_t seconds); | 53 static TimeDelta FromSeconds(int64_t seconds); |
| 55 static TimeDelta FromMilliseconds(int64_t milliseconds); | 54 static TimeDelta FromMilliseconds(int64_t milliseconds); |
| 56 static TimeDelta FromMicroseconds(int64_t microseconds) { | 55 static TimeDelta FromMicroseconds(int64_t microseconds) { |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 270 |
| 272 } // namespace time_internal | 271 } // namespace time_internal |
| 273 | 272 |
| 274 | 273 |
| 275 // ----------------------------------------------------------------------------- | 274 // ----------------------------------------------------------------------------- |
| 276 // Time | 275 // Time |
| 277 // | 276 // |
| 278 // This class represents an absolute point in time, internally represented as | 277 // This class represents an absolute point in time, internally represented as |
| 279 // microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970. | 278 // microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970. |
| 280 | 279 |
| 281 class V8_BASE_EXPORT Time final : public time_internal::TimeBase<Time> { | 280 class Time final : public time_internal::TimeBase<Time> { |
| 282 public: | 281 public: |
| 283 // Contains the NULL time. Use Time::Now() to get the current time. | 282 // Contains the NULL time. Use Time::Now() to get the current time. |
| 284 Time() : TimeBase(0) {} | 283 Time() : TimeBase(0) {} |
| 285 | 284 |
| 286 // Returns the current time. Watch out, the system might adjust its clock | 285 // Returns the current time. Watch out, the system might adjust its clock |
| 287 // in which case time will actually go backwards. We don't guarantee that | 286 // in which case time will actually go backwards. We don't guarantee that |
| 288 // times are increasing, or that two calls to Now() won't be the same. | 287 // times are increasing, or that two calls to Now() won't be the same. |
| 289 static Time Now(); | 288 static Time Now(); |
| 290 | 289 |
| 291 // Returns the current time. Same as Now() except that this function always | 290 // Returns the current time. Same as Now() except that this function always |
| (...skipping 24 matching lines...) Expand all Loading... |
| 316 // Converts to/from the Javascript convention for times, a number of | 315 // Converts to/from the Javascript convention for times, a number of |
| 317 // milliseconds since the epoch: | 316 // milliseconds since the epoch: |
| 318 static Time FromJsTime(double ms_since_epoch); | 317 static Time FromJsTime(double ms_since_epoch); |
| 319 double ToJsTime() const; | 318 double ToJsTime() const; |
| 320 | 319 |
| 321 private: | 320 private: |
| 322 friend class time_internal::TimeBase<Time>; | 321 friend class time_internal::TimeBase<Time>; |
| 323 explicit Time(int64_t us) : TimeBase(us) {} | 322 explicit Time(int64_t us) : TimeBase(us) {} |
| 324 }; | 323 }; |
| 325 | 324 |
| 326 V8_BASE_EXPORT std::ostream& operator<<(std::ostream&, const Time&); | 325 std::ostream& operator<<(std::ostream&, const Time&); |
| 327 | 326 |
| 328 inline Time operator+(const TimeDelta& delta, const Time& time) { | 327 inline Time operator+(const TimeDelta& delta, const Time& time) { |
| 329 return time + delta; | 328 return time + delta; |
| 330 } | 329 } |
| 331 | 330 |
| 332 | 331 |
| 333 // ----------------------------------------------------------------------------- | 332 // ----------------------------------------------------------------------------- |
| 334 // TimeTicks | 333 // TimeTicks |
| 335 // | 334 // |
| 336 // This class represents an abstract time that is most of the time incrementing | 335 // This class represents an abstract time that is most of the time incrementing |
| 337 // for use in measuring time durations. It is internally represented in | 336 // for use in measuring time durations. It is internally represented in |
| 338 // microseconds. It can not be converted to a human-readable time, but is | 337 // microseconds. It can not be converted to a human-readable time, but is |
| 339 // guaranteed not to decrease (if the user changes the computer clock, | 338 // guaranteed not to decrease (if the user changes the computer clock, |
| 340 // Time::Now() may actually decrease or jump). But note that TimeTicks may | 339 // Time::Now() may actually decrease or jump). But note that TimeTicks may |
| 341 // "stand still", for example if the computer suspended. | 340 // "stand still", for example if the computer suspended. |
| 342 | 341 |
| 343 class V8_BASE_EXPORT TimeTicks final | 342 class TimeTicks final : public time_internal::TimeBase<TimeTicks> { |
| 344 : public time_internal::TimeBase<TimeTicks> { | |
| 345 public: | 343 public: |
| 346 TimeTicks() : TimeBase(0) {} | 344 TimeTicks() : TimeBase(0) {} |
| 347 | 345 |
| 348 // Platform-dependent tick count representing "right now." | 346 // Platform-dependent tick count representing "right now." |
| 349 // The resolution of this clock is ~1-15ms. Resolution varies depending | 347 // The resolution of this clock is ~1-15ms. Resolution varies depending |
| 350 // on hardware/operating system configuration. | 348 // on hardware/operating system configuration. |
| 351 // This method never returns a null TimeTicks. | 349 // This method never returns a null TimeTicks. |
| 352 static TimeTicks Now(); | 350 static TimeTicks Now(); |
| 353 | 351 |
| 354 // Returns a platform-dependent high-resolution tick count. Implementation | 352 // Returns a platform-dependent high-resolution tick count. Implementation |
| (...skipping 16 matching lines...) Expand all Loading... |
| 371 | 369 |
| 372 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) { | 370 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) { |
| 373 return ticks + delta; | 371 return ticks + delta; |
| 374 } | 372 } |
| 375 | 373 |
| 376 | 374 |
| 377 // ThreadTicks ---------------------------------------------------------------- | 375 // ThreadTicks ---------------------------------------------------------------- |
| 378 | 376 |
| 379 // Represents a clock, specific to a particular thread, than runs only while the | 377 // Represents a clock, specific to a particular thread, than runs only while the |
| 380 // thread is running. | 378 // thread is running. |
| 381 class V8_BASE_EXPORT ThreadTicks final | 379 class ThreadTicks final : public time_internal::TimeBase<ThreadTicks> { |
| 382 : public time_internal::TimeBase<ThreadTicks> { | |
| 383 public: | 380 public: |
| 384 ThreadTicks() : TimeBase(0) {} | 381 ThreadTicks() : TimeBase(0) {} |
| 385 | 382 |
| 386 // Returns true if ThreadTicks::Now() is supported on this system. | 383 // Returns true if ThreadTicks::Now() is supported on this system. |
| 387 static bool IsSupported(); | 384 static bool IsSupported(); |
| 388 | 385 |
| 389 // Waits until the initialization is completed. Needs to be guarded with a | 386 // Waits until the initialization is completed. Needs to be guarded with a |
| 390 // call to IsSupported(). | 387 // call to IsSupported(). |
| 391 static void WaitUntilInitialized() { | 388 static void WaitUntilInitialized() { |
| 392 #if V8_OS_WIN | 389 #if V8_OS_WIN |
| (...skipping 11 matching lines...) Expand all Loading... |
| 404 static ThreadTicks Now(); | 401 static ThreadTicks Now(); |
| 405 | 402 |
| 406 #if V8_OS_WIN | 403 #if V8_OS_WIN |
| 407 // Similar to Now() above except this returns thread-specific CPU time for an | 404 // Similar to Now() above except this returns thread-specific CPU time for an |
| 408 // arbitrary thread. All comments for Now() method above apply apply to this | 405 // arbitrary thread. All comments for Now() method above apply apply to this |
| 409 // method as well. | 406 // method as well. |
| 410 static ThreadTicks GetForThread(const HANDLE& thread_handle); | 407 static ThreadTicks GetForThread(const HANDLE& thread_handle); |
| 411 #endif | 408 #endif |
| 412 | 409 |
| 413 private: | 410 private: |
| 414 template <class TimeClass> | |
| 415 friend class time_internal::TimeBase; | |
| 416 | |
| 417 // Please use Now() or GetForThread() to create a new object. This is for | 411 // Please use Now() or GetForThread() to create a new object. This is for |
| 418 // internal use and testing. Ticks are in microseconds. | 412 // internal use and testing. Ticks are in microseconds. |
| 419 explicit ThreadTicks(int64_t ticks) : TimeBase(ticks) {} | 413 explicit ThreadTicks(int64_t ticks) : TimeBase(ticks) {} |
| 420 | 414 |
| 421 #if V8_OS_WIN | 415 #if V8_OS_WIN |
| 422 // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't | 416 // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't |
| 423 // been measured yet. Needs to be guarded with a call to IsSupported(). | 417 // been measured yet. Needs to be guarded with a call to IsSupported(). |
| 424 static double TSCTicksPerSecond(); | 418 static double TSCTicksPerSecond(); |
| 425 static bool IsSupportedWin(); | 419 static bool IsSupportedWin(); |
| 426 static void WaitUntilInitializedWin(); | 420 static void WaitUntilInitializedWin(); |
| 427 #endif | 421 #endif |
| 428 }; | 422 }; |
| 429 | 423 |
| 430 } // namespace base | 424 } // namespace base |
| 431 } // namespace v8 | 425 } // namespace v8 |
| 432 | 426 |
| 433 #endif // V8_BASE_PLATFORM_TIME_H_ | 427 #endif // V8_BASE_PLATFORM_TIME_H_ |
| OLD | NEW |