1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_AwakeTimeStamp_h
8 #define mozilla_AwakeTimeStamp_h
11 #include <mozilla/Types.h>
12 #include "mozilla/Assertions.h"
16 class AwakeTimeDuration
;
18 // Conceptually like mozilla::TimeStamp, but only increments when the device is
19 // awake, on all platforms, and with a restricted API.
21 // It is always valid, there is no way to acquire an AwakeTimeStamp that is not
22 // valid, unlike TimeStamp that can be null.
24 // Some arithmetic and ordering operations are supported, when they make sense.
26 // This timestamp shouldn't be considered to be high-resolution, and is suitable
27 // to measure time from a hundred of milliseconds (because of Windows
29 class AwakeTimeStamp
{
31 MFBT_API
static AwakeTimeStamp
NowLoRes();
32 MFBT_API
void operator+=(const AwakeTimeDuration
& aOther
);
33 MFBT_API
void operator-=(const AwakeTimeDuration
& aOther
);
34 MFBT_API
bool operator<(const AwakeTimeStamp
& aOther
) const {
35 return mValueUs
< aOther
.mValueUs
;
37 MFBT_API
bool operator<=(const AwakeTimeStamp
& aOther
) const {
38 return mValueUs
<= aOther
.mValueUs
;
40 MFBT_API
bool operator>=(const AwakeTimeStamp
& aOther
) const {
41 return mValueUs
>= aOther
.mValueUs
;
43 MFBT_API
bool operator>(const AwakeTimeStamp
& aOther
) const {
44 return mValueUs
> aOther
.mValueUs
;
46 MFBT_API
bool operator==(const AwakeTimeStamp
& aOther
) const {
47 return mValueUs
== aOther
.mValueUs
;
49 MFBT_API
bool operator!=(const AwakeTimeStamp
& aOther
) const {
50 return !(*this == aOther
);
52 MFBT_API AwakeTimeDuration
operator-(AwakeTimeStamp
const& aOther
) const;
53 MFBT_API AwakeTimeStamp
operator+(const AwakeTimeDuration
& aDuration
) const;
56 explicit AwakeTimeStamp(uint64_t aValueUs
) : mValueUs(aValueUs
) {}
61 // A duration, only counting the time the computer was awake.
63 // Can be obtained via subtracting two AwakeTimeStamp, or default-contructed to
64 // mean a empty duration.
66 // Arithmetic and ordering operations are defined when they make sense.
67 class AwakeTimeDuration
{
69 MFBT_API
AwakeTimeDuration() : mValueUs(0) {}
71 MFBT_API
double ToSeconds() const;
72 MFBT_API
double ToMilliseconds() const;
73 MFBT_API
double ToMicroseconds() const;
74 MFBT_API
void operator+=(const AwakeTimeDuration
& aDuration
) {
75 mValueUs
+= aDuration
.mValueUs
;
77 MFBT_API AwakeTimeDuration
operator+(const AwakeTimeDuration
& aOther
) const {
78 return AwakeTimeDuration(mValueUs
+ aOther
.mValueUs
);
80 MFBT_API AwakeTimeDuration
operator-(const AwakeTimeDuration
& aOther
) const {
81 MOZ_ASSERT(mValueUs
>= aOther
.mValueUs
);
82 return AwakeTimeDuration(mValueUs
- aOther
.mValueUs
);
84 MFBT_API
void operator-=(const AwakeTimeDuration
& aOther
) {
85 MOZ_ASSERT(mValueUs
>= aOther
.mValueUs
);
86 mValueUs
-= aOther
.mValueUs
;
88 MFBT_API
bool operator<(const AwakeTimeDuration
& aOther
) const {
89 return mValueUs
< aOther
.mValueUs
;
91 MFBT_API
bool operator<=(const AwakeTimeDuration
& aOther
) const {
92 return mValueUs
<= aOther
.mValueUs
;
94 MFBT_API
bool operator>=(const AwakeTimeDuration
& aOther
) const {
95 return mValueUs
>= aOther
.mValueUs
;
97 MFBT_API
bool operator>(const AwakeTimeDuration
& aOther
) const {
98 return mValueUs
> aOther
.mValueUs
;
100 MFBT_API
bool operator==(const AwakeTimeDuration
& aOther
) const {
101 return mValueUs
== aOther
.mValueUs
;
103 MFBT_API
bool operator!=(const AwakeTimeDuration
& aOther
) const {
104 return !(*this == aOther
);
108 friend AwakeTimeStamp
;
109 // Not using a default value because we want this private, but allow creating
110 // duration that are empty.
111 explicit AwakeTimeDuration(uint64_t aValueUs
) : mValueUs(aValueUs
) {}
116 }; // namespace mozilla
118 #endif // mozilla_AwakeTimeStamp_h