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
12 #include <mozilla/Types.h>
13 #include "mozilla/Assertions.h"
17 class AwakeTimeDuration
;
19 // Conceptually like mozilla::TimeStamp, but only increments when the device is
20 // awake, on all platforms, and with a restricted API.
22 // It is always valid, there is no way to acquire an AwakeTimeStamp that is not
23 // valid, unlike TimeStamp that can be null.
25 // Some arithmetic and ordering operations are supported, when they make sense.
27 // This timestamp shouldn't be considered to be high-resolution, and is suitable
28 // to measure time from a hundred of milliseconds (because of Windows
30 class AwakeTimeStamp
{
32 MFBT_API
static AwakeTimeStamp
NowLoRes();
33 MFBT_API
void operator+=(const AwakeTimeDuration
& aOther
);
34 MFBT_API
void operator-=(const AwakeTimeDuration
& aOther
);
35 MFBT_API
bool operator<(const AwakeTimeStamp
& aOther
) const {
36 return mValueUs
< aOther
.mValueUs
;
38 MFBT_API
bool operator<=(const AwakeTimeStamp
& aOther
) const {
39 return mValueUs
<= aOther
.mValueUs
;
41 MFBT_API
bool operator>=(const AwakeTimeStamp
& aOther
) const {
42 return mValueUs
>= aOther
.mValueUs
;
44 MFBT_API
bool operator>(const AwakeTimeStamp
& aOther
) const {
45 return mValueUs
> aOther
.mValueUs
;
47 MFBT_API
bool operator==(const AwakeTimeStamp
& aOther
) const {
48 return mValueUs
== aOther
.mValueUs
;
50 MFBT_API
bool operator!=(const AwakeTimeStamp
& aOther
) const {
51 return !(*this == aOther
);
53 MFBT_API AwakeTimeDuration
operator-(AwakeTimeStamp
const& aOther
) const;
54 MFBT_API AwakeTimeStamp
operator+(const AwakeTimeDuration
& aDuration
) const;
57 explicit AwakeTimeStamp(uint64_t aValueUs
) : mValueUs(aValueUs
) {}
62 // A duration, only counting the time the computer was awake.
64 // Can be obtained via subtracting two AwakeTimeStamp, or default-contructed to
65 // mean a empty duration.
67 // Arithmetic and ordering operations are defined when they make sense.
68 class AwakeTimeDuration
{
70 MFBT_API
AwakeTimeDuration() : mValueUs(0) {}
72 MFBT_API
double ToSeconds() const;
73 MFBT_API
double ToMilliseconds() const;
74 MFBT_API
double ToMicroseconds() const;
75 MFBT_API
void operator+=(const AwakeTimeDuration
& aDuration
) {
76 mValueUs
+= aDuration
.mValueUs
;
78 MFBT_API AwakeTimeDuration
operator+(const AwakeTimeDuration
& aOther
) const {
79 return AwakeTimeDuration(mValueUs
+ aOther
.mValueUs
);
81 MFBT_API AwakeTimeDuration
operator-(const AwakeTimeDuration
& aOther
) const {
82 MOZ_ASSERT(mValueUs
>= aOther
.mValueUs
);
83 return AwakeTimeDuration(mValueUs
- aOther
.mValueUs
);
85 MFBT_API
void operator-=(const AwakeTimeDuration
& aOther
) {
86 MOZ_ASSERT(mValueUs
>= aOther
.mValueUs
);
87 mValueUs
-= aOther
.mValueUs
;
89 MFBT_API
bool operator<(const AwakeTimeDuration
& aOther
) const {
90 return mValueUs
< aOther
.mValueUs
;
92 MFBT_API
bool operator<=(const AwakeTimeDuration
& aOther
) const {
93 return mValueUs
<= aOther
.mValueUs
;
95 MFBT_API
bool operator>=(const AwakeTimeDuration
& aOther
) const {
96 return mValueUs
>= aOther
.mValueUs
;
98 MFBT_API
bool operator>(const AwakeTimeDuration
& aOther
) const {
99 return mValueUs
> aOther
.mValueUs
;
101 MFBT_API
bool operator==(const AwakeTimeDuration
& aOther
) const {
102 return mValueUs
== aOther
.mValueUs
;
104 MFBT_API
bool operator!=(const AwakeTimeDuration
& aOther
) const {
105 return !(*this == aOther
);
109 friend AwakeTimeStamp
;
110 // Not using a default value because we want this private, but allow creating
111 // duration that are empty.
112 explicit AwakeTimeDuration(uint64_t aValueUs
) : mValueUs(aValueUs
) {}
117 }; // namespace mozilla
119 #endif // mozilla_AwakeTimeStamp_h