Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / mozglue / misc / AwakeTimeStamp.cpp
bloba67c3e49a8aa3c6fa4515cec6313db60b65236d5
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 #include "AwakeTimeStamp.h"
9 #ifdef XP_WIN
10 # include <windows.h>
11 #endif
13 #include "mozilla/Assertions.h"
14 #include "mozilla/DebugOnly.h"
16 namespace mozilla {
18 static constexpr uint64_t kUSperS = 1000000;
19 static constexpr uint64_t kUSperMS = 1000;
20 #ifndef XP_WIN
21 static constexpr uint64_t kNSperUS = 1000;
22 #endif
24 double AwakeTimeDuration::ToSeconds() const {
25 return static_cast<double>(mValueUs) / kUSperS;
27 double AwakeTimeDuration::ToMilliseconds() const {
28 return static_cast<double>(mValueUs) / kUSperMS;
30 double AwakeTimeDuration::ToMicroseconds() const {
31 return static_cast<double>(mValueUs);
34 AwakeTimeDuration AwakeTimeDuration::FromSeconds(uint64_t aSeconds) {
35 return AwakeTimeDuration(aSeconds * 1000000);
37 AwakeTimeDuration AwakeTimeDuration::FromMilliseconds(uint64_t aMilliseconds) {
38 return AwakeTimeDuration(aMilliseconds * 1000);
40 AwakeTimeDuration AwakeTimeDuration::FromMicroseconds(uint64_t aMicroseconds) {
41 return AwakeTimeDuration(aMicroseconds);
44 AwakeTimeDuration AwakeTimeStamp::operator-(
45 AwakeTimeStamp const& aOther) const {
46 return AwakeTimeDuration(mValueUs - aOther.mValueUs);
49 AwakeTimeStamp AwakeTimeStamp::operator-(
50 AwakeTimeDuration const& aOther) const {
51 return AwakeTimeStamp(mValueUs - aOther.mValueUs);
54 AwakeTimeStamp AwakeTimeStamp::operator+(
55 const AwakeTimeDuration& aDuration) const {
56 return AwakeTimeStamp(mValueUs + aDuration.mValueUs);
59 void AwakeTimeStamp::operator+=(const AwakeTimeDuration& aOther) {
60 mValueUs += aOther.mValueUs;
63 void AwakeTimeStamp::operator-=(const AwakeTimeDuration& aOther) {
64 MOZ_ASSERT(mValueUs >= aOther.mValueUs);
65 mValueUs -= aOther.mValueUs;
68 // Apple things
69 #if defined(__APPLE__) && defined(__MACH__)
70 # include <time.h>
71 # include <sys/time.h>
72 # include <sys/types.h>
73 # include <mach/mach_time.h>
75 AwakeTimeStamp AwakeTimeStamp::Now() {
76 return AwakeTimeStamp(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / kNSperUS);
79 AwakeTimeStamp AwakeTimeStamp::NowLoRes() { return Now(); }
81 #elif defined(XP_WIN)
83 // Number of hundreds of nanoseconds in a microsecond
84 static constexpr uint64_t kHNSperUS = 10;
86 AwakeTimeStamp AwakeTimeStamp::NowLoRes() {
87 ULONGLONG interrupt_time;
88 DebugOnly<bool> rv = QueryUnbiasedInterruptTime(&interrupt_time);
89 MOZ_ASSERT(rv);
91 return AwakeTimeStamp(interrupt_time / kHNSperUS);
94 AwakeTimeStamp AwakeTimeStamp::Now() {
95 ULONGLONG interrupt_time;
96 QueryUnbiasedInterruptTimePrecise(&interrupt_time);
98 return AwakeTimeStamp(interrupt_time / kHNSperUS);
101 #else // Linux and other POSIX but not macOS
102 # include <time.h>
104 uint64_t TimespecToMicroseconds(struct timespec aTs) {
105 return aTs.tv_sec * kUSperS + aTs.tv_nsec / kNSperUS;
108 AwakeTimeStamp AwakeTimeStamp::Now() {
109 struct timespec ts = {0};
110 DebugOnly<int> rv = clock_gettime(CLOCK_MONOTONIC, &ts);
111 MOZ_ASSERT(!rv);
112 return AwakeTimeStamp(TimespecToMicroseconds(ts));
115 AwakeTimeStamp AwakeTimeStamp::NowLoRes() { return Now(); }
117 #endif
119 }; // namespace mozilla