summaryrefslogtreecommitdiff
path: root/base/timer/elapsed_timer.cc
blob: 4c71b902e3a05043d6742cd8e0fa7ec17083c5bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/timer/elapsed_timer.h"

#include "base/check.h"

namespace base {

namespace {
bool g_mock_elapsed_timers_for_test = false;
}  // namespace

ElapsedTimer::ElapsedTimer() : start_time_(TimeTicks::Now()) {}

ElapsedTimer::ElapsedTimer(ElapsedTimer&& other)
    : start_time_(other.start_time_) {}

void ElapsedTimer::operator=(ElapsedTimer&& other) {
  start_time_ = other.start_time_;
}

TimeDelta ElapsedTimer::Elapsed() const {
  if (g_mock_elapsed_timers_for_test)
    return ScopedMockElapsedTimersForTest::kMockElapsedTime;
  return TimeTicks::Now() - start_time_;
}

ElapsedThreadTimer::ElapsedThreadTimer()
    : is_supported_(ThreadTicks::IsSupported()),
      begin_(is_supported_ ? ThreadTicks::Now() : ThreadTicks()) {}

TimeDelta ElapsedThreadTimer::Elapsed() const {
  if (!is_supported_)
    return TimeDelta();
  if (g_mock_elapsed_timers_for_test)
    return ScopedMockElapsedTimersForTest::kMockElapsedTime;
  return ThreadTicks::Now() - begin_;
}

// static
constexpr TimeDelta ScopedMockElapsedTimersForTest::kMockElapsedTime;

ScopedMockElapsedTimersForTest::ScopedMockElapsedTimersForTest() {
  DCHECK(!g_mock_elapsed_timers_for_test);
  g_mock_elapsed_timers_for_test = true;
}

ScopedMockElapsedTimersForTest::~ScopedMockElapsedTimersForTest() {
  DCHECK(g_mock_elapsed_timers_for_test);
  g_mock_elapsed_timers_for_test = false;
}

}  // namespace base