blob: 0e227bd4dbcf252a5618c4ae536211d00f0d2e62 [file] [log] [blame]
jar@chromium.org3d6a82f2011-11-05 09:39:27 +09001// Copyright (c) 2011 The Chromium 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#include "base/profiler/tracked_time.h"
6
7#include "build/build_config.h"
8
9#if defined(OS_WIN)
10#include <mmsystem.h> // Declare timeGetTime()... after including build_config.
11#endif
12
13namespace tracked_objects {
14
15#if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS)
16
17Duration::Duration() : ms_(0) {}
18Duration::Duration(int32 duration) : ms_(duration) {}
19
20Duration& Duration::operator+=(const Duration& other) {
21 ms_ += other.ms_;
22 return *this;
23}
24
25Duration Duration::operator+(const Duration& other) const {
26 return Duration(ms_ + other.ms_);
27}
28
29bool Duration::operator==(const Duration& other) const {
30 return ms_ == other.ms_;
31}
32
33bool Duration::operator!=(const Duration& other) const {
34 return ms_ != other.ms_;
35}
36
37bool Duration::operator>(const Duration& other) const {
38 return ms_ > other.ms_;
39}
40
41// static
42Duration Duration::FromMilliseconds(int ms) { return Duration(ms); }
43
44int32 Duration::InMilliseconds() const { return ms_; }
45
46//------------------------------------------------------------------------------
47
48TrackedTime::TrackedTime() : ms_(0) {}
49TrackedTime::TrackedTime(int32 ms) : ms_(ms) {}
50TrackedTime::TrackedTime(const base::TimeTicks& time)
51 : ms_((time - base::TimeTicks()).InMilliseconds()) {
52}
53
54// static
55TrackedTime TrackedTime::Now() {
56#if defined(OS_WIN)
57 // Use lock-free accessor to 32 bit time.
58 // Note that TimeTicks::Now() is built on this, so we have "compatible"
59 // times when we down-convert a TimeTicks sample.
60 // TODO(jar): Surface this interface via something in base/time.h.
61 return TrackedTime(static_cast<int32>(timeGetTime()));
62#else
63 // Posix has nice cheap 64 bit times, so we just down-convert it.
64 return TrackedTime(base::TimeTicks::Now());
65#endif // OS_WIN
66}
67
68Duration TrackedTime::operator-(const TrackedTime& other) const {
69 return Duration(ms_ - other.ms_);
70}
71
72TrackedTime TrackedTime::operator+(const Duration& other) const {
73 return TrackedTime(ms_ + other.ms_);
74}
75
76bool TrackedTime::is_null() const { return ms_ == 0; }
77
78#endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS
79
80} // namespace tracked_objects