1/*
2 * Copyright (C) 2016-2019 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/Seconds.h>
30
31namespace WTF {
32
33class MonotonicTime;
34class PrintStream;
35
36// The current time according to a wall clock (aka real time clock). This uses floating point
37// internally so that you can reason about infinity and other things that arise in math. It's
38// acceptable to use this to wrap NaN times, negative times, and infinite times, so long as they
39// are relative to the same clock. Use this only if wall clock time is needed. For elapsed time
40// measurement use MonotonicTime instead.
41class WallTime final {
42 WTF_MAKE_FAST_ALLOCATED;
43public:
44 static constexpr ClockType clockType = ClockType::Wall;
45
46 // This is the epoch. So, x.secondsSinceEpoch() should be the same as x - WallTime().
47 constexpr WallTime() { }
48
49 // Call this if you know for sure that the double represents time according to
50 // WTF::currentTime(). It must be in seconds and it must be from the same time source.
51 static constexpr WallTime fromRawSeconds(double value)
52 {
53 return WallTime(value);
54 }
55
56 WTF_EXPORT_PRIVATE static WallTime now();
57
58 static constexpr WallTime infinity() { return fromRawSeconds(std::numeric_limits<double>::infinity()); }
59 static constexpr WallTime nan() { return fromRawSeconds(std::numeric_limits<double>::quiet_NaN()); }
60
61 constexpr Seconds secondsSinceEpoch() const { return Seconds(m_value); }
62
63 WallTime approximateWallTime() const { return *this; }
64 WTF_EXPORT_PRIVATE MonotonicTime approximateMonotonicTime() const;
65
66 explicit constexpr operator bool() const { return !!m_value; }
67
68 constexpr WallTime operator+(Seconds other) const
69 {
70 return fromRawSeconds(m_value + other.value());
71 }
72
73 constexpr WallTime operator-(Seconds other) const
74 {
75 return fromRawSeconds(m_value - other.value());
76 }
77
78 // Time is a scalar and scalars can be negated as this could arise from algebraic
79 // transformations. So, we allow it.
80 constexpr WallTime operator-() const
81 {
82 return fromRawSeconds(-m_value);
83 }
84
85 WallTime& operator+=(Seconds other)
86 {
87 return *this = *this + other;
88 }
89
90 WallTime& operator-=(Seconds other)
91 {
92 return *this = *this - other;
93 }
94
95 constexpr Seconds operator-(WallTime other) const
96 {
97 return Seconds(m_value - other.m_value);
98 }
99
100 constexpr bool operator==(WallTime other) const
101 {
102 return m_value == other.m_value;
103 }
104
105 constexpr bool operator!=(WallTime other) const
106 {
107 return m_value != other.m_value;
108 }
109
110 constexpr bool operator<(WallTime other) const
111 {
112 return m_value < other.m_value;
113 }
114
115 constexpr bool operator>(WallTime other) const
116 {
117 return m_value > other.m_value;
118 }
119
120 constexpr bool operator<=(WallTime other) const
121 {
122 return m_value <= other.m_value;
123 }
124
125 constexpr bool operator>=(WallTime other) const
126 {
127 return m_value >= other.m_value;
128 }
129
130 WTF_EXPORT_PRIVATE void dump(PrintStream&) const;
131
132 WallTime isolatedCopy() const
133 {
134 return *this;
135 }
136
137 struct MarkableTraits;
138
139private:
140 constexpr WallTime(double rawValue)
141 : m_value(rawValue)
142 {
143 }
144
145 double m_value { 0 };
146};
147
148struct WallTime::MarkableTraits {
149 static bool isEmptyValue(WallTime time)
150 {
151 return std::isnan(time.m_value);
152 }
153
154 static constexpr WallTime emptyValue()
155 {
156 return WallTime::nan();
157 }
158};
159
160WTF_EXPORT_PRIVATE void sleep(WallTime);
161
162} // namespace WTF
163
164namespace std {
165
166inline bool isnan(WTF::WallTime time)
167{
168 return std::isnan(time.secondsSinceEpoch().value());
169}
170
171inline bool isinf(WTF::WallTime time)
172{
173 return std::isinf(time.secondsSinceEpoch().value());
174}
175
176inline bool isfinite(WTF::WallTime time)
177{
178 return std::isfinite(time.secondsSinceEpoch().value());
179}
180
181} // namespace std
182
183using WTF::WallTime;
184