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
15 #if defined(_LIBCPP_WIN32API)
16 #define WIN32_LEAN_AND_MEAN
19 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
20 #include <winapifamily.h>
23 #if !defined(CLOCK_REALTIME)
24 #include <sys/time.h> // for gettimeofday and timeval
25 #endif // !defined(CLOCK_REALTIME)
26 #endif // defined(_LIBCPP_WIN32API)
28 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
30 #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
31 #elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
32 #error "Monotonic clock not implemented"
36 _LIBCPP_BEGIN_NAMESPACE_STD
43 const bool system_clock::is_steady;
45 system_clock::time_point
46 system_clock::now() _NOEXCEPT
48 #if defined(_LIBCPP_WIN32API)
49 // FILETIME is in 100ns units
50 using filetime_duration =
51 _VSTD::chrono::duration<__int64,
52 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
53 nanoseconds::period>>;
55 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
56 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
59 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
60 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
61 GetSystemTimePreciseAsFileTime(&ft);
63 GetSystemTimeAsFileTime(&ft);
66 GetSystemTimeAsFileTime(&ft);
69 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
70 static_cast<__int64>(ft.dwLowDateTime)};
71 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
75 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
76 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
77 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
78 #else // !CLOCK_REALTIME
81 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
82 #endif // CLOCK_REALTIME
87 system_clock::to_time_t(const time_point& t) _NOEXCEPT
89 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
92 system_clock::time_point
93 system_clock::from_time_t(time_t t) _NOEXCEPT
95 return system_clock::time_point(seconds(t));
98 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
101 // Warning: If this is not truly steady, then it is non-conforming. It is
102 // better for it to not exist and have the rest of libc++ use system_clock
105 const bool steady_clock::is_steady;
107 #if defined(__APPLE__)
109 // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
110 // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
111 // are run time constants supplied by the OS. This clock has no relationship
112 // to the Gregorian calendar. It's main use is as a high resolution timer.
114 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
115 // for that case as an optimization.
121 return static_cast<steady_clock::rep>(mach_absolute_time());
126 compute_steady_factor()
128 mach_timebase_info_data_t MachInfo;
129 mach_timebase_info(&MachInfo);
130 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
137 static const double factor = compute_steady_factor();
138 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
141 typedef steady_clock::rep (*FP)();
147 mach_timebase_info_data_t MachInfo;
148 mach_timebase_info(&MachInfo);
149 if (MachInfo.numer == MachInfo.denom)
150 return &steady_simplified;
154 steady_clock::time_point
155 steady_clock::now() _NOEXCEPT
157 static FP fp = init_steady_clock();
158 return time_point(duration(fp()));
161 #elif defined(_LIBCPP_WIN32API)
163 steady_clock::time_point
164 steady_clock::now() _NOEXCEPT
166 static LARGE_INTEGER freq;
167 static BOOL initialized = FALSE;
169 initialized = QueryPerformanceFrequency(&freq); // always succceeds
171 LARGE_INTEGER counter;
172 QueryPerformanceCounter(&counter);
173 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
176 #elif defined(CLOCK_MONOTONIC)
178 steady_clock::time_point
179 steady_clock::now() _NOEXCEPT
182 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
183 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
184 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
188 #error "Monotonic clock not implemented"
191 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
195 _LIBCPP_END_NAMESPACE_STD