Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: src/platform/elapsed-timer.h

Issue 358363002: Move platform abstraction to base library (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: updates Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef V8_PLATFORM_ELAPSED_TIMER_H_
6 #define V8_PLATFORM_ELAPSED_TIMER_H_
7
8 #include "src/checks.h"
9 #include "src/platform/time.h"
10
11 namespace v8 {
12 namespace internal {
13
14 class ElapsedTimer V8_FINAL BASE_EMBEDDED {
15 public:
16 #ifdef DEBUG
17 ElapsedTimer() : started_(false) {}
18 #endif
19
20 // Starts this timer. Once started a timer can be checked with
21 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|.
22 // This method must not be called on an already started timer.
23 void Start() {
24 ASSERT(!IsStarted());
25 start_ticks_ = Now();
26 #ifdef DEBUG
27 started_ = true;
28 #endif
29 ASSERT(IsStarted());
30 }
31
32 // Stops this timer. Must not be called on a timer that was not
33 // started before.
34 void Stop() {
35 ASSERT(IsStarted());
36 start_ticks_ = TimeTicks();
37 #ifdef DEBUG
38 started_ = false;
39 #endif
40 ASSERT(!IsStarted());
41 }
42
43 // Returns |true| if this timer was started previously.
44 bool IsStarted() const {
45 ASSERT(started_ || start_ticks_.IsNull());
46 ASSERT(!started_ || !start_ticks_.IsNull());
47 return !start_ticks_.IsNull();
48 }
49
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()|
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
54 // on a previously started timer.
55 TimeDelta Restart() {
56 ASSERT(IsStarted());
57 TimeTicks ticks = Now();
58 TimeDelta elapsed = ticks - start_ticks_;
59 ASSERT(elapsed.InMicroseconds() >= 0);
60 start_ticks_ = ticks;
61 ASSERT(IsStarted());
62 return elapsed;
63 }
64
65 // Returns the time elapsed since the previous start. This method may only
66 // be called on a previously started timer.
67 TimeDelta Elapsed() const {
68 ASSERT(IsStarted());
69 TimeDelta elapsed = Now() - start_ticks_;
70 ASSERT(elapsed.InMicroseconds() >= 0);
71 return elapsed;
72 }
73
74 // 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
76 // a previously started timer.
77 bool HasExpired(TimeDelta time_delta) const {
78 ASSERT(IsStarted());
79 return Elapsed() >= time_delta;
80 }
81
82 private:
83 static V8_INLINE TimeTicks Now() {
84 TimeTicks now = TimeTicks::HighResolutionNow();
85 ASSERT(!now.IsNull());
86 return now;
87 }
88
89 TimeTicks start_ticks_;
90 #ifdef DEBUG
91 bool started_;
92 #endif
93 };
94
95 } } // namespace v8::internal
96
97 #endif // V8_PLATFORM_ELAPSED_TIMER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698