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 | #include "config.h" |
27 | #include <wtf/TimeWithDynamicClockType.h> |
28 | |
29 | #include <cfloat> |
30 | #include <cmath> |
31 | #include <wtf/Condition.h> |
32 | #include <wtf/DataLog.h> |
33 | #include <wtf/PrintStream.h> |
34 | #include <wtf/Lock.h> |
35 | |
36 | namespace WTF { |
37 | |
38 | TimeWithDynamicClockType TimeWithDynamicClockType::now(ClockType type) |
39 | { |
40 | switch (type) { |
41 | case ClockType::Wall: |
42 | return WallTime::now(); |
43 | case ClockType::Monotonic: |
44 | return MonotonicTime::now(); |
45 | } |
46 | RELEASE_ASSERT_NOT_REACHED(); |
47 | return TimeWithDynamicClockType(); |
48 | } |
49 | |
50 | TimeWithDynamicClockType TimeWithDynamicClockType::nowWithSameClock() const |
51 | { |
52 | return now(clockType()); |
53 | } |
54 | |
55 | WallTime TimeWithDynamicClockType::wallTime() const |
56 | { |
57 | RELEASE_ASSERT(m_type == ClockType::Wall); |
58 | return WallTime::fromRawSeconds(m_value); |
59 | } |
60 | |
61 | MonotonicTime TimeWithDynamicClockType::monotonicTime() const |
62 | { |
63 | RELEASE_ASSERT(m_type == ClockType::Monotonic); |
64 | return MonotonicTime::fromRawSeconds(m_value); |
65 | } |
66 | |
67 | WallTime TimeWithDynamicClockType::approximateWallTime() const |
68 | { |
69 | switch (m_type) { |
70 | case ClockType::Wall: |
71 | return wallTime(); |
72 | case ClockType::Monotonic: |
73 | return monotonicTime().approximateWallTime(); |
74 | } |
75 | RELEASE_ASSERT_NOT_REACHED(); |
76 | return WallTime(); |
77 | } |
78 | |
79 | MonotonicTime TimeWithDynamicClockType::approximateMonotonicTime() const |
80 | { |
81 | switch (m_type) { |
82 | case ClockType::Wall: |
83 | return wallTime().approximateMonotonicTime(); |
84 | case ClockType::Monotonic: |
85 | return monotonicTime(); |
86 | } |
87 | RELEASE_ASSERT_NOT_REACHED(); |
88 | return MonotonicTime(); |
89 | } |
90 | |
91 | Seconds TimeWithDynamicClockType::operator-(const TimeWithDynamicClockType& other) const |
92 | { |
93 | RELEASE_ASSERT(m_type == other.m_type); |
94 | return Seconds(m_value - other.m_value); |
95 | } |
96 | |
97 | bool TimeWithDynamicClockType::operator<(const TimeWithDynamicClockType& other) const |
98 | { |
99 | RELEASE_ASSERT(m_type == other.m_type); |
100 | return m_value < other.m_value; |
101 | } |
102 | |
103 | bool TimeWithDynamicClockType::operator>(const TimeWithDynamicClockType& other) const |
104 | { |
105 | RELEASE_ASSERT(m_type == other.m_type); |
106 | return m_value > other.m_value; |
107 | } |
108 | |
109 | bool TimeWithDynamicClockType::operator<=(const TimeWithDynamicClockType& other) const |
110 | { |
111 | RELEASE_ASSERT(m_type == other.m_type); |
112 | return m_value <= other.m_value; |
113 | } |
114 | |
115 | bool TimeWithDynamicClockType::operator>=(const TimeWithDynamicClockType& other) const |
116 | { |
117 | RELEASE_ASSERT(m_type == other.m_type); |
118 | return m_value >= other.m_value; |
119 | } |
120 | |
121 | void TimeWithDynamicClockType::dump(PrintStream& out) const |
122 | { |
123 | out.print(m_type, "(" , m_value, " sec)" ); |
124 | } |
125 | |
126 | void sleep(const TimeWithDynamicClockType& time) |
127 | { |
128 | Lock fakeLock; |
129 | Condition fakeCondition; |
130 | LockHolder fakeLocker(fakeLock); |
131 | fakeCondition.waitUntil(fakeLock, time); |
132 | } |
133 | |
134 | bool hasElapsed(const TimeWithDynamicClockType& time) |
135 | { |
136 | // Avoid doing now(). |
137 | if (!(time > time.withSameClockAndRawSeconds(0))) |
138 | return true; |
139 | if (std::isinf(time.secondsSinceEpoch().value())) |
140 | return false; |
141 | |
142 | return time <= time.nowWithSameClock(); |
143 | } |
144 | |
145 | } // namespace WTF |
146 | |
147 | |
148 | |