Andrew Top | 2ea2238 | 2016-12-08 09:47:36 -0800 | [diff] [blame] | 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_h |
| 8 | #define mozilla_TimeStamp_h |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | #include "mozilla/Assertions.h" |
| 12 | #include "mozilla/Attributes.h" |
| 13 | #include "mozilla/FloatingPoint.h" |
| 14 | #include "mozilla/TypeTraits.h" |
| 15 | #include "mozilla/Types.h" |
| 16 | |
| 17 | namespace IPC { |
| 18 | template<typename T> struct ParamTraits; |
| 19 | } // namespace IPC |
| 20 | |
| 21 | #ifdef XP_WIN |
| 22 | // defines TimeStampValue as a complex value keeping both |
| 23 | // GetTickCount and QueryPerformanceCounter values |
| 24 | #include "TimeStamp_windows.h" |
| 25 | #endif |
| 26 | |
| 27 | namespace mozilla { |
| 28 | |
| 29 | #ifndef XP_WIN |
| 30 | typedef uint64_t TimeStampValue; |
| 31 | #endif |
| 32 | |
| 33 | class TimeStamp; |
| 34 | |
| 35 | /** |
| 36 | * Platform-specific implementation details of BaseTimeDuration. |
| 37 | */ |
| 38 | class BaseTimeDurationPlatformUtils |
| 39 | { |
| 40 | public: |
| 41 | static MFBT_API double ToSeconds(int64_t aTicks); |
| 42 | static MFBT_API double ToSecondsSigDigits(int64_t aTicks); |
| 43 | static MFBT_API int64_t TicksFromMilliseconds(double aMilliseconds); |
| 44 | static MFBT_API int64_t ResolutionInTicks(); |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Instances of this class represent the length of an interval of time. |
| 49 | * Negative durations are allowed, meaning the end is before the start. |
| 50 | * |
| 51 | * Internally the duration is stored as a int64_t in units of |
| 52 | * PR_TicksPerSecond() when building with NSPR interval timers, or a |
| 53 | * system-dependent unit when building with system clocks. The |
| 54 | * system-dependent unit must be constant, otherwise the semantics of |
| 55 | * this class would be broken. |
| 56 | * |
| 57 | * The ValueCalculator template parameter determines how arithmetic |
| 58 | * operations are performed on the integer count of ticks (mValue). |
| 59 | */ |
| 60 | template <typename ValueCalculator> |
| 61 | class BaseTimeDuration |
| 62 | { |
| 63 | public: |
| 64 | // The default duration is 0. |
| 65 | MOZ_CONSTEXPR BaseTimeDuration() : mValue(0) {} |
| 66 | // Allow construction using '0' as the initial value, for readability, |
| 67 | // but no other numbers (so we don't have any implicit unit conversions). |
| 68 | struct _SomethingVeryRandomHere; |
| 69 | MOZ_IMPLICIT BaseTimeDuration(_SomethingVeryRandomHere* aZero) : mValue(0) |
| 70 | { |
| 71 | MOZ_ASSERT(!aZero, "Who's playing funny games here?"); |
| 72 | } |
| 73 | // Default copy-constructor and assignment are OK |
| 74 | |
| 75 | // Converting copy-constructor and assignment operator |
| 76 | template <typename E> |
| 77 | explicit BaseTimeDuration(const BaseTimeDuration<E>& aOther) |
| 78 | : mValue(aOther.mValue) |
| 79 | { } |
| 80 | |
| 81 | template <typename E> |
| 82 | BaseTimeDuration& operator=(const BaseTimeDuration<E>& aOther) |
| 83 | { |
| 84 | mValue = aOther.mValue; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | double ToSeconds() const |
| 89 | { |
| 90 | if (mValue == INT64_MAX) { |
| 91 | return PositiveInfinity<double>(); |
| 92 | } |
| 93 | if (mValue == INT64_MIN) { |
| 94 | return NegativeInfinity<double>(); |
| 95 | } |
| 96 | return BaseTimeDurationPlatformUtils::ToSeconds(mValue); |
| 97 | } |
| 98 | // Return a duration value that includes digits of time we think to |
| 99 | // be significant. This method should be used when displaying a |
| 100 | // time to humans. |
| 101 | double ToSecondsSigDigits() const |
| 102 | { |
| 103 | if (mValue == INT64_MAX) { |
| 104 | return PositiveInfinity<double>(); |
| 105 | } |
| 106 | if (mValue == INT64_MIN) { |
| 107 | return NegativeInfinity<double>(); |
| 108 | } |
| 109 | return BaseTimeDurationPlatformUtils::ToSecondsSigDigits(mValue); |
| 110 | } |
| 111 | double ToMilliseconds() const { return ToSeconds() * 1000.0; } |
| 112 | double ToMicroseconds() const { return ToMilliseconds() * 1000.0; } |
| 113 | |
| 114 | // Using a double here is safe enough; with 53 bits we can represent |
| 115 | // durations up to over 280,000 years exactly. If the units of |
| 116 | // mValue do not allow us to represent durations of that length, |
| 117 | // long durations are clamped to the max/min representable value |
| 118 | // instead of overflowing. |
| 119 | static inline BaseTimeDuration FromSeconds(double aSeconds) |
| 120 | { |
| 121 | return FromMilliseconds(aSeconds * 1000.0); |
| 122 | } |
| 123 | static BaseTimeDuration FromMilliseconds(double aMilliseconds) |
| 124 | { |
| 125 | if (aMilliseconds == PositiveInfinity<double>()) { |
| 126 | return Forever(); |
| 127 | } |
| 128 | if (aMilliseconds == NegativeInfinity<double>()) { |
| 129 | return FromTicks(INT64_MIN); |
| 130 | } |
| 131 | return FromTicks( |
| 132 | BaseTimeDurationPlatformUtils::TicksFromMilliseconds(aMilliseconds)); |
| 133 | } |
| 134 | static inline BaseTimeDuration FromMicroseconds(double aMicroseconds) |
| 135 | { |
| 136 | return FromMilliseconds(aMicroseconds / 1000.0); |
| 137 | } |
| 138 | |
| 139 | static BaseTimeDuration Forever() |
| 140 | { |
| 141 | return FromTicks(INT64_MAX); |
| 142 | } |
| 143 | |
| 144 | BaseTimeDuration operator+(const BaseTimeDuration& aOther) const |
| 145 | { |
| 146 | return FromTicks(ValueCalculator::Add(mValue, aOther.mValue)); |
| 147 | } |
| 148 | BaseTimeDuration operator-(const BaseTimeDuration& aOther) const |
| 149 | { |
| 150 | return FromTicks(ValueCalculator::Subtract(mValue, aOther.mValue)); |
| 151 | } |
| 152 | BaseTimeDuration& operator+=(const BaseTimeDuration& aOther) |
| 153 | { |
| 154 | mValue = ValueCalculator::Add(mValue, aOther.mValue); |
| 155 | return *this; |
| 156 | } |
| 157 | BaseTimeDuration& operator-=(const BaseTimeDuration& aOther) |
| 158 | { |
| 159 | mValue = ValueCalculator::Subtract(mValue, aOther.mValue); |
| 160 | return *this; |
| 161 | } |
| 162 | BaseTimeDuration operator-() const |
| 163 | { |
| 164 | // We don't just use FromTicks(ValueCalculator::Subtract(0, mValue)) |
| 165 | // since that won't give the correct result for -TimeDuration::Forever(). |
| 166 | int64_t ticks; |
| 167 | if (mValue == INT64_MAX) { |
| 168 | ticks = INT64_MIN; |
| 169 | } else if (mValue == INT64_MIN) { |
| 170 | ticks = INT64_MAX; |
| 171 | } else { |
| 172 | ticks = -mValue; |
| 173 | } |
| 174 | |
| 175 | return FromTicks(ticks); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | // Block double multiplier (slower, imprecise if long duration) - Bug 853398. |
| 180 | // If required, use MultDouble explicitly and with care. |
| 181 | BaseTimeDuration operator*(const double aMultiplier) const = delete; |
| 182 | |
| 183 | // Block double divisor (for the same reason, and because dividing by |
| 184 | // fractional values would otherwise invoke the int64_t variant, and rounding |
| 185 | // the passed argument can then cause divide-by-zero) - Bug 1147491. |
| 186 | BaseTimeDuration operator/(const double aDivisor) const = delete; |
| 187 | |
| 188 | public: |
| 189 | BaseTimeDuration MultDouble(double aMultiplier) const |
| 190 | { |
| 191 | return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 192 | } |
| 193 | BaseTimeDuration operator*(const int32_t aMultiplier) const |
| 194 | { |
| 195 | return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 196 | } |
| 197 | BaseTimeDuration operator*(const uint32_t aMultiplier) const |
| 198 | { |
| 199 | return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 200 | } |
| 201 | BaseTimeDuration operator*(const int64_t aMultiplier) const |
| 202 | { |
| 203 | return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 204 | } |
| 205 | BaseTimeDuration operator*(const uint64_t aMultiplier) const |
| 206 | { |
| 207 | if (aMultiplier > INT64_MAX) { |
| 208 | return Forever(); |
| 209 | } |
| 210 | return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); |
| 211 | } |
| 212 | BaseTimeDuration operator/(const int64_t aDivisor) const |
| 213 | { |
| 214 | MOZ_ASSERT(aDivisor != 0, "Division by zero"); |
| 215 | return FromTicks(ValueCalculator::Divide(mValue, aDivisor)); |
| 216 | } |
| 217 | double operator/(const BaseTimeDuration& aOther) const |
| 218 | { |
| 219 | #ifndef MOZ_B2G |
| 220 | // Bug 1066388 - This fails on B2G ICS Emulator |
| 221 | MOZ_ASSERT(aOther.mValue != 0, "Division by zero"); |
| 222 | #endif |
| 223 | return ValueCalculator::DivideDouble(mValue, aOther.mValue); |
| 224 | } |
| 225 | BaseTimeDuration operator%(const BaseTimeDuration& aOther) const |
| 226 | { |
| 227 | MOZ_ASSERT(aOther.mValue != 0, "Division by zero"); |
| 228 | return FromTicks(ValueCalculator::Modulo(mValue, aOther.mValue)); |
| 229 | } |
| 230 | |
| 231 | template<typename E> |
| 232 | bool operator<(const BaseTimeDuration<E>& aOther) const |
| 233 | { |
| 234 | return mValue < aOther.mValue; |
| 235 | } |
| 236 | template<typename E> |
| 237 | bool operator<=(const BaseTimeDuration<E>& aOther) const |
| 238 | { |
| 239 | return mValue <= aOther.mValue; |
| 240 | } |
| 241 | template<typename E> |
| 242 | bool operator>=(const BaseTimeDuration<E>& aOther) const |
| 243 | { |
| 244 | return mValue >= aOther.mValue; |
| 245 | } |
| 246 | template<typename E> |
| 247 | bool operator>(const BaseTimeDuration<E>& aOther) const |
| 248 | { |
| 249 | return mValue > aOther.mValue; |
| 250 | } |
| 251 | template<typename E> |
| 252 | bool operator==(const BaseTimeDuration<E>& aOther) const |
| 253 | { |
| 254 | return mValue == aOther.mValue; |
| 255 | } |
| 256 | template<typename E> |
| 257 | bool operator!=(const BaseTimeDuration<E>& aOther) const |
| 258 | { |
| 259 | return mValue != aOther.mValue; |
| 260 | } |
| 261 | bool IsZero() const |
| 262 | { |
| 263 | return mValue == 0; |
| 264 | } |
| 265 | explicit operator bool() const |
| 266 | { |
| 267 | return mValue != 0; |
| 268 | } |
| 269 | |
| 270 | // Return a best guess at the system's current timing resolution, |
| 271 | // which might be variable. BaseTimeDurations below this order of |
| 272 | // magnitude are meaningless, and those at the same order of |
| 273 | // magnitude or just above are suspect. |
| 274 | static BaseTimeDuration Resolution() { |
| 275 | return FromTicks(BaseTimeDurationPlatformUtils::ResolutionInTicks()); |
| 276 | } |
| 277 | |
| 278 | // We could define additional operators here: |
| 279 | // -- convert to/from other time units |
| 280 | // -- scale duration by a float |
| 281 | // but let's do that on demand. |
| 282 | // Comparing durations for equality will only lead to bugs on |
| 283 | // platforms with high-resolution timers. |
| 284 | |
| 285 | private: |
| 286 | friend class TimeStamp; |
| 287 | friend struct IPC::ParamTraits<mozilla::BaseTimeDuration<ValueCalculator>>; |
| 288 | template <typename> |
| 289 | friend class BaseTimeDuration; |
| 290 | |
| 291 | static BaseTimeDuration FromTicks(int64_t aTicks) |
| 292 | { |
| 293 | BaseTimeDuration t; |
| 294 | t.mValue = aTicks; |
| 295 | return t; |
| 296 | } |
| 297 | |
| 298 | static BaseTimeDuration FromTicks(double aTicks) |
| 299 | { |
| 300 | // NOTE: this MUST be a >= test, because int64_t(double(INT64_MAX)) |
| 301 | // overflows and gives INT64_MIN. |
| 302 | if (aTicks >= double(INT64_MAX)) { |
| 303 | return FromTicks(INT64_MAX); |
| 304 | } |
| 305 | |
| 306 | // This MUST be a <= test. |
| 307 | if (aTicks <= double(INT64_MIN)) { |
| 308 | return FromTicks(INT64_MIN); |
| 309 | } |
| 310 | |
| 311 | return FromTicks(int64_t(aTicks)); |
| 312 | } |
| 313 | |
| 314 | // Duration, result is implementation-specific difference of two TimeStamps |
| 315 | int64_t mValue; |
| 316 | }; |
| 317 | |
| 318 | /** |
| 319 | * Perform arithmetic operations on the value of a BaseTimeDuration without |
| 320 | * doing strict checks on the range of values. |
| 321 | */ |
| 322 | class TimeDurationValueCalculator |
| 323 | { |
| 324 | public: |
| 325 | static int64_t Add(int64_t aA, int64_t aB) { return aA + aB; } |
| 326 | static int64_t Subtract(int64_t aA, int64_t aB) { return aA - aB; } |
| 327 | |
| 328 | template <typename T> |
| 329 | static int64_t Multiply(int64_t aA, T aB) |
| 330 | { |
| 331 | static_assert(IsIntegral<T>::value, |
| 332 | "Using integer multiplication routine with non-integer type." |
| 333 | " Further specialization required"); |
| 334 | return aA * static_cast<int64_t>(aB); |
| 335 | } |
| 336 | |
| 337 | static int64_t Divide(int64_t aA, int64_t aB) { return aA / aB; } |
| 338 | static double DivideDouble(int64_t aA, int64_t aB) |
| 339 | { |
| 340 | return static_cast<double>(aA) / aB; |
| 341 | } |
| 342 | static int64_t Modulo(int64_t aA, int64_t aB) { return aA % aB; } |
| 343 | }; |
| 344 | |
| 345 | template <> |
| 346 | inline int64_t |
| 347 | TimeDurationValueCalculator::Multiply<double>(int64_t aA, double aB) |
| 348 | { |
| 349 | return static_cast<int64_t>(aA * aB); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Specialization of BaseTimeDuration that uses TimeDurationValueCalculator for |
| 354 | * arithmetic on the mValue member. |
| 355 | * |
| 356 | * Use this class for time durations that are *not* expected to hold values of |
| 357 | * Forever (or the negative equivalent) or when such time duration are *not* |
| 358 | * expected to be used in arithmetic operations. |
| 359 | */ |
| 360 | typedef BaseTimeDuration<TimeDurationValueCalculator> TimeDuration; |
| 361 | |
| 362 | /** |
| 363 | * Instances of this class represent moments in time, or a special |
| 364 | * "null" moment. We do not use the non-monotonic system clock or |
| 365 | * local time, since they can be reset, causing apparent backward |
| 366 | * travel in time, which can confuse algorithms. Instead we measure |
| 367 | * elapsed time according to the system. This time can never go |
| 368 | * backwards (i.e. it never wraps around, at least not in less than |
| 369 | * five million years of system elapsed time). It might not advance |
| 370 | * while the system is sleeping. If TimeStamp::SetNow() is not called |
| 371 | * at all for hours or days, we might not notice the passage of some |
| 372 | * of that time. |
| 373 | * |
| 374 | * We deliberately do not expose a way to convert TimeStamps to some |
| 375 | * particular unit. All you can do is compute a difference between two |
| 376 | * TimeStamps to get a TimeDuration. You can also add a TimeDuration |
| 377 | * to a TimeStamp to get a new TimeStamp. You can't do something |
| 378 | * meaningless like add two TimeStamps. |
| 379 | * |
| 380 | * Internally this is implemented as either a wrapper around |
| 381 | * - high-resolution, monotonic, system clocks if they exist on this |
| 382 | * platform |
| 383 | * - PRIntervalTime otherwise. We detect wraparounds of |
| 384 | * PRIntervalTime and work around them. |
| 385 | * |
| 386 | * This class is similar to C++11's time_point, however it is |
| 387 | * explicitly nullable and provides an IsNull() method. time_point |
| 388 | * is initialized to the clock's epoch and provides a |
| 389 | * time_since_epoch() method that functions similiarly. i.e. |
| 390 | * t.IsNull() is equivalent to t.time_since_epoch() == decltype(t)::duration::zero(); |
| 391 | */ |
| 392 | class TimeStamp |
| 393 | { |
| 394 | public: |
| 395 | /** |
| 396 | * Initialize to the "null" moment |
| 397 | */ |
| 398 | MOZ_CONSTEXPR TimeStamp() : mValue(0) {} |
| 399 | // Default copy-constructor and assignment are OK |
| 400 | |
| 401 | /** |
| 402 | * The system timestamps are the same as the TimeStamp |
| 403 | * retrieved by mozilla::TimeStamp. Since we need this for |
| 404 | * vsync timestamps, we enable the creation of mozilla::TimeStamps |
| 405 | * on platforms that support vsync aligned refresh drivers / compositors |
| 406 | * Verified true as of Jan 31, 2015: B2G and OS X |
| 407 | * False on Windows 7 |
| 408 | * UNTESTED ON OTHER PLATFORMS |
| 409 | */ |
| 410 | #if defined(MOZ_WIDGET_GONK) || defined(XP_DARWIN) |
| 411 | static TimeStamp FromSystemTime(int64_t aSystemTime) |
| 412 | { |
| 413 | static_assert(sizeof(aSystemTime) == sizeof(TimeStampValue), |
| 414 | "System timestamp should be same units as TimeStampValue"); |
| 415 | return TimeStamp(aSystemTime); |
| 416 | } |
| 417 | #endif |
| 418 | |
| 419 | /** |
| 420 | * Return true if this is the "null" moment |
| 421 | */ |
| 422 | bool IsNull() const { return mValue == 0; } |
| 423 | |
| 424 | /** |
| 425 | * Return true if this is not the "null" moment, may be used in tests, e.g.: |
| 426 | * |if (timestamp) { ... }| |
| 427 | */ |
| 428 | explicit operator bool() const |
| 429 | { |
| 430 | return mValue != 0; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Return a timestamp reflecting the current elapsed system time. This |
| 435 | * is monotonically increasing (i.e., does not decrease) over the |
| 436 | * lifetime of this process' XPCOM session. |
| 437 | * |
| 438 | * Now() is trying to ensure the best possible precision on each platform, |
| 439 | * at least one millisecond. |
| 440 | * |
| 441 | * NowLoRes() has been introduced to workaround performance problems of |
| 442 | * QueryPerformanceCounter on the Windows platform. NowLoRes() is giving |
| 443 | * lower precision, usually 15.6 ms, but with very good performance benefit. |
| 444 | * Use it for measurements of longer times, like >200ms timeouts. |
| 445 | */ |
| 446 | static TimeStamp Now() { return Now(true); } |
| 447 | static TimeStamp NowLoRes() { return Now(false); } |
| 448 | |
| 449 | /** |
| 450 | * Return a timestamp representing the time when the current process was |
| 451 | * created which will be comparable with other timestamps taken with this |
| 452 | * class. If the actual process creation time is detected to be inconsistent |
| 453 | * the @a aIsInconsistent parameter will be set to true, the returned |
| 454 | * timestamp however will still be valid though inaccurate. |
| 455 | * |
| 456 | * @param aIsInconsistent Set to true if an inconsistency was detected in the |
| 457 | * process creation time |
| 458 | * @returns A timestamp representing the time when the process was created, |
| 459 | * this timestamp is always valid even when errors are reported |
| 460 | */ |
| 461 | static MFBT_API TimeStamp ProcessCreation(bool& aIsInconsistent); |
| 462 | |
| 463 | /** |
| 464 | * Records a process restart. After this call ProcessCreation() will return |
| 465 | * the time when the browser was restarted instead of the actual time when |
| 466 | * the process was created. |
| 467 | */ |
| 468 | static MFBT_API void RecordProcessRestart(); |
| 469 | |
| 470 | /** |
| 471 | * Compute the difference between two timestamps. Both must be non-null. |
| 472 | */ |
| 473 | TimeDuration operator-(const TimeStamp& aOther) const |
| 474 | { |
| 475 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 476 | MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 477 | static_assert(-INT64_MAX > INT64_MIN, "int64_t sanity check"); |
| 478 | int64_t ticks = int64_t(mValue - aOther.mValue); |
| 479 | // Check for overflow. |
| 480 | if (mValue > aOther.mValue) { |
| 481 | if (ticks < 0) { |
| 482 | ticks = INT64_MAX; |
| 483 | } |
| 484 | } else { |
| 485 | if (ticks > 0) { |
| 486 | ticks = INT64_MIN; |
| 487 | } |
| 488 | } |
| 489 | return TimeDuration::FromTicks(ticks); |
| 490 | } |
| 491 | |
| 492 | TimeStamp operator+(const TimeDuration& aOther) const |
| 493 | { |
| 494 | TimeStamp result = *this; |
| 495 | result += aOther; |
| 496 | return result; |
| 497 | } |
| 498 | TimeStamp operator-(const TimeDuration& aOther) const |
| 499 | { |
| 500 | TimeStamp result = *this; |
| 501 | result -= aOther; |
| 502 | return result; |
| 503 | } |
| 504 | TimeStamp& operator+=(const TimeDuration& aOther) |
| 505 | { |
| 506 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 507 | TimeStampValue value = mValue + aOther.mValue; |
| 508 | // Check for underflow. |
| 509 | // (We don't check for overflow because it's not obvious what the error |
| 510 | // behavior should be in that case.) |
| 511 | if (aOther.mValue < 0 && value > mValue) { |
| 512 | value = 0; |
| 513 | } |
| 514 | mValue = value; |
| 515 | return *this; |
| 516 | } |
| 517 | TimeStamp& operator-=(const TimeDuration& aOther) |
| 518 | { |
| 519 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 520 | TimeStampValue value = mValue - aOther.mValue; |
| 521 | // Check for underflow. |
| 522 | // (We don't check for overflow because it's not obvious what the error |
| 523 | // behavior should be in that case.) |
| 524 | if (aOther.mValue > 0 && value > mValue) { |
| 525 | value = 0; |
| 526 | } |
| 527 | mValue = value; |
| 528 | return *this; |
| 529 | } |
| 530 | |
| 531 | bool operator<(const TimeStamp& aOther) const |
| 532 | { |
| 533 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 534 | MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 535 | return mValue < aOther.mValue; |
| 536 | } |
| 537 | bool operator<=(const TimeStamp& aOther) const |
| 538 | { |
| 539 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 540 | MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 541 | return mValue <= aOther.mValue; |
| 542 | } |
| 543 | bool operator>=(const TimeStamp& aOther) const |
| 544 | { |
| 545 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 546 | MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 547 | return mValue >= aOther.mValue; |
| 548 | } |
| 549 | bool operator>(const TimeStamp& aOther) const |
| 550 | { |
| 551 | MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); |
| 552 | MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); |
| 553 | return mValue > aOther.mValue; |
| 554 | } |
| 555 | bool operator==(const TimeStamp& aOther) const |
| 556 | { |
| 557 | return IsNull() |
| 558 | ? aOther.IsNull() |
| 559 | : !aOther.IsNull() && mValue == aOther.mValue; |
| 560 | } |
| 561 | bool operator!=(const TimeStamp& aOther) const |
| 562 | { |
| 563 | return !(*this == aOther); |
| 564 | } |
| 565 | |
| 566 | // Comparing TimeStamps for equality should be discouraged. Adding |
| 567 | // two TimeStamps, or scaling TimeStamps, is nonsense and must never |
| 568 | // be allowed. |
| 569 | |
| 570 | static MFBT_API void Startup(); |
| 571 | static MFBT_API void Shutdown(); |
| 572 | |
| 573 | private: |
| 574 | friend struct IPC::ParamTraits<mozilla::TimeStamp>; |
| 575 | friend void StartupTimelineRecordExternal(int, uint64_t); |
| 576 | |
| 577 | MOZ_IMPLICIT TimeStamp(TimeStampValue aValue) : mValue(aValue) {} |
| 578 | |
| 579 | static MFBT_API TimeStamp Now(bool aHighResolution); |
| 580 | |
| 581 | /** |
| 582 | * Computes the uptime of the current process in microseconds. The result |
| 583 | * is platform-dependent and needs to be checked against existing timestamps |
| 584 | * for consistency. |
| 585 | * |
| 586 | * @returns The number of microseconds since the calling process was started |
| 587 | * or 0 if an error was encountered while computing the uptime |
| 588 | */ |
| 589 | static MFBT_API uint64_t ComputeProcessUptime(); |
| 590 | |
| 591 | /** |
| 592 | * When built with PRIntervalTime, a value of 0 means this instance |
| 593 | * is "null". Otherwise, the low 32 bits represent a PRIntervalTime, |
| 594 | * and the high 32 bits represent a counter of the number of |
| 595 | * rollovers of PRIntervalTime that we've seen. This counter starts |
| 596 | * at 1 to avoid a real time colliding with the "null" value. |
| 597 | * |
| 598 | * PR_INTERVAL_MAX is set at 100,000 ticks per second. So the minimum |
| 599 | * time to wrap around is about 2^64/100000 seconds, i.e. about |
| 600 | * 5,849,424 years. |
| 601 | * |
| 602 | * When using a system clock, a value is system dependent. |
| 603 | */ |
| 604 | TimeStampValue mValue; |
| 605 | }; |
| 606 | |
| 607 | } // namespace mozilla |
| 608 | |
| 609 | #endif /* mozilla_TimeStamp_h */ |