| 1 | /* |
| 2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <wtf/ClockType.h> |
| 29 | #include <wtf/MonotonicTime.h> |
| 30 | #include <wtf/WallTime.h> |
| 31 | |
| 32 | namespace WTF { |
| 33 | |
| 34 | class PrintStream; |
| 35 | |
| 36 | class TimeWithDynamicClockType { |
| 37 | public: |
| 38 | TimeWithDynamicClockType() { } |
| 39 | |
| 40 | TimeWithDynamicClockType(WallTime time) |
| 41 | : m_value(time.secondsSinceEpoch().value()) |
| 42 | , m_type(ClockType::Wall) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | TimeWithDynamicClockType(MonotonicTime time) |
| 47 | : m_value(time.secondsSinceEpoch().value()) |
| 48 | , m_type(ClockType::Monotonic) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | static TimeWithDynamicClockType fromRawSeconds(double value, ClockType type) |
| 53 | { |
| 54 | TimeWithDynamicClockType result; |
| 55 | result.m_value = value; |
| 56 | result.m_type = type; |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | Seconds secondsSinceEpoch() const { return Seconds(m_value); } |
| 61 | ClockType clockType() const { return m_type; } |
| 62 | |
| 63 | WTF_EXPORT_PRIVATE static TimeWithDynamicClockType now(ClockType); |
| 64 | |
| 65 | WTF_EXPORT_PRIVATE TimeWithDynamicClockType nowWithSameClock() const; |
| 66 | |
| 67 | TimeWithDynamicClockType withSameClockAndRawSeconds(double value) const |
| 68 | { |
| 69 | return TimeWithDynamicClockType::fromRawSeconds(value, clockType()); |
| 70 | } |
| 71 | |
| 72 | // Asserts that the time is of the type you want. |
| 73 | WTF_EXPORT_PRIVATE WallTime wallTime() const; |
| 74 | WTF_EXPORT_PRIVATE MonotonicTime monotonicTime() const; |
| 75 | |
| 76 | WTF_EXPORT_PRIVATE WallTime approximateWallTime() const; |
| 77 | WTF_EXPORT_PRIVATE MonotonicTime approximateMonotonicTime() const; |
| 78 | |
| 79 | explicit operator bool() const { return !!m_value; } |
| 80 | |
| 81 | TimeWithDynamicClockType operator+(Seconds other) const |
| 82 | { |
| 83 | return withSameClockAndRawSeconds(m_value + other.value()); |
| 84 | } |
| 85 | |
| 86 | TimeWithDynamicClockType operator-(Seconds other) const |
| 87 | { |
| 88 | return withSameClockAndRawSeconds(m_value - other.value()); |
| 89 | } |
| 90 | |
| 91 | // Time is a scalar and scalars can be negated as this could arise from algebraic |
| 92 | // transformations. So, we allow it. |
| 93 | TimeWithDynamicClockType operator-() const |
| 94 | { |
| 95 | return withSameClockAndRawSeconds(-m_value); |
| 96 | } |
| 97 | |
| 98 | TimeWithDynamicClockType operator+=(Seconds other) |
| 99 | { |
| 100 | return *this = *this + other; |
| 101 | } |
| 102 | |
| 103 | TimeWithDynamicClockType operator-=(Seconds other) |
| 104 | { |
| 105 | return *this = *this - other; |
| 106 | } |
| 107 | |
| 108 | WTF_EXPORT_PRIVATE Seconds operator-(const TimeWithDynamicClockType&) const; |
| 109 | |
| 110 | bool operator==(const TimeWithDynamicClockType& other) const |
| 111 | { |
| 112 | return m_value == other.m_value |
| 113 | && m_type == other.m_type; |
| 114 | } |
| 115 | |
| 116 | bool operator!=(const TimeWithDynamicClockType& other) const |
| 117 | { |
| 118 | return !(*this == other); |
| 119 | } |
| 120 | |
| 121 | // To do relative comparisons, you must be using times with the same clock type. |
| 122 | WTF_EXPORT_PRIVATE bool operator<(const TimeWithDynamicClockType&) const; |
| 123 | WTF_EXPORT_PRIVATE bool operator>(const TimeWithDynamicClockType&) const; |
| 124 | WTF_EXPORT_PRIVATE bool operator<=(const TimeWithDynamicClockType&) const; |
| 125 | WTF_EXPORT_PRIVATE bool operator>=(const TimeWithDynamicClockType&) const; |
| 126 | |
| 127 | WTF_EXPORT_PRIVATE void dump(PrintStream&) const; |
| 128 | |
| 129 | private: |
| 130 | double m_value { 0 }; |
| 131 | ClockType m_type { ClockType::Wall }; |
| 132 | }; |
| 133 | |
| 134 | WTF_EXPORT_PRIVATE void sleep(const TimeWithDynamicClockType&); |
| 135 | |
| 136 | WTF_EXPORT_PRIVATE bool hasElapsed(const TimeWithDynamicClockType&); |
| 137 | |
| 138 | } // namespace WTF |
| 139 | |
| 140 | namespace std { |
| 141 | |
| 142 | inline bool isnan(WTF::TimeWithDynamicClockType time) |
| 143 | { |
| 144 | return std::isnan(time.secondsSinceEpoch().value()); |
| 145 | } |
| 146 | |
| 147 | inline bool isinf(WTF::TimeWithDynamicClockType time) |
| 148 | { |
| 149 | return std::isinf(time.secondsSinceEpoch().value()); |
| 150 | } |
| 151 | |
| 152 | inline bool isfinite(WTF::TimeWithDynamicClockType time) |
| 153 | { |
| 154 | return std::isfinite(time.secondsSinceEpoch().value()); |
| 155 | } |
| 156 | |
| 157 | } // namespace std |
| 158 | |
| 159 | using WTF::TimeWithDynamicClockType; |
| 160 | using WTF::hasElapsed; |
| 161 | using WTF::sleep; |
| 162 | |