OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_PLATFORM_ELAPSED_TIMER_H_ |
| 29 #define V8_PLATFORM_ELAPSED_TIMER_H_ |
| 30 |
| 31 #include "checks.h" |
| 32 #include "platform/time.h" |
| 33 |
| 34 namespace v8 { |
| 35 namespace internal { |
| 36 |
| 37 class ElapsedTimer V8_FINAL BASE_EMBEDDED { |
| 38 public: |
| 39 #ifdef DEBUG |
| 40 ElapsedTimer() : started_(false) {} |
| 41 #endif |
| 42 |
| 43 // Starts this timer. Once started a timer can be checked with |
| 44 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|. |
| 45 // This method must not be called on an already started timer. |
| 46 void Start() { |
| 47 ASSERT(!IsStarted()); |
| 48 start_ticks_ = Now(); |
| 49 #ifdef DEBUG |
| 50 started_ = true; |
| 51 #endif |
| 52 ASSERT(IsStarted()); |
| 53 } |
| 54 |
| 55 // Stops this timer. Must not be called on a timer that was not |
| 56 // started before. |
| 57 void Stop() { |
| 58 ASSERT(IsStarted()); |
| 59 start_ticks_ = TimeTicks(); |
| 60 #ifdef DEBUG |
| 61 started_ = false; |
| 62 #endif |
| 63 ASSERT(!IsStarted()); |
| 64 } |
| 65 |
| 66 // Returns |true| if this timer was started previously. |
| 67 bool IsStarted() const { |
| 68 ASSERT(started_ || start_ticks_.IsNull()); |
| 69 ASSERT(!started_ || !start_ticks_.IsNull()); |
| 70 return !start_ticks_.IsNull(); |
| 71 } |
| 72 |
| 73 // Restarts the timer and returns the time elapsed since the previous start. |
| 74 // This method is equivalent to obtaining the elapsed time with |Elapsed()| |
| 75 // and then starting the timer again, but does so in one single operation, |
| 76 // avoiding the need to obtain the clock value twice. It may only be called |
| 77 // on a previously started timer. |
| 78 TimeDelta Restart() { |
| 79 ASSERT(IsStarted()); |
| 80 TimeTicks ticks = Now(); |
| 81 TimeDelta elapsed = ticks - start_ticks_; |
| 82 ASSERT(elapsed.InMicroseconds() >= 0); |
| 83 start_ticks_ = ticks; |
| 84 ASSERT(IsStarted()); |
| 85 return elapsed; |
| 86 } |
| 87 |
| 88 // Returns the time elapsed since the previous start. This method may only |
| 89 // be called on a previously started timer. |
| 90 MUST_USE_RESULT TimeDelta Elapsed() const { |
| 91 ASSERT(IsStarted()); |
| 92 TimeDelta elapsed = Now() - start_ticks_; |
| 93 ASSERT(elapsed.InMicroseconds() >= 0); |
| 94 return elapsed; |
| 95 } |
| 96 |
| 97 // Returns |true| if the specified |time_delta| has elapsed since the |
| 98 // previous start, or |false| if not. This method may only be called on |
| 99 // a previously started timer. |
| 100 MUST_USE_RESULT bool HasExpired(TimeDelta time_delta) const { |
| 101 ASSERT(IsStarted()); |
| 102 return Elapsed() >= time_delta; |
| 103 } |
| 104 |
| 105 private: |
| 106 MUST_USE_RESULT V8_INLINE(static TimeTicks Now()) { |
| 107 TimeTicks now = TimeTicks::HighResNow(); |
| 108 ASSERT(!now.IsNull()); |
| 109 return now; |
| 110 } |
| 111 |
| 112 TimeTicks start_ticks_; |
| 113 #ifdef DEBUG |
| 114 bool started_; |
| 115 #endif |
| 116 }; |
| 117 |
| 118 } } // namespace v8::internal |
| 119 |
| 120 #endif // V8_PLATFORM_ELAPSED_TIMER_H_ |
OLD | NEW |