Skip to content

Commit 2dec36e

Browse files
committed
[libc++] NFCI: Refactor chrono.cpp to make it easier to support new platforms
Also simplify a few conditionals along the way for readability.
1 parent 45e0f65 commit 2dec36e

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

libcxx/src/chrono.cpp

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4242
namespace chrono
4343
{
4444

45+
//
4546
// system_clock
47+
//
4648

47-
const bool system_clock::is_steady;
48-
49-
system_clock::time_point
50-
system_clock::now() _NOEXCEPT
51-
{
5249
#if defined(_LIBCPP_WIN32API)
50+
51+
static system_clock::time_point __libcpp_system_clock_now() {
5352
// FILETIME is in 100ns units
5453
using filetime_duration =
5554
_VSTD::chrono::duration<__int64,
@@ -60,31 +59,42 @@ system_clock::now() _NOEXCEPT
6059
static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
6160

6261
FILETIME ft;
63-
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
64-
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
62+
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
6563
GetSystemTimePreciseAsFileTime(&ft);
66-
#else
67-
GetSystemTimeAsFileTime(&ft);
68-
#endif
6964
#else
7065
GetSystemTimeAsFileTime(&ft);
7166
#endif
7267

7368
filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
7469
static_cast<__int64>(ft.dwLowDateTime)};
75-
return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
76-
#else
77-
#if defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
70+
return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
71+
}
72+
73+
#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
74+
75+
static system_clock::time_point __libcpp_system_clock_now() {
7876
struct timespec tp;
7977
if (0 != clock_gettime(CLOCK_REALTIME, &tp))
8078
__throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
81-
return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
79+
return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
80+
}
81+
8282
#else
83+
84+
static system_clock::time_point __libcpp_system_clock_now() {
8385
timeval tv;
8486
gettimeofday(&tv, 0);
85-
return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
86-
#endif
87+
return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
88+
}
89+
8790
#endif
91+
92+
const bool system_clock::is_steady;
93+
94+
system_clock::time_point
95+
system_clock::now() _NOEXCEPT
96+
{
97+
return __libcpp_system_clock_now();
8898
}
8999

90100
time_t
@@ -99,33 +109,28 @@ system_clock::from_time_t(time_t t) _NOEXCEPT
99109
return system_clock::time_point(seconds(t));
100110
}
101111

102-
#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
112+
//
103113
// steady_clock
104114
//
105115
// Warning: If this is not truly steady, then it is non-conforming. It is
106116
// better for it to not exist and have the rest of libc++ use system_clock
107117
// instead.
118+
//
108119

109-
const bool steady_clock::is_steady;
120+
#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
110121

111122
#if defined(__APPLE__)
112123

113-
#if !defined(CLOCK_MONOTONIC_RAW)
114-
# error "Building libc++ on Apple platforms requires CLOCK_MONOTONIC_RAW"
115-
#endif
116-
117124
// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
118125
// mach_absolute_time are able to time functions in the nanosecond range.
119126
// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
120127
// also counts cycles when the system is asleep. Thus, it is the only
121128
// acceptable implementation of steady_clock.
122-
steady_clock::time_point
123-
steady_clock::now() _NOEXCEPT
124-
{
129+
static steady_clock::time_point __libcpp_steady_clock_now() {
125130
struct timespec tp;
126131
if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
127132
__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
128-
return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
133+
return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
129134
}
130135

131136
#elif defined(_LIBCPP_WIN32API)
@@ -138,36 +143,40 @@ steady_clock::now() _NOEXCEPT
138143
static LARGE_INTEGER
139144
__QueryPerformanceFrequency()
140145
{
141-
LARGE_INTEGER val;
142-
(void) QueryPerformanceFrequency(&val);
143-
return val;
146+
LARGE_INTEGER val;
147+
(void) QueryPerformanceFrequency(&val);
148+
return val;
144149
}
145150

146-
steady_clock::time_point
147-
steady_clock::now() _NOEXCEPT
148-
{
151+
static steady_clock::time_point __libcpp_steady_clock_now() {
149152
static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
150153

151154
LARGE_INTEGER counter;
152155
(void) QueryPerformanceCounter(&counter);
153-
return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
156+
return steady_clock::time_point(steady_clock::duration(counter.QuadPart * nano::den / freq.QuadPart));
154157
}
155158

156159
#elif defined(CLOCK_MONOTONIC)
157160

158-
steady_clock::time_point
159-
steady_clock::now() _NOEXCEPT
160-
{
161+
static steady_clock::time_point __libcpp_steady_clock_now() {
161162
struct timespec tp;
162163
if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
163164
__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
164-
return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
165+
return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
165166
}
166167

167168
#else
168-
# error "Monotonic clock not implemented"
169+
# error "Monotonic clock not implemented on this platform"
169170
#endif
170171

172+
const bool steady_clock::is_steady;
173+
174+
steady_clock::time_point
175+
steady_clock::now() _NOEXCEPT
176+
{
177+
return __libcpp_steady_clock_now();
178+
}
179+
171180
#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
172181

173182
}

0 commit comments

Comments
 (0)