From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- mozglue/misc/AwakeTimeStamp.cpp | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 mozglue/misc/AwakeTimeStamp.cpp (limited to 'mozglue/misc/AwakeTimeStamp.cpp') diff --git a/mozglue/misc/AwakeTimeStamp.cpp b/mozglue/misc/AwakeTimeStamp.cpp new file mode 100644 index 0000000000..a5c8ed4abd --- /dev/null +++ b/mozglue/misc/AwakeTimeStamp.cpp @@ -0,0 +1,93 @@ +/* -*- 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/. */ + +#include "AwakeTimeStamp.h" + +#ifdef XP_WIN +# include +#endif + +#include "mozilla/Assertions.h" +#include "mozilla/DebugOnly.h" + +namespace mozilla { + +static constexpr uint64_t kUSperS = 1000000; +static constexpr uint64_t kUSperMS = 1000; +#ifndef XP_WIN +static constexpr uint64_t kNSperUS = 1000; +#endif + +double AwakeTimeDuration::ToSeconds() const { + return static_cast(mValueUs) / kUSperS; +} +double AwakeTimeDuration::ToMilliseconds() const { + return static_cast(mValueUs) / kUSperMS; +} +double AwakeTimeDuration::ToMicroseconds() const { + return static_cast(mValueUs); +} + +AwakeTimeDuration AwakeTimeStamp::operator-( + AwakeTimeStamp const& aOther) const { + return AwakeTimeDuration(mValueUs - aOther.mValueUs); +} + +AwakeTimeStamp AwakeTimeStamp::operator+( + const AwakeTimeDuration& aDuration) const { + return AwakeTimeStamp(mValueUs + aDuration.mValueUs); +} + +void AwakeTimeStamp::operator+=(const AwakeTimeDuration& aOther) { + mValueUs += aOther.mValueUs; +} + +void AwakeTimeStamp::operator-=(const AwakeTimeDuration& aOther) { + MOZ_ASSERT(mValueUs >= aOther.mValueUs); + mValueUs -= aOther.mValueUs; +} + +// Apple things +#if defined(__APPLE__) && defined(__MACH__) +# include +# include +# include +# include + +AwakeTimeStamp AwakeTimeStamp::NowLoRes() { + return AwakeTimeStamp(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / kNSperUS); +} + +#elif defined(XP_WIN) + +// Number of hundreds of nanoseconds in a microsecond +static constexpr uint64_t kHNSperUS = 10; + +AwakeTimeStamp AwakeTimeStamp::NowLoRes() { + ULONGLONG interrupt_time; + DebugOnly rv = QueryUnbiasedInterruptTime(&interrupt_time); + MOZ_ASSERT(rv); + + return AwakeTimeStamp(interrupt_time / kHNSperUS); +} + +#else // Linux and other POSIX but not macOS +# include + +uint64_t TimespecToMicroseconds(struct timespec aTs) { + return aTs.tv_sec * kUSperS + aTs.tv_nsec / kNSperUS; +} + +AwakeTimeStamp AwakeTimeStamp::NowLoRes() { + struct timespec ts = {0}; + DebugOnly rv = clock_gettime(CLOCK_MONOTONIC, &ts); + MOZ_ASSERT(!rv); + return AwakeTimeStamp(TimespecToMicroseconds(ts)); +} + +#endif + +}; // namespace mozilla -- cgit v1.2.3