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