Debian Codegraph @

mozglue/misc/AwakeTimeStamp.h

raw
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/. */
6
7 #ifndef mozilla_AwakeTimeStamp_h
8 #define mozilla_AwakeTimeStamp_h
9
10 #include <stdint.h>
11 #include <mozilla/Types.h>
12 #include "mozilla/Assertions.h"
13
14 namespace mozilla {
15
16 class AwakeTimeDuration;
17
18 // Conceptually like mozilla::TimeStamp, but only increments when the device is
19 // awake, on all platforms, and with a restricted API.
20 //
21 // It is always valid, there is no way to acquire an AwakeTimeStamp that is not
22 // valid, unlike TimeStamp that can be null.
23 //
24 // Some arithmetic and ordering operations are supported, when they make sense.
25 //
26 // When using NowLoRes(), the timestamp shouldn't be considered to be
27 // high-resolution, and is suitable to measure time from a hundred of
28 // milliseconds (because of Windows limitations).
29 // Now() can be a bit more expensive on Windows, and is precise. Both
30 // methods are equivalent on non-Windows.
31 class AwakeTimeStamp {
32 public:
33 using DurationType = AwakeTimeDuration;
34 MFBT_API static AwakeTimeStamp NowLoRes();
35 MFBT_API static AwakeTimeStamp Now();
36 MFBT_API void operator+=(const AwakeTimeDuration& aOther);
37 MFBT_API void operator-=(const AwakeTimeDuration& aOther);
38 MFBT_API bool operator<(const AwakeTimeStamp& aOther) const {
39 return mValueUs < aOther.mValueUs;
40 }
41 MFBT_API bool operator<=(const AwakeTimeStamp& aOther) const {
42 return mValueUs <= aOther.mValueUs;
43 }
44 MFBT_API bool operator>=(const AwakeTimeStamp& aOther) const {
45 return mValueUs >= aOther.mValueUs;
46 }
47 MFBT_API bool operator>(const AwakeTimeStamp& aOther) const {
48 return mValueUs > aOther.mValueUs;
49 }
50 MFBT_API bool operator==(const AwakeTimeStamp& aOther) const {
51 return mValueUs == aOther.mValueUs;
52 }
53 MFBT_API bool operator!=(const AwakeTimeStamp& aOther) const {
54 return !(*this == aOther);
55 }
56 MFBT_API AwakeTimeDuration operator-(AwakeTimeStamp const& aOther) const;
57 MFBT_API AwakeTimeStamp operator-(AwakeTimeDuration const& aOther) const;
58 MFBT_API AwakeTimeStamp operator+(const AwakeTimeDuration& aDuration) const;
59
60 private:
61 explicit AwakeTimeStamp(uint64_t aValueUs) : mValueUs(aValueUs) {}
62
63 uint64_t mValueUs;
64 };
65
66 // A duration, only counting the time the computer was awake.
67 //
68 // Can be obtained via subtracting two AwakeTimeStamp, or default-contructed to
69 // mean a empty duration.
70 //
71 // Arithmetic and ordering operations are defined when they make sense.
72 class AwakeTimeDuration {
73 public:
74 MFBT_API AwakeTimeDuration() : mValueUs(0) {}
75
76 MFBT_API double ToSeconds() const;
77 MFBT_API double ToMilliseconds() const;
78 MFBT_API double ToMicroseconds() const;
79 static MFBT_API AwakeTimeDuration FromSeconds(uint64_t aSeconds);
80 static MFBT_API AwakeTimeDuration FromMilliseconds(uint64_t aMilliseconds);
81 static MFBT_API AwakeTimeDuration FromMicroseconds(uint64_t aMicroseconds);
82 MFBT_API void operator+=(const AwakeTimeDuration& aDuration) {
83 mValueUs += aDuration.mValueUs;
84 }
85 MFBT_API AwakeTimeDuration operator+(const AwakeTimeDuration& aOther) const {
86 return AwakeTimeDuration(mValueUs + aOther.mValueUs);
87 }
88 MFBT_API AwakeTimeDuration operator-(const AwakeTimeDuration& aOther) const {
89 MOZ_ASSERT(mValueUs >= aOther.mValueUs);
90 return AwakeTimeDuration(mValueUs - aOther.mValueUs);
91 }
92 MFBT_API void operator-=(const AwakeTimeDuration& aOther) {
93 MOZ_ASSERT(mValueUs >= aOther.mValueUs);
94 mValueUs -= aOther.mValueUs;
95 }
96 MFBT_API bool operator<(const AwakeTimeDuration& aOther) const {
97 return mValueUs < aOther.mValueUs;
98 }
99 MFBT_API bool operator<=(const AwakeTimeDuration& aOther) const {
100 return mValueUs <= aOther.mValueUs;
101 }
102 MFBT_API bool operator>=(const AwakeTimeDuration& aOther) const {
103 return mValueUs >= aOther.mValueUs;
104 }
105 MFBT_API bool operator>(const AwakeTimeDuration& aOther) const {
106 return mValueUs > aOther.mValueUs;
107 }
108 MFBT_API bool operator==(const AwakeTimeDuration& aOther) const {
109 return mValueUs == aOther.mValueUs;
110 }
111 MFBT_API bool operator!=(const AwakeTimeDuration& aOther) const {
112 return !(*this == aOther);
113 }
114
115 private:
116 friend AwakeTimeStamp;
117 // Not using a default value because we want this private, but allow creating
118 // duration that are empty.
119 explicit AwakeTimeDuration(uint64_t aValueUs) : mValueUs(aValueUs) {}
120
121 uint64_t mValueUs;
122 };
123
124 }; // namespace mozilla
125
126 #endif // mozilla_AwakeTimeStamp_h