elapsed-timer.h 2.66 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_BASE_PLATFORM_ELAPSED_TIMER_H_
#define V8_BASE_PLATFORM_ELAPSED_TIMER_H_
7

8 9
#include "src/base/logging.h"
#include "src/base/platform/time.h"
10 11

namespace v8 {
12
namespace base {
13

14
class ElapsedTimer final {
15 16 17 18 19 20 21 22 23
 public:
#ifdef DEBUG
  ElapsedTimer() : started_(false) {}
#endif

  // Starts this timer. Once started a timer can be checked with
  // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|.
  // This method must not be called on an already started timer.
  void Start() {
24
    DCHECK(!IsStarted());
25 26 27 28
    start_ticks_ = Now();
#ifdef DEBUG
    started_ = true;
#endif
29
    DCHECK(IsStarted());
30 31 32 33 34
  }

  // Stops this timer. Must not be called on a timer that was not
  // started before.
  void Stop() {
35
    DCHECK(IsStarted());
36 37 38 39
    start_ticks_ = TimeTicks();
#ifdef DEBUG
    started_ = false;
#endif
40
    DCHECK(!IsStarted());
41 42 43 44
  }

  // Returns |true| if this timer was started previously.
  bool IsStarted() const {
45 46
    DCHECK(started_ || start_ticks_.IsNull());
    DCHECK(!started_ || !start_ticks_.IsNull());
47 48 49 50 51 52 53 54
    return !start_ticks_.IsNull();
  }

  // Restarts the timer and returns the time elapsed since the previous start.
  // This method is equivalent to obtaining the elapsed time with |Elapsed()|
  // and then starting the timer again, but does so in one single operation,
  // avoiding the need to obtain the clock value twice. It may only be called
  // on a previously started timer.
55
  TimeDelta Restart() {
56
    DCHECK(IsStarted());
57 58
    TimeTicks ticks = Now();
    TimeDelta elapsed = ticks - start_ticks_;
59
    DCHECK_GE(elapsed.InMicroseconds(), 0);
60
    start_ticks_ = ticks;
61
    DCHECK(IsStarted());
62 63 64 65 66
    return elapsed;
  }

  // Returns the time elapsed since the previous start. This method may only
  // be called on a previously started timer.
67
  TimeDelta Elapsed() const {
68
    DCHECK(IsStarted());
69
    TimeDelta elapsed = Now() - start_ticks_;
70
    DCHECK_GE(elapsed.InMicroseconds(), 0);
71 72 73 74 75 76
    return elapsed;
  }

  // Returns |true| if the specified |time_delta| has elapsed since the
  // previous start, or |false| if not. This method may only be called on
  // a previously started timer.
77
  bool HasExpired(TimeDelta time_delta) const {
78
    DCHECK(IsStarted());
79 80 81
    return Elapsed() >= time_delta;
  }

82
 private:
83
  static V8_INLINE TimeTicks Now() {
84
    TimeTicks now = TimeTicks::HighResolutionNow();
85
    DCHECK(!now.IsNull());
86 87 88 89 90 91 92 93 94
    return now;
  }

  TimeTicks start_ticks_;
#ifdef DEBUG
  bool started_;
#endif
};

95 96
}  // namespace base
}  // namespace v8
97

98
#endif  // V8_BASE_PLATFORM_ELAPSED_TIMER_H_