| 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_TimeStamp_windows_h |
| 8 |
#define mozilla_TimeStamp_windows_h |
| 9 |
|
| 10 |
#include "mozilla/Types.h" |
| 11 |
|
| 12 |
namespace mozilla { |
| 13 |
|
| 14 |
/** |
| 15 |
* The [mt] unit: |
| 16 |
* |
| 17 |
* Many values are kept in ticks of the Performance Counter x 1000, |
| 18 |
* further just referred as [mt], meaning milli-ticks. |
| 19 |
* |
| 20 |
* This is needed to preserve maximum precision of the performance frequency |
| 21 |
* representation. GetTickCount64 values in milliseconds are multiplied with |
| 22 |
* frequency per second. Therefore we need to multiply QPC value by 1000 to |
| 23 |
* have the same units to allow simple arithmentic with both QPC and GTC. |
| 24 |
*/ |
| 25 |
#define ms2mt(x) ((x) * mozilla::GetQueryPerformanceFrequencyPerSec()) |
| 26 |
#define mt2ms(x) ((x) / mozilla::GetQueryPerformanceFrequencyPerSec()) |
| 27 |
#define mt2ms_f(x) (double(x) / mozilla::GetQueryPerformanceFrequencyPerSec()) |
| 28 |
|
| 29 |
MFBT_API uint64_t GetQueryPerformanceFrequencyPerSec(); |
| 30 |
|
| 31 |
class TimeStamp; |
| 32 |
class TimeStampValue; |
| 33 |
class TimeStampValueTests; |
| 34 |
class TimeStampTests; |
| 35 |
|
| 36 |
TimeStampValue NowInternal(bool aHighResolution); |
| 37 |
|
| 38 |
class TimeStampValue { |
| 39 |
friend TimeStampValue NowInternal(bool); |
| 40 |
friend bool IsCanonicalTimeStamp(TimeStampValue); |
| 41 |
friend struct IPC::ParamTraits<mozilla::TimeStampValue>; |
| 42 |
friend class TimeStamp; |
| 43 |
friend class TimeStampValueTests; |
| 44 |
friend class TimeStampTests; |
| 45 |
|
| 46 |
// Both QPC and GTC are kept in [mt] units. |
| 47 |
uint64_t mGTC; |
| 48 |
uint64_t mQPC; |
| 49 |
|
| 50 |
bool mIsNull; |
| 51 |
bool mHasQPC; |
| 52 |
|
| 53 |
constexpr MFBT_API TimeStampValue(uint64_t aGTC, uint64_t aQPC, bool aHasQPC) |
| 54 |
: mGTC(aGTC), |
| 55 |
mQPC(aQPC), |
| 56 |
mIsNull(aGTC == 0 && aQPC == 0), |
| 57 |
mHasQPC(aHasQPC) {} |
| 58 |
|
| 59 |
// This constructor should be explicit but it is replacing a constructor that |
| 60 |
// was MOZ_IMPLICIT and there are many locations that are using the automatic |
| 61 |
// conversion. |
| 62 |
constexpr MOZ_IMPLICIT MFBT_API TimeStampValue(uint64_t aGTCAndQPC) |
| 63 |
: TimeStampValue(aGTCAndQPC, aGTCAndQPC, true) {} |
| 64 |
|
| 65 |
MFBT_API uint64_t CheckQPC(const TimeStampValue& aOther) const; |
| 66 |
|
| 67 |
public: |
| 68 |
MFBT_API uint64_t operator-(const TimeStampValue& aOther) const; |
| 69 |
|
| 70 |
TimeStampValue operator+(const int64_t aOther) const { |
| 71 |
return TimeStampValue(mGTC + aOther, mQPC + aOther, mHasQPC); |
| 72 |
} |
| 73 |
TimeStampValue operator-(const int64_t aOther) const { |
| 74 |
return TimeStampValue(mGTC - aOther, mQPC - aOther, mHasQPC); |
| 75 |
} |
| 76 |
MFBT_API TimeStampValue& operator+=(const int64_t aOther); |
| 77 |
MFBT_API TimeStampValue& operator-=(const int64_t aOther); |
| 78 |
|
| 79 |
constexpr bool operator<(const TimeStampValue& aOther) const { |
| 80 |
return mHasQPC && aOther.mHasQPC ? mQPC < aOther.mQPC : mGTC < aOther.mGTC; |
| 81 |
} |
| 82 |
constexpr bool operator>(const TimeStampValue& aOther) const { |
| 83 |
return mHasQPC && aOther.mHasQPC ? mQPC > aOther.mQPC : mGTC > aOther.mGTC; |
| 84 |
} |
| 85 |
constexpr bool operator<=(const TimeStampValue& aOther) const { |
| 86 |
return mHasQPC && aOther.mHasQPC ? mQPC <= aOther.mQPC |
| 87 |
: mGTC <= aOther.mGTC; |
| 88 |
} |
| 89 |
constexpr bool operator>=(const TimeStampValue& aOther) const { |
| 90 |
return mHasQPC && aOther.mHasQPC ? mQPC >= aOther.mQPC |
| 91 |
: mGTC >= aOther.mGTC; |
| 92 |
} |
| 93 |
constexpr bool operator==(const TimeStampValue& aOther) const { |
| 94 |
return mHasQPC && aOther.mHasQPC ? mQPC == aOther.mQPC |
| 95 |
: mGTC == aOther.mGTC; |
| 96 |
} |
| 97 |
constexpr bool operator!=(const TimeStampValue& aOther) const { |
| 98 |
return mHasQPC && aOther.mHasQPC ? mQPC != aOther.mQPC |
| 99 |
: mGTC != aOther.mGTC; |
| 100 |
} |
| 101 |
constexpr bool IsNull() const { return mIsNull; } |
| 102 |
|
| 103 |
#if defined(DEBUG) |
| 104 |
uint64_t GTC() const { return mGTC; } |
| 105 |
uint64_t QPC() const { return mQPC; } |
| 106 |
|
| 107 |
bool HasQPC() const { return mHasQPC; } |
| 108 |
#endif |
| 109 |
}; |
| 110 |
|
| 111 |
} // namespace mozilla |
| 112 |
|
| 113 |
#endif /* mozilla_TimeStamp_h */ |