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() { | 55 TimeDelta Restart() { return Restart(Now()); } |
56 | |
57 TimeDelta Restart(TimeTicks ticks) { | |
Igor Sheludko
2016/11/18 18:56:27
s/ticks/now/ to be clear.
Camillo Bruni
2016/11/21 10:50:03
done.
| |
56 DCHECK(IsStarted()); | 58 DCHECK(IsStarted()); |
57 TimeTicks ticks = Now(); | |
58 TimeDelta elapsed = ticks - start_ticks_; | 59 TimeDelta elapsed = ticks - start_ticks_; |
59 DCHECK(elapsed.InMicroseconds() >= 0); | 60 DCHECK(elapsed.InMicroseconds() >= 0); |
60 start_ticks_ = ticks; | 61 start_ticks_ = ticks; |
61 DCHECK(IsStarted()); | 62 DCHECK(IsStarted()); |
62 return elapsed; | 63 return elapsed; |
63 } | 64 } |
64 | 65 |
65 // Returns the time elapsed since the previous start. This method may only | 66 // Returns the time elapsed since the previous start. This method may only |
66 // be called on a previously started timer. | 67 // be called on a previously started timer. |
67 TimeDelta Elapsed() const { | 68 TimeDelta Elapsed() const { |
68 DCHECK(IsStarted()); | 69 DCHECK(IsStarted()); |
69 TimeDelta elapsed = Now() - start_ticks_; | 70 TimeDelta elapsed = Now() - start_ticks_; |
70 DCHECK(elapsed.InMicroseconds() >= 0); | 71 DCHECK(elapsed.InMicroseconds() >= 0); |
71 return elapsed; | 72 return elapsed; |
72 } | 73 } |
73 | 74 |
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 | |
74 // Returns |true| if the specified |time_delta| has elapsed since the | 83 // Returns |true| if the specified |time_delta| has elapsed since the |
75 // previous start, or |false| if not. This method may only be called on | 84 // previous start, or |false| if not. This method may only be called on |
76 // a previously started timer. | 85 // a previously started timer. |
77 bool HasExpired(TimeDelta time_delta) const { | 86 bool HasExpired(TimeDelta time_delta) const { |
78 DCHECK(IsStarted()); | 87 DCHECK(IsStarted()); |
79 return Elapsed() >= time_delta; | 88 return Elapsed() >= time_delta; |
80 } | 89 } |
81 | 90 |
82 private: | 91 private: |
83 static V8_INLINE TimeTicks Now() { | 92 static V8_INLINE TimeTicks Now() { |
84 TimeTicks now = TimeTicks::HighResolutionNow(); | 93 TimeTicks now = TimeTicks::HighResolutionNow(); |
85 DCHECK(!now.IsNull()); | 94 DCHECK(!now.IsNull()); |
86 return now; | 95 return now; |
87 } | 96 } |
88 | 97 |
89 TimeTicks start_ticks_; | 98 TimeTicks start_ticks_; |
90 #ifdef DEBUG | 99 #ifdef DEBUG |
91 bool started_; | 100 bool started_; |
92 #endif | 101 #endif |
93 }; | 102 }; |
94 | 103 |
95 } // namespace base | 104 } // namespace base |
96 } // namespace v8 | 105 } // namespace v8 |
97 | 106 |
98 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ | 107 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ |
OLD | NEW |