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 #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
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(_WIN32) && !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
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 filetime_duration
57 nt_to_unix_epoch{11644473600};
60 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
61 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
62 GetSystemTimePreciseAsFileTime(&ft);
64 GetSystemTimeAsFileTime(&ft);
67 GetSystemTimeAsFileTime(&ft);
70 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
71 static_cast<__int64>(ft.dwLowDateTime)};
72 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
76 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
77 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
78 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
79 #else // !CLOCK_REALTIME
82 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
83 #endif // CLOCK_REALTIME
88 system_clock::to_time_t(const time_point& t) _NOEXCEPT
90 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
93 system_clock::time_point
94 system_clock::from_time_t(time_t t) _NOEXCEPT
96 return system_clock::time_point(seconds(t));
99 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
102 // Warning: If this is not truly steady, then it is non-conforming. It is
103 // better for it to not exist and have the rest of libc++ use system_clock
106 const bool steady_clock::is_steady;
108 #if defined(__APPLE__)
110 // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
111 // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
112 // are run time constants supplied by the OS. This clock has no relationship
113 // to the Gregorian calendar. It's main use is as a high resolution timer.
115 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
116 // for that case as an optimization.
122 return static_cast<steady_clock::rep>(mach_absolute_time());
127 compute_steady_factor()
129 mach_timebase_info_data_t MachInfo;
130 mach_timebase_info(&MachInfo);
131 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
138 static const double factor = compute_steady_factor();
139 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
142 typedef steady_clock::rep (*FP)();
148 mach_timebase_info_data_t MachInfo;
149 mach_timebase_info(&MachInfo);
150 if (MachInfo.numer == MachInfo.denom)
151 return &steady_simplified;
155 steady_clock::time_point
156 steady_clock::now() _NOEXCEPT
158 static FP fp = init_steady_clock();
159 return time_point(duration(fp()));
162 #elif defined(_WIN32)
164 steady_clock::time_point
165 steady_clock::now() _NOEXCEPT
167 static LARGE_INTEGER freq;
168 static BOOL initialized = FALSE;
170 initialized = QueryPerformanceFrequency(&freq); // always succceeds
172 LARGE_INTEGER counter;
173 QueryPerformanceCounter(&counter);
174 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
177 #elif defined(CLOCK_MONOTONIC)
179 steady_clock::time_point
180 steady_clock::now() _NOEXCEPT
183 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
184 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
185 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
189 #error "Monotonic clock not implemented"
192 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
196 _LIBCPP_END_NAMESPACE_STD