1 //===------------------------- chrono.cpp ---------------------------------===//
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 #include "cerrno" // errno
11 #include "system_error" // __throw_system_error
12 #include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
13 #include "include/apple_availability.h"
15 #if __has_include(<unistd.h>)
19 #if !defined(__APPLE__) && _POSIX_TIMERS > 0
20 #define _LIBCPP_USE_CLOCK_GETTIME
23 #if defined(_LIBCPP_WIN32API)
24 # define WIN32_LEAN_AND_MEAN
25 # define VC_EXTRA_LEAN
27 # if _WIN32_WINNT >= _WIN32_WINNT_WIN8
28 # include <winapifamily.h>
31 # if !defined(CLOCK_REALTIME)
32 # include <sys/time.h> // for gettimeofday and timeval
33 # endif // !defined(CLOCK_REALTIME)
34 #endif // defined(_LIBCPP_WIN32API)
36 #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
37 # pragma comment(lib, "rt")
40 _LIBCPP_BEGIN_NAMESPACE_STD
47 const bool system_clock::is_steady;
49 system_clock::time_point
50 system_clock::now() _NOEXCEPT
52 #if defined(_LIBCPP_WIN32API)
53 // FILETIME is in 100ns units
54 using filetime_duration =
55 _VSTD::chrono::duration<__int64,
56 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
57 nanoseconds::period>>;
59 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
60 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
63 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
64 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
65 GetSystemTimePreciseAsFileTime(&ft);
67 GetSystemTimeAsFileTime(&ft);
70 GetSystemTimeAsFileTime(&ft);
73 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
74 static_cast<__int64>(ft.dwLowDateTime)};
75 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
77 #if defined(CLOCK_REALTIME)
79 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
80 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
81 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
85 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
86 #endif // CLOCK_REALTIME
91 system_clock::to_time_t(const time_point& t) _NOEXCEPT
93 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
96 system_clock::time_point
97 system_clock::from_time_t(time_t t) _NOEXCEPT
99 return system_clock::time_point(seconds(t));
102 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
105 // Warning: If this is not truly steady, then it is non-conforming. It is
106 // better for it to not exist and have the rest of libc++ use system_clock
109 const bool steady_clock::is_steady;
111 #if defined(__APPLE__)
113 #if !defined(CLOCK_MONOTONIC_RAW)
114 # error "Building libc++ on Apple platforms requires CLOCK_MONOTONIC_RAW"
117 // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
118 // mach_absolute_time are able to time functions in the nanosecond range.
119 // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
120 // also counts cycles when the system is asleep. Thus, it is the only
121 // acceptable implementation of steady_clock.
122 steady_clock::time_point
123 steady_clock::now() _NOEXCEPT
126 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
127 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
128 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
131 #elif defined(_LIBCPP_WIN32API)
133 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
134 // If the function fails, the return value is zero. <snip>
135 // On systems that run Windows XP or later, the function will always succeed
136 // and will thus never return zero.
139 __QueryPerformanceFrequency()
142 (void) QueryPerformanceFrequency(&val);
146 steady_clock::time_point
147 steady_clock::now() _NOEXCEPT
149 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
151 LARGE_INTEGER counter;
152 (void) QueryPerformanceCounter(&counter);
153 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
156 #elif defined(CLOCK_MONOTONIC)
158 steady_clock::time_point
159 steady_clock::now() _NOEXCEPT
162 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
163 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
164 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
168 # error "Monotonic clock not implemented"
171 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
175 _LIBCPP_END_NAMESPACE_STD