Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / mozglue / misc / TimeStamp_windows.h
blobc96e060e55d7f99f160a6b30dfbb0c6c052cac6a
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 #ifndef mozilla_TimeStamp_windows_h
8 #define mozilla_TimeStamp_windows_h
10 #include "mozilla/Types.h"
12 namespace mozilla {
14 /**
15 * The [mt] unit:
17 * Many values are kept in ticks of the Performance Counter x 1000,
18 * further just referred as [mt], meaning milli-ticks.
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.
25 #define ms2mt(x) ((x)*mozilla::GetQueryPerformanceFrequencyPerSec())
26 #define mt2ms(x) ((x) / mozilla::GetQueryPerformanceFrequencyPerSec())
27 #define mt2ms_f(x) (double(x) / mozilla::GetQueryPerformanceFrequencyPerSec())
29 MFBT_API uint64_t GetQueryPerformanceFrequencyPerSec();
31 class TimeStamp;
32 class TimeStampValue;
33 class TimeStampValueTests;
34 class TimeStampTests;
36 TimeStampValue NowInternal(bool aHighResolution);
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;
46 // Both QPC and GTC are kept in [mt] units.
47 uint64_t mGTC;
48 uint64_t mQPC;
50 bool mIsNull;
51 bool mHasQPC;
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) {}
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) {}
65 MFBT_API uint64_t CheckQPC(const TimeStampValue& aOther) const;
67 public:
68 MFBT_API uint64_t operator-(const TimeStampValue& aOther) const;
70 TimeStampValue operator+(const int64_t aOther) const {
71 return TimeStampValue(mGTC + aOther, mQPC + aOther, mHasQPC);
73 TimeStampValue operator-(const int64_t aOther) const {
74 return TimeStampValue(mGTC - aOther, mQPC - aOther, mHasQPC);
76 MFBT_API TimeStampValue& operator+=(const int64_t aOther);
77 MFBT_API TimeStampValue& operator-=(const int64_t aOther);
79 constexpr bool operator<(const TimeStampValue& aOther) const {
80 return mHasQPC && aOther.mHasQPC ? mQPC < aOther.mQPC : mGTC < aOther.mGTC;
82 constexpr bool operator>(const TimeStampValue& aOther) const {
83 return mHasQPC && aOther.mHasQPC ? mQPC > aOther.mQPC : mGTC > aOther.mGTC;
85 constexpr bool operator<=(const TimeStampValue& aOther) const {
86 return mHasQPC && aOther.mHasQPC ? mQPC <= aOther.mQPC
87 : mGTC <= aOther.mGTC;
89 constexpr bool operator>=(const TimeStampValue& aOther) const {
90 return mHasQPC && aOther.mHasQPC ? mQPC >= aOther.mQPC
91 : mGTC >= aOther.mGTC;
93 constexpr bool operator==(const TimeStampValue& aOther) const {
94 return mHasQPC && aOther.mHasQPC ? mQPC == aOther.mQPC
95 : mGTC == aOther.mGTC;
97 constexpr bool operator!=(const TimeStampValue& aOther) const {
98 return mHasQPC && aOther.mHasQPC ? mQPC != aOther.mQPC
99 : mGTC != aOther.mGTC;
101 constexpr bool IsNull() const { return mIsNull; }
103 #if defined(DEBUG)
104 uint64_t GTC() const { return mGTC; }
105 uint64_t QPC() const { return mQPC; }
107 bool HasQPC() const { return mHasQPC; }
108 #endif
111 } // namespace mozilla
113 #endif /* mozilla_TimeStamp_h */