]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - src/chrono.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / src / chrono.cpp
1 //===------------------------- chrono.cpp ---------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "chrono"
11 #include "cerrno"        // errno
12 #include "system_error"  // __throw_system_error
13 #include <time.h>        // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
14
15 #if defined(_WIN32)
16 #define WIN32_LEAN_AND_MEAN
17 #define VC_EXTRA_LEAN
18 #include <Windows.h>
19 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
20 #include <winapifamily.h>
21 #endif
22 #else
23 #if !defined(CLOCK_REALTIME)
24 #include <sys/time.h>        // for gettimeofday and timeval
25 #endif
26 #endif
27
28 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
29 #if __APPLE__
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"
33 #endif
34 #endif
35
36 _LIBCPP_BEGIN_NAMESPACE_STD
37
38 namespace chrono
39 {
40
41 // system_clock
42
43 const bool system_clock::is_steady;
44
45 system_clock::time_point
46 system_clock::now() _NOEXCEPT
47 {
48 #if defined(_WIN32)
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>>;
54
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};
58
59   FILETIME ft;
60 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
61 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
62   GetSystemTimePreciseAsFileTime(&ft);
63 #else
64   GetSystemTimeAsFileTime(&ft);
65 #endif
66 #else
67   GetSystemTimeAsFileTime(&ft);
68 #endif
69
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));
73 #else
74 #ifdef CLOCK_REALTIME
75     struct timespec tp;
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
80     timeval tv;
81     gettimeofday(&tv, 0);
82     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
83 #endif  // CLOCK_REALTIME
84 #endif
85 }
86
87 time_t
88 system_clock::to_time_t(const time_point& t) _NOEXCEPT
89 {
90     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
91 }
92
93 system_clock::time_point
94 system_clock::from_time_t(time_t t) _NOEXCEPT
95 {
96     return system_clock::time_point(seconds(t));
97 }
98
99 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
100 // steady_clock
101 //
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
104 //  instead.
105
106 const bool steady_clock::is_steady;
107
108 #if defined(__APPLE__)
109
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.
114
115 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
116 //   for that case as an optimization.
117
118 static
119 steady_clock::rep
120 steady_simplified()
121 {
122     return static_cast<steady_clock::rep>(mach_absolute_time());
123 }
124
125 static
126 double
127 compute_steady_factor()
128 {
129     mach_timebase_info_data_t MachInfo;
130     mach_timebase_info(&MachInfo);
131     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
132 }
133
134 static
135 steady_clock::rep
136 steady_full()
137 {
138     static const double factor = compute_steady_factor();
139     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
140 }
141
142 typedef steady_clock::rep (*FP)();
143
144 static
145 FP
146 init_steady_clock()
147 {
148     mach_timebase_info_data_t MachInfo;
149     mach_timebase_info(&MachInfo);
150     if (MachInfo.numer == MachInfo.denom)
151         return &steady_simplified;
152     return &steady_full;
153 }
154
155 steady_clock::time_point
156 steady_clock::now() _NOEXCEPT
157 {
158     static FP fp = init_steady_clock();
159     return time_point(duration(fp()));
160 }
161
162 #elif defined(_WIN32)
163
164 steady_clock::time_point
165 steady_clock::now() _NOEXCEPT
166 {
167   static LARGE_INTEGER freq;
168   static BOOL initialized = FALSE;
169   if (!initialized)
170     initialized = QueryPerformanceFrequency(&freq); // always succceeds
171
172   LARGE_INTEGER counter;
173   QueryPerformanceCounter(&counter);
174   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
175 }
176
177 #elif defined(CLOCK_MONOTONIC)
178
179 steady_clock::time_point
180 steady_clock::now() _NOEXCEPT
181 {
182     struct timespec tp;
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));
186 }
187
188 #else
189 #error "Monotonic clock not implemented"
190 #endif
191
192 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
193
194 }
195
196 _LIBCPP_END_NAMESPACE_STD