1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API
11 // to be defined before any system header to include definition of struct timespec64.
12 # define _LARGE_TIME_API
15 #include <__system_error/throw_system_error.h>
16 #include <cerrno> // errno
20 # include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic
23 #include "include/apple_availability.h"
24 #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
26 #if __has_include(<unistd.h>)
27 # include <unistd.h> // _POSIX_TIMERS
30 #if __has_include(<sys/time.h>)
31 # include <sys/time.h> // for gettimeofday and timeval
34 // OpenBSD and GPU do not have a fully conformant suite of POSIX timers, but
35 // it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
36 #if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(__AMDGPU__) || \
37 defined(__NVPTX__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
38 # define _LIBCPP_HAS_CLOCK_GETTIME
41 #if defined(_LIBCPP_WIN32API)
42 # define WIN32_LEAN_AND_MEAN
43 # define VC_EXTRA_LEAN
45 # if _WIN32_WINNT >= _WIN32_WINNT_WIN8
46 # include <winapifamily.h>
48 #endif // defined(_LIBCPP_WIN32API)
50 #if defined(__Fuchsia__)
51 # include <zircon/syscalls.h>
54 #if __has_include(<mach/mach_time.h>)
55 # include <mach/mach_time.h>
58 #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
59 # pragma comment(lib, "rt")
62 _LIBCPP_BEGIN_NAMESPACE_STD
70 #if defined(_LIBCPP_WIN32API)
72 # if _WIN32_WINNT < _WIN32_WINNT_WIN8
76 typedef void(WINAPI
* GetSystemTimeAsFileTimePtr
)(LPFILETIME
);
78 class GetSystemTimeInit
{
81 fp
= (GetSystemTimeAsFileTimePtr
)(void*)GetProcAddress(
82 GetModuleHandleW(L
"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
84 fp
= GetSystemTimeAsFileTime
;
86 GetSystemTimeAsFileTimePtr fp
;
89 // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
90 // attribute with a value that's reserved for the implementation (we're the implementation).
91 # include "chrono_system_time_init.h"
96 static system_clock::time_point
__libcpp_system_clock_now() {
97 // FILETIME is in 100ns units
98 using filetime_duration
=
99 std::chrono::duration
<__int64
, std::ratio_multiply
<std::ratio
<100, 1>, nanoseconds::period
>>;
101 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
102 static constexpr const seconds nt_to_unix_epoch
{11644473600};
105 # if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \
106 (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
107 GetSystemTimePreciseAsFileTime(&ft
);
108 # elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
109 GetSystemTimeAsFileTime(&ft
);
111 GetSystemTimeAsFileTimeFunc
.fp(&ft
);
114 filetime_duration d
{(static_cast<__int64
>(ft
.dwHighDateTime
) << 32) | static_cast<__int64
>(ft
.dwLowDateTime
)};
115 return system_clock::time_point(duration_cast
<system_clock::duration
>(d
- nt_to_unix_epoch
));
118 #elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
120 static system_clock::time_point
__libcpp_system_clock_now() {
122 if (0 != clock_gettime(CLOCK_REALTIME
, &tp
))
123 __throw_system_error(errno
, "clock_gettime(CLOCK_REALTIME) failed");
124 return system_clock::time_point(seconds(tp
.tv_sec
) + microseconds(tp
.tv_nsec
/ 1000));
129 static system_clock::time_point
__libcpp_system_clock_now() {
131 gettimeofday(&tv
, 0);
132 return system_clock::time_point(seconds(tv
.tv_sec
) + microseconds(tv
.tv_usec
));
137 _LIBCPP_DIAGNOSTIC_PUSH
138 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated")
139 const bool system_clock::is_steady
;
140 _LIBCPP_DIAGNOSTIC_POP
142 system_clock::time_point
system_clock::now() noexcept
{ return __libcpp_system_clock_now(); }
144 time_t system_clock::to_time_t(const time_point
& t
) noexcept
{
145 return time_t(duration_cast
<seconds
>(t
.time_since_epoch()).count());
148 system_clock::time_point
system_clock::from_time_t(time_t t
) noexcept
{ return system_clock::time_point(seconds(t
)); }
153 // Warning: If this is not truly steady, then it is non-conforming. It is
154 // better for it to not exist and have the rest of libc++ use system_clock
158 #if _LIBCPP_HAS_MONOTONIC_CLOCK
160 # if defined(__APPLE__)
162 // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
163 // mach_absolute_time are able to time functions in the nanosecond range.
164 // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
165 // also counts cycles when the system is asleep. Thus, it is the only
166 // acceptable implementation of steady_clock.
167 static steady_clock::time_point
__libcpp_steady_clock_now() {
169 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW
, &tp
))
170 __throw_system_error(errno
, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
171 return steady_clock::time_point(seconds(tp
.tv_sec
) + nanoseconds(tp
.tv_nsec
));
174 # elif defined(_LIBCPP_WIN32API)
176 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
177 // If the function fails, the return value is zero. <snip>
178 // On systems that run Windows XP or later, the function will always succeed
179 // and will thus never return zero.
181 static LARGE_INTEGER
__QueryPerformanceFrequency() {
183 (void)QueryPerformanceFrequency(&val
);
187 static steady_clock::time_point
__libcpp_steady_clock_now() {
188 static const LARGE_INTEGER freq
= __QueryPerformanceFrequency();
190 LARGE_INTEGER counter
;
191 (void)QueryPerformanceCounter(&counter
);
192 auto seconds
= counter
.QuadPart
/ freq
.QuadPart
;
193 auto fractions
= counter
.QuadPart
% freq
.QuadPart
;
194 auto dur
= seconds
* nano::den
+ fractions
* nano::den
/ freq
.QuadPart
;
195 return steady_clock::time_point(steady_clock::duration(dur
));
198 # elif defined(__MVS__)
200 static steady_clock::time_point
__libcpp_steady_clock_now() {
201 struct timespec64 ts
;
202 if (0 != gettimeofdayMonotonic(&ts
))
203 __throw_system_error(errno
, "failed to obtain time of day");
205 return steady_clock::time_point(seconds(ts
.tv_sec
) + nanoseconds(ts
.tv_nsec
));
208 # elif defined(__Fuchsia__)
210 static steady_clock::time_point
__libcpp_steady_clock_now() noexcept
{
211 // Implicitly link against the vDSO system call ABI without
212 // requiring the final link to specify -lzircon explicitly when
213 // statically linking libc++.
214 # pragma comment(lib, "zircon")
216 return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
219 # elif defined(_LIBCPP_HAS_CLOCK_GETTIME)
221 static steady_clock::time_point
__libcpp_steady_clock_now() {
223 if (0 != clock_gettime(CLOCK_MONOTONIC
, &tp
))
224 __throw_system_error(errno
, "clock_gettime(CLOCK_MONOTONIC) failed");
225 return steady_clock::time_point(seconds(tp
.tv_sec
) + nanoseconds(tp
.tv_nsec
));
229 # error "Monotonic clock not implemented on this platform"
232 _LIBCPP_DIAGNOSTIC_PUSH
233 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated")
234 const bool steady_clock::is_steady
;
235 _LIBCPP_DIAGNOSTIC_POP
237 steady_clock::time_point
steady_clock::now() noexcept
{ return __libcpp_steady_clock_now(); }
239 #endif // _LIBCPP_HAS_MONOTONIC_CLOCK
241 } // namespace chrono
243 _LIBCPP_END_NAMESPACE_STD