summaryrefslogtreecommitdiffstats
path: root/mozglue/misc/AwakeTimeStamp.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /mozglue/misc/AwakeTimeStamp.h
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mozglue/misc/AwakeTimeStamp.h')
-rw-r--r--mozglue/misc/AwakeTimeStamp.h118
1 files changed, 118 insertions, 0 deletions
diff --git a/mozglue/misc/AwakeTimeStamp.h b/mozglue/misc/AwakeTimeStamp.h
new file mode 100644
index 0000000000..b95ef9e77e
--- /dev/null
+++ b/mozglue/misc/AwakeTimeStamp.h
@@ -0,0 +1,118 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_AwakeTimeStamp_h
+#define mozilla_AwakeTimeStamp_h
+
+#include <stdint.h>
+#include <mozilla/Types.h>
+#include "mozilla/Assertions.h"
+
+namespace mozilla {
+
+class AwakeTimeDuration;
+
+// Conceptually like mozilla::TimeStamp, but only increments when the device is
+// awake, on all platforms, and with a restricted API.
+//
+// It is always valid, there is no way to acquire an AwakeTimeStamp that is not
+// valid, unlike TimeStamp that can be null.
+//
+// Some arithmetic and ordering operations are supported, when they make sense.
+//
+// This timestamp shouldn't be considered to be high-resolution, and is suitable
+// to measure time from a hundred of milliseconds (because of Windows
+// limitations).
+class AwakeTimeStamp {
+ public:
+ MFBT_API static AwakeTimeStamp NowLoRes();
+ MFBT_API void operator+=(const AwakeTimeDuration& aOther);
+ MFBT_API void operator-=(const AwakeTimeDuration& aOther);
+ MFBT_API bool operator<(const AwakeTimeStamp& aOther) const {
+ return mValueUs < aOther.mValueUs;
+ }
+ MFBT_API bool operator<=(const AwakeTimeStamp& aOther) const {
+ return mValueUs <= aOther.mValueUs;
+ }
+ MFBT_API bool operator>=(const AwakeTimeStamp& aOther) const {
+ return mValueUs >= aOther.mValueUs;
+ }
+ MFBT_API bool operator>(const AwakeTimeStamp& aOther) const {
+ return mValueUs > aOther.mValueUs;
+ }
+ MFBT_API bool operator==(const AwakeTimeStamp& aOther) const {
+ return mValueUs == aOther.mValueUs;
+ }
+ MFBT_API bool operator!=(const AwakeTimeStamp& aOther) const {
+ return !(*this == aOther);
+ }
+ MFBT_API AwakeTimeDuration operator-(AwakeTimeStamp const& aOther) const;
+ MFBT_API AwakeTimeStamp operator+(const AwakeTimeDuration& aDuration) const;
+
+ private:
+ explicit AwakeTimeStamp(uint64_t aValueUs) : mValueUs(aValueUs) {}
+
+ uint64_t mValueUs;
+};
+
+// A duration, only counting the time the computer was awake.
+//
+// Can be obtained via subtracting two AwakeTimeStamp, or default-contructed to
+// mean a empty duration.
+//
+// Arithmetic and ordering operations are defined when they make sense.
+class AwakeTimeDuration {
+ public:
+ MFBT_API AwakeTimeDuration() : mValueUs(0) {}
+
+ MFBT_API double ToSeconds() const;
+ MFBT_API double ToMilliseconds() const;
+ MFBT_API double ToMicroseconds() const;
+ MFBT_API void operator+=(const AwakeTimeDuration& aDuration) {
+ mValueUs += aDuration.mValueUs;
+ }
+ MFBT_API AwakeTimeDuration operator+(const AwakeTimeDuration& aOther) const {
+ return AwakeTimeDuration(mValueUs + aOther.mValueUs);
+ }
+ MFBT_API AwakeTimeDuration operator-(const AwakeTimeDuration& aOther) const {
+ MOZ_ASSERT(mValueUs >= aOther.mValueUs);
+ return AwakeTimeDuration(mValueUs - aOther.mValueUs);
+ }
+ MFBT_API void operator-=(const AwakeTimeDuration& aOther) {
+ MOZ_ASSERT(mValueUs >= aOther.mValueUs);
+ mValueUs -= aOther.mValueUs;
+ }
+ MFBT_API bool operator<(const AwakeTimeDuration& aOther) const {
+ return mValueUs < aOther.mValueUs;
+ }
+ MFBT_API bool operator<=(const AwakeTimeDuration& aOther) const {
+ return mValueUs <= aOther.mValueUs;
+ }
+ MFBT_API bool operator>=(const AwakeTimeDuration& aOther) const {
+ return mValueUs >= aOther.mValueUs;
+ }
+ MFBT_API bool operator>(const AwakeTimeDuration& aOther) const {
+ return mValueUs > aOther.mValueUs;
+ }
+ MFBT_API bool operator==(const AwakeTimeDuration& aOther) const {
+ return mValueUs == aOther.mValueUs;
+ }
+ MFBT_API bool operator!=(const AwakeTimeDuration& aOther) const {
+ return !(*this == aOther);
+ }
+
+ private:
+ friend AwakeTimeStamp;
+ // Not using a default value because we want this private, but allow creating
+ // duration that are empty.
+ explicit AwakeTimeDuration(uint64_t aValueUs) : mValueUs(aValueUs) {}
+
+ uint64_t mValueUs;
+};
+
+}; // namespace mozilla
+
+#endif // mozilla_AwakeTimeStamp_h