1 //===------------------------- chrono.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "cerrno" // errno
12 #include "system_error" // __throw_system_error
13 #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
16 #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
17 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
18 #define _LIBCXX_USE_CLOCK_GETTIME
20 #elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
21 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 100000
22 #define _LIBCXX_USE_CLOCK_GETTIME
24 #elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
25 #if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 100000
26 #define _LIBCXX_USE_CLOCK_GETTIME
28 #elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
29 #if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 30000
30 #define _LIBCXX_USE_CLOCK_GETTIME
32 #endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
34 #define _LIBCXX_USE_CLOCK_GETTIME
37 #if defined(_LIBCPP_WIN32API)
38 #define WIN32_LEAN_AND_MEAN
41 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
42 #include <winapifamily.h>
45 #if !defined(CLOCK_REALTIME) || !defined(_LIBCXX_USE_CLOCK_GETTIME)
46 #include <sys/time.h> // for gettimeofday and timeval
47 #endif // !defined(CLOCK_REALTIME)
48 #endif // defined(_LIBCPP_WIN32API)
50 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
52 #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
53 #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
54 #error "Monotonic clock not implemented"
58 _LIBCPP_BEGIN_NAMESPACE_STD
65 const bool system_clock::is_steady;
67 system_clock::time_point
68 system_clock::now() _NOEXCEPT
70 #if defined(_LIBCPP_WIN32API)
71 // FILETIME is in 100ns units
72 using filetime_duration =
73 _VSTD::chrono::duration<__int64,
74 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
75 nanoseconds::period>>;
77 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
78 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
81 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
82 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
83 GetSystemTimePreciseAsFileTime(&ft);
85 GetSystemTimeAsFileTime(&ft);
88 GetSystemTimeAsFileTime(&ft);
91 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
92 static_cast<__int64>(ft.dwLowDateTime)};
93 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
95 #if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
97 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
98 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
99 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
102 gettimeofday(&tv, 0);
103 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
104 #endif // _LIBCXX_USE_CLOCK_GETTIME && CLOCK_REALTIME
109 system_clock::to_time_t(const time_point& t) _NOEXCEPT
111 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
114 system_clock::time_point
115 system_clock::from_time_t(time_t t) _NOEXCEPT
117 return system_clock::time_point(seconds(t));
120 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
123 // Warning: If this is not truly steady, then it is non-conforming. It is
124 // better for it to not exist and have the rest of libc++ use system_clock
127 const bool steady_clock::is_steady;
129 #if defined(__APPLE__)
131 // Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
132 #if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
133 steady_clock::time_point
134 steady_clock::now() _NOEXCEPT
137 if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
138 __throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
139 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
143 // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
144 // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
145 // are run time constants supplied by the OS. This clock has no relationship
146 // to the Gregorian calendar. It's main use is as a high resolution timer.
148 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
149 // for that case as an optimization.
155 return static_cast<steady_clock::rep>(mach_absolute_time());
160 compute_steady_factor()
162 mach_timebase_info_data_t MachInfo;
163 mach_timebase_info(&MachInfo);
164 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
171 static const double factor = compute_steady_factor();
172 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
175 typedef steady_clock::rep (*FP)();
181 mach_timebase_info_data_t MachInfo;
182 mach_timebase_info(&MachInfo);
183 if (MachInfo.numer == MachInfo.denom)
184 return &steady_simplified;
188 steady_clock::time_point
189 steady_clock::now() _NOEXCEPT
191 static FP fp = init_steady_clock();
192 return time_point(duration(fp()));
194 #endif // defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
196 #elif defined(_LIBCPP_WIN32API)
198 steady_clock::time_point
199 steady_clock::now() _NOEXCEPT
201 static LARGE_INTEGER freq;
202 static BOOL initialized = FALSE;
204 initialized = QueryPerformanceFrequency(&freq); // always succceeds
206 LARGE_INTEGER counter;
207 QueryPerformanceCounter(&counter);
208 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
211 #elif defined(CLOCK_MONOTONIC)
213 // On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
214 // time functions in the nanosecond range. Thus, they are the only acceptable
215 // implementations of steady_clock.
217 #error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
220 steady_clock::time_point
221 steady_clock::now() _NOEXCEPT
224 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
225 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
226 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
230 #error "Monotonic clock not implemented"
233 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
237 _LIBCPP_END_NAMESPACE_STD