| 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 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Restarts the timer and returns the time elapsed since the previous start. | 50 // Restarts the timer and returns the time elapsed since the previous start. |
| 51 // This method is equivalent to obtaining the elapsed time with |Elapsed()| | 51 // This method is equivalent to obtaining the elapsed time with |Elapsed()| |
| 52 // and then starting the timer again, but does so in one single operation, | 52 // and then starting the timer again, but does so in one single operation, |
| 53 // avoiding the need to obtain the clock value twice. It may only be called | 53 // avoiding the need to obtain the clock value twice. It may only be called |
| 54 // on a previously started timer. | 54 // on a previously started timer. |
| 55 TimeDelta Restart() { return Restart(Now()); } | 55 TimeDelta Restart() { |
| 56 | |
| 57 TimeDelta Restart(TimeTicks now) { | |
| 58 DCHECK(IsStarted()); | 56 DCHECK(IsStarted()); |
| 59 TimeDelta elapsed = now - start_ticks_; | 57 TimeTicks ticks = Now(); |
| 58 TimeDelta elapsed = ticks - start_ticks_; |
| 60 DCHECK(elapsed.InMicroseconds() >= 0); | 59 DCHECK(elapsed.InMicroseconds() >= 0); |
| 61 start_ticks_ = now; | 60 start_ticks_ = ticks; |
| 62 DCHECK(IsStarted()); | 61 DCHECK(IsStarted()); |
| 63 return elapsed; | 62 return elapsed; |
| 64 } | 63 } |
| 65 | 64 |
| 66 // Returns the time elapsed since the previous start. This method may only | 65 // Returns the time elapsed since the previous start. This method may only |
| 67 // be called on a previously started timer. | 66 // be called on a previously started timer. |
| 68 TimeDelta Elapsed() const { | 67 TimeDelta Elapsed() const { |
| 69 DCHECK(IsStarted()); | 68 DCHECK(IsStarted()); |
| 70 TimeDelta elapsed = Now() - start_ticks_; | 69 TimeDelta elapsed = Now() - start_ticks_; |
| 71 DCHECK(elapsed.InMicroseconds() >= 0); | 70 DCHECK(elapsed.InMicroseconds() >= 0); |
| 72 return elapsed; | 71 return elapsed; |
| 73 } | 72 } |
| 74 | 73 |
| 75 // Move the start_ticks_ to adjust the current timer. | |
| 76 void Subtract(TimeDelta delta) { | |
| 77 DCHECK(IsStarted()); | |
| 78 // If the delta is negative we want to make the elapsed time smaller, hence | |
| 79 // we have to move the start_ticks_ in the opposite direction. | |
| 80 start_ticks_ += delta; | |
| 81 } | |
| 82 | |
| 83 // Returns |true| if the specified |time_delta| has elapsed since the | 74 // Returns |true| if the specified |time_delta| has elapsed since the |
| 84 // previous start, or |false| if not. This method may only be called on | 75 // previous start, or |false| if not. This method may only be called on |
| 85 // a previously started timer. | 76 // a previously started timer. |
| 86 bool HasExpired(TimeDelta time_delta) const { | 77 bool HasExpired(TimeDelta time_delta) const { |
| 87 DCHECK(IsStarted()); | 78 DCHECK(IsStarted()); |
| 88 return Elapsed() >= time_delta; | 79 return Elapsed() >= time_delta; |
| 89 } | 80 } |
| 90 | 81 |
| 82 private: |
| 91 static V8_INLINE TimeTicks Now() { | 83 static V8_INLINE TimeTicks Now() { |
| 92 TimeTicks now = TimeTicks::HighResolutionNow(); | 84 TimeTicks now = TimeTicks::HighResolutionNow(); |
| 93 DCHECK(!now.IsNull()); | 85 DCHECK(!now.IsNull()); |
| 94 return now; | 86 return now; |
| 95 } | 87 } |
| 96 | 88 |
| 97 private: | |
| 98 TimeTicks start_ticks_; | 89 TimeTicks start_ticks_; |
| 99 #ifdef DEBUG | 90 #ifdef DEBUG |
| 100 bool started_; | 91 bool started_; |
| 101 #endif | 92 #endif |
| 102 }; | 93 }; |
| 103 | 94 |
| 104 } // namespace base | 95 } // namespace base |
| 105 } // namespace v8 | 96 } // namespace v8 |
| 106 | 97 |
| 107 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ | 98 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ |
| OLD | NEW |