| 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_ELAPSED_TIMER_H_ | 5 #ifndef V8_BASE_PLATFORM_ELAPSED_TIMER_H_ |
| 6 #define V8_BASE_PLATFORM_ELAPSED_TIMER_H_ | 6 #define V8_BASE_PLATFORM_ELAPSED_TIMER_H_ |
| 7 | 7 |
| 8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
| 9 #include "src/base/platform/time.h" | 9 #include "src/base/platform/time.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 class ElapsedTimer FINAL { | 14 class ElapsedTimer FINAL { |
| 15 public: | 15 public: |
| 16 #ifdef DEBUG | 16 #if DCHECK_IS_ON |
| 17 ElapsedTimer() : started_(false) {} | 17 ElapsedTimer() : started_(false) {} |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 // Starts this timer. Once started a timer can be checked with | 20 // Starts this timer. Once started a timer can be checked with |
| 21 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|. | 21 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|. |
| 22 // This method must not be called on an already started timer. | 22 // This method must not be called on an already started timer. |
| 23 void Start() { | 23 void Start() { |
| 24 DCHECK(!IsStarted()); | 24 DCHECK(!IsStarted()); |
| 25 start_ticks_ = Now(); | 25 start_ticks_ = Now(); |
| 26 #ifdef DEBUG | 26 #if DCHECK_IS_ON |
| 27 started_ = true; | 27 started_ = true; |
| 28 #endif | 28 #endif |
| 29 DCHECK(IsStarted()); | 29 DCHECK(IsStarted()); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Stops this timer. Must not be called on a timer that was not | 32 // Stops this timer. Must not be called on a timer that was not |
| 33 // started before. | 33 // started before. |
| 34 void Stop() { | 34 void Stop() { |
| 35 DCHECK(IsStarted()); | 35 DCHECK(IsStarted()); |
| 36 start_ticks_ = TimeTicks(); | 36 start_ticks_ = TimeTicks(); |
| 37 #ifdef DEBUG | 37 #if DCHECK_IS_ON |
| 38 started_ = false; | 38 started_ = false; |
| 39 #endif | 39 #endif |
| 40 DCHECK(!IsStarted()); | 40 DCHECK(!IsStarted()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Returns |true| if this timer was started previously. | 43 // Returns |true| if this timer was started previously. |
| 44 bool IsStarted() const { | 44 bool IsStarted() const { |
| 45 DCHECK(started_ || start_ticks_.IsNull()); | 45 DCHECK(started_ || start_ticks_.IsNull()); |
| 46 DCHECK(!started_ || !start_ticks_.IsNull()); | 46 DCHECK(!started_ || !start_ticks_.IsNull()); |
| 47 return !start_ticks_.IsNull(); | 47 return !start_ticks_.IsNull(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } | 80 } |
| 81 | 81 |
| 82 private: | 82 private: |
| 83 static V8_INLINE TimeTicks Now() { | 83 static V8_INLINE TimeTicks Now() { |
| 84 TimeTicks now = TimeTicks::HighResolutionNow(); | 84 TimeTicks now = TimeTicks::HighResolutionNow(); |
| 85 DCHECK(!now.IsNull()); | 85 DCHECK(!now.IsNull()); |
| 86 return now; | 86 return now; |
| 87 } | 87 } |
| 88 | 88 |
| 89 TimeTicks start_ticks_; | 89 TimeTicks start_ticks_; |
| 90 #ifdef DEBUG | 90 #if DCHECK_IS_ON |
| 91 bool started_; | 91 bool started_; |
| 92 #endif | 92 #endif |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 } } // namespace v8::base | 95 } } // namespace v8::base |
| 96 | 96 |
| 97 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ | 97 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ |
| OLD | NEW |