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
32namespace WTF {
33
34class PrintStream;
35
36class TimeWithDynamicClockType final {
37 WTF_MAKE_FAST_ALLOCATED;
38public:
39 TimeWithDynamicClockType() { }
40
41 TimeWithDynamicClockType(WallTime time)
42 : m_value(time.secondsSinceEpoch().value())
43 , m_type(ClockType::Wall)
44 {
45 }
46
47 TimeWithDynamicClockType(MonotonicTime time)
48 : m_value(time.secondsSinceEpoch().value())
49 , m_type(ClockType::Monotonic)
50 {
51 }
52
53 static TimeWithDynamicClockType fromRawSeconds(double value, ClockType type)
54 {
55 TimeWithDynamicClockType result;
56 result.m_value = value;
57 result.m_type = type;
58 return result;
59 }
60
61 Seconds secondsSinceEpoch() const { return Seconds(m_value); }
62 ClockType clockType() const { return m_type; }
63
64 WTF_EXPORT_PRIVATE static TimeWithDynamicClockType now(ClockType);
65
66 WTF_EXPORT_PRIVATE TimeWithDynamicClockType nowWithSameClock() const;
67
68 TimeWithDynamicClockType withSameClockAndRawSeconds(double value) const
69 {
70 return TimeWithDynamicClockType::fromRawSeconds(value, clockType());
71 }
72
73 // Asserts that the time is of the type you want.
74 WTF_EXPORT_PRIVATE WallTime wallTime() const;
75 WTF_EXPORT_PRIVATE MonotonicTime monotonicTime() const;
76
77 WTF_EXPORT_PRIVATE WallTime approximateWallTime() const;
78 WTF_EXPORT_PRIVATE MonotonicTime approximateMonotonicTime() const;
79
80 explicit operator bool() const { return !!m_value; }
81
82 TimeWithDynamicClockType operator+(Seconds other) const
83 {
84 return withSameClockAndRawSeconds(m_value + other.value());
85 }
86
87 TimeWithDynamicClockType operator-(Seconds other) const
88 {
89 return withSameClockAndRawSeconds(m_value - other.value());
90 }
91
92 // Time is a scalar and scalars can be negated as this could arise from algebraic
93 // transformations. So, we allow it.
94 TimeWithDynamicClockType operator-() const
95 {
96 return withSameClockAndRawSeconds(-m_value);
97 }
98
99 TimeWithDynamicClockType operator+=(Seconds other)
100 {
101 return *this = *this + other;
102 }
103
104 TimeWithDynamicClockType operator-=(Seconds other)
105 {
106 return *this = *this - other;
107 }
108
109 WTF_EXPORT_PRIVATE Seconds operator-(const TimeWithDynamicClockType&) const;
110
111 bool operator==(const TimeWithDynamicClockType& other) const
112 {
113 return m_value == other.m_value
114 && m_type == other.m_type;
115 }
116
117 bool operator!=(const TimeWithDynamicClockType& other) const
118 {
119 return !(*this == other);
120 }
121
122 // To do relative comparisons, you must be using times with the same clock type.
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 WTF_EXPORT_PRIVATE bool operator>=(const TimeWithDynamicClockType&) const;
127
128 WTF_EXPORT_PRIVATE void dump(PrintStream&) const;
129
130private:
131 double m_value { 0 };
132 ClockType m_type { ClockType::Wall };
133};
134
135WTF_EXPORT_PRIVATE void sleep(const TimeWithDynamicClockType&);
136
137WTF_EXPORT_PRIVATE bool hasElapsed(const TimeWithDynamicClockType&);
138
139} // namespace WTF
140
141namespace std {
142
143inline bool isnan(WTF::TimeWithDynamicClockType time)
144{
145 return std::isnan(time.secondsSinceEpoch().value());
146}
147
148inline bool isinf(WTF::TimeWithDynamicClockType time)
149{
150 return std::isinf(time.secondsSinceEpoch().value());
151}
152
153inline bool isfinite(WTF::TimeWithDynamicClockType time)
154{
155 return std::isfinite(time.secondsSinceEpoch().value());
156}
157
158} // namespace std
159
160using WTF::TimeWithDynamicClockType;
161using WTF::hasElapsed;
162using WTF::sleep;
163