| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #if defined(__MVS__) |
| 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 |
| 13 | #endif |
| 14 | |
| 15 | #include <__system_error/throw_system_error.h> |
| 16 | #include <cerrno> // errno |
| 17 | #include <chrono> |
| 18 | |
| 19 | #if defined(__MVS__) |
| 20 | # include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic |
| 21 | #endif |
| 22 | |
| 23 | #include "include/apple_availability.h" |
| 24 | #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW} |
| 25 | |
| 26 | #if __has_include(<unistd.h>) |
| 27 | # include <unistd.h> // _POSIX_TIMERS |
| 28 | #endif |
| 29 | |
| 30 | #if __has_include(<sys/time.h>) |
| 31 | # include <sys/time.h> // for gettimeofday and timeval |
| 32 | #endif |
| 33 | |
| 34 | #if defined(__LLVM_LIBC__) |
| 35 | # define _LIBCPP_HAS_TIMESPEC_GET |
| 36 | #endif |
| 37 | |
| 38 | // OpenBSD and GPU do not have a fully conformant suite of POSIX timers, but |
| 39 | // it does have clock_gettime and CLOCK_MONOTONIC which is all we need. |
| 40 | #if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(__AMDGPU__) || \ |
| 41 | defined(__NVPTX__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) |
| 42 | # define _LIBCPP_HAS_CLOCK_GETTIME |
| 43 | #endif |
| 44 | |
| 45 | #if defined(_LIBCPP_WIN32API) |
| 46 | # define WIN32_LEAN_AND_MEAN |
| 47 | # define VC_EXTRA_LEAN |
| 48 | # include <windows.h> |
| 49 | # if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 50 | # include <winapifamily.h> |
| 51 | # endif |
| 52 | #endif // defined(_LIBCPP_WIN32API) |
| 53 | |
| 54 | #if defined(__Fuchsia__) |
| 55 | # include <zircon/syscalls.h> |
| 56 | #endif |
| 57 | |
| 58 | #if __has_include(<mach/mach_time.h>) |
| 59 | # include <mach/mach_time.h> |
| 60 | #endif |
| 61 | |
| 62 | #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) |
| 63 | # pragma comment(lib, "rt") |
| 64 | #endif |
| 65 | |
| 66 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 67 | |
| 68 | namespace chrono { |
| 69 | |
| 70 | // |
| 71 | // system_clock |
| 72 | // |
| 73 | |
| 74 | #if defined(_LIBCPP_WIN32API) |
| 75 | |
| 76 | # if _WIN32_WINNT < _WIN32_WINNT_WIN8 |
| 77 | |
| 78 | namespace { |
| 79 | |
| 80 | typedef void(WINAPI* GetSystemTimeAsFileTimePtr)(LPFILETIME); |
| 81 | |
| 82 | class GetSystemTimeInit { |
| 83 | public: |
| 84 | GetSystemTimeInit() { |
| 85 | fp = (GetSystemTimeAsFileTimePtr)(void*)GetProcAddress( |
| 86 | GetModuleHandleW(L"kernel32.dll" ), "GetSystemTimePreciseAsFileTime" ); |
| 87 | if (fp == nullptr) |
| 88 | fp = GetSystemTimeAsFileTime; |
| 89 | } |
| 90 | GetSystemTimeAsFileTimePtr fp; |
| 91 | }; |
| 92 | |
| 93 | // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority |
| 94 | // attribute with a value that's reserved for the implementation (we're the implementation). |
| 95 | # include "chrono_system_time_init.h" |
| 96 | } // namespace |
| 97 | |
| 98 | # endif |
| 99 | |
| 100 | static system_clock::time_point __libcpp_system_clock_now() { |
| 101 | // FILETIME is in 100ns units |
| 102 | using filetime_duration = |
| 103 | std::chrono::duration<__int64, std::ratio_multiply<std::ratio<100, 1>, nanoseconds::period>>; |
| 104 | |
| 105 | // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. |
| 106 | static constexpr const seconds nt_to_unix_epoch{11644473600}; |
| 107 | |
| 108 | FILETIME ft; |
| 109 | # if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \ |
| 110 | (_WIN32_WINNT >= _WIN32_WINNT_WIN10) |
| 111 | GetSystemTimePreciseAsFileTime(&ft); |
| 112 | # elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 113 | GetSystemTimeAsFileTime(&ft); |
| 114 | # else |
| 115 | GetSystemTimeAsFileTimeFunc.fp(&ft); |
| 116 | # endif |
| 117 | |
| 118 | filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) | static_cast<__int64>(ft.dwLowDateTime)}; |
| 119 | return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch)); |
| 120 | } |
| 121 | |
| 122 | #elif defined(_LIBCPP_HAS_TIMESPEC_GET) |
| 123 | |
| 124 | static system_clock::time_point __libcpp_system_clock_now() { |
| 125 | struct timespec ts; |
| 126 | if (timespec_get(&ts, TIME_UTC) != TIME_UTC) |
| 127 | std::__throw_system_error(errno, "timespec_get(TIME_UTC) failed" ); |
| 128 | return system_clock::time_point(seconds(ts.tv_sec) + microseconds(ts.tv_nsec / 1000)); |
| 129 | } |
| 130 | |
| 131 | #elif defined(_LIBCPP_HAS_CLOCK_GETTIME) |
| 132 | |
| 133 | static system_clock::time_point __libcpp_system_clock_now() { |
| 134 | struct timespec tp; |
| 135 | if (0 != clock_gettime(CLOCK_REALTIME, tp: &tp)) |
| 136 | std::__throw_system_error(errno, what_arg: "clock_gettime(CLOCK_REALTIME) failed" ); |
| 137 | return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); |
| 138 | } |
| 139 | |
| 140 | #else |
| 141 | |
| 142 | static system_clock::time_point __libcpp_system_clock_now() { |
| 143 | timeval tv; |
| 144 | gettimeofday(&tv, 0); |
| 145 | return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
| 146 | } |
| 147 | |
| 148 | #endif |
| 149 | |
| 150 | _LIBCPP_DIAGNOSTIC_PUSH |
| 151 | _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated" ) |
| 152 | const bool system_clock::is_steady; |
| 153 | _LIBCPP_DIAGNOSTIC_POP |
| 154 | |
| 155 | system_clock::time_point system_clock::now() noexcept { return __libcpp_system_clock_now(); } |
| 156 | |
| 157 | time_t system_clock::to_time_t(const time_point& t) noexcept { |
| 158 | return time_t(duration_cast<seconds>(fd: t.time_since_epoch()).count()); |
| 159 | } |
| 160 | |
| 161 | system_clock::time_point system_clock::from_time_t(time_t t) noexcept { return system_clock::time_point(seconds(t)); } |
| 162 | |
| 163 | // |
| 164 | // steady_clock |
| 165 | // |
| 166 | // Warning: If this is not truly steady, then it is non-conforming. It is |
| 167 | // better for it to not exist and have the rest of libc++ use system_clock |
| 168 | // instead. |
| 169 | // |
| 170 | |
| 171 | #if _LIBCPP_HAS_MONOTONIC_CLOCK |
| 172 | |
| 173 | # if defined(__APPLE__) |
| 174 | |
| 175 | // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or |
| 176 | // mach_absolute_time are able to time functions in the nanosecond range. |
| 177 | // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it |
| 178 | // also counts cycles when the system is asleep. Thus, it is the only |
| 179 | // acceptable implementation of steady_clock. |
| 180 | static steady_clock::time_point __libcpp_steady_clock_now() { |
| 181 | struct timespec tp; |
| 182 | if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) |
| 183 | std::__throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed" ); |
| 184 | return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 185 | } |
| 186 | |
| 187 | # elif defined(_LIBCPP_WIN32API) |
| 188 | |
| 189 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says: |
| 190 | // If the function fails, the return value is zero. <snip> |
| 191 | // On systems that run Windows XP or later, the function will always succeed |
| 192 | // and will thus never return zero. |
| 193 | |
| 194 | static LARGE_INTEGER __QueryPerformanceFrequency() { |
| 195 | LARGE_INTEGER val; |
| 196 | (void)QueryPerformanceFrequency(&val); |
| 197 | return val; |
| 198 | } |
| 199 | |
| 200 | static steady_clock::time_point __libcpp_steady_clock_now() { |
| 201 | static const LARGE_INTEGER freq = __QueryPerformanceFrequency(); |
| 202 | |
| 203 | LARGE_INTEGER counter; |
| 204 | (void)QueryPerformanceCounter(&counter); |
| 205 | auto seconds = counter.QuadPart / freq.QuadPart; |
| 206 | auto fractions = counter.QuadPart % freq.QuadPart; |
| 207 | auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart; |
| 208 | return steady_clock::time_point(steady_clock::duration(dur)); |
| 209 | } |
| 210 | |
| 211 | # elif defined(__MVS__) |
| 212 | |
| 213 | static steady_clock::time_point __libcpp_steady_clock_now() { |
| 214 | struct timespec64 ts; |
| 215 | if (0 != gettimeofdayMonotonic(&ts)) |
| 216 | std::__throw_system_error(errno, "failed to obtain time of day" ); |
| 217 | |
| 218 | return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec)); |
| 219 | } |
| 220 | |
| 221 | # elif defined(__Fuchsia__) |
| 222 | |
| 223 | static steady_clock::time_point __libcpp_steady_clock_now() noexcept { |
| 224 | // Implicitly link against the vDSO system call ABI without |
| 225 | // requiring the final link to specify -lzircon explicitly when |
| 226 | // statically linking libc++. |
| 227 | # pragma comment(lib, "zircon") |
| 228 | |
| 229 | return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic())); |
| 230 | } |
| 231 | |
| 232 | # elif defined(_LIBCPP_HAS_TIMESPEC_GET) |
| 233 | |
| 234 | static steady_clock::time_point __libcpp_steady_clock_now() { |
| 235 | struct timespec ts; |
| 236 | if (timespec_get(&ts, TIME_MONOTONIC) != TIME_MONOTONIC) |
| 237 | std::__throw_system_error(errno, "timespec_get(TIME_MONOTONIC) failed" ); |
| 238 | return steady_clock::time_point(seconds(ts.tv_sec) + microseconds(ts.tv_nsec / 1000)); |
| 239 | } |
| 240 | |
| 241 | # elif defined(_LIBCPP_HAS_CLOCK_GETTIME) |
| 242 | |
| 243 | static steady_clock::time_point __libcpp_steady_clock_now() { |
| 244 | struct timespec tp; |
| 245 | if (0 != clock_gettime(CLOCK_MONOTONIC, tp: &tp)) |
| 246 | std::__throw_system_error(errno, what_arg: "clock_gettime(CLOCK_MONOTONIC) failed" ); |
| 247 | return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 248 | } |
| 249 | |
| 250 | # else |
| 251 | # error "Monotonic clock not implemented on this platform" |
| 252 | # endif |
| 253 | |
| 254 | _LIBCPP_DIAGNOSTIC_PUSH |
| 255 | _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated" ) |
| 256 | const bool steady_clock::is_steady; |
| 257 | _LIBCPP_DIAGNOSTIC_POP |
| 258 | |
| 259 | steady_clock::time_point steady_clock::now() noexcept { return __libcpp_steady_clock_now(); } |
| 260 | |
| 261 | #endif // _LIBCPP_HAS_MONOTONIC_CLOCK |
| 262 | |
| 263 | } // namespace chrono |
| 264 | |
| 265 | _LIBCPP_END_NAMESPACE_STD |
| 266 | |