]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/src/chrono.cpp
Merge libc++ r291274, and update the library Makefile.
[FreeBSD/FreeBSD.git] / contrib / libc++ / 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(_LIBCPP_WIN32API)
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 // !defined(CLOCK_REALTIME)
26 #endif // defined(_LIBCPP_WIN32API)
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(_LIBCPP_WIN32API) && !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(_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>>;
54
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};
57
58   FILETIME ft;
59 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
60 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
61   GetSystemTimePreciseAsFileTime(&ft);
62 #else
63   GetSystemTimeAsFileTime(&ft);
64 #endif
65 #else
66   GetSystemTimeAsFileTime(&ft);
67 #endif
68
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));
72 #else
73 #ifdef CLOCK_REALTIME
74     struct timespec tp;
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
79     timeval tv;
80     gettimeofday(&tv, 0);
81     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
82 #endif  // CLOCK_REALTIME
83 #endif
84 }
85
86 time_t
87 system_clock::to_time_t(const time_point& t) _NOEXCEPT
88 {
89     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
90 }
91
92 system_clock::time_point
93 system_clock::from_time_t(time_t t) _NOEXCEPT
94 {
95     return system_clock::time_point(seconds(t));
96 }
97
98 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
99 // steady_clock
100 //
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
103 //  instead.
104
105 const bool steady_clock::is_steady;
106
107 #if defined(__APPLE__)
108
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.
113
114 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
115 //   for that case as an optimization.
116
117 static
118 steady_clock::rep
119 steady_simplified()
120 {
121     return static_cast<steady_clock::rep>(mach_absolute_time());
122 }
123
124 static
125 double
126 compute_steady_factor()
127 {
128     mach_timebase_info_data_t MachInfo;
129     mach_timebase_info(&MachInfo);
130     return static_cast<double>(MachInfo.numer) / MachInfo.denom;
131 }
132
133 static
134 steady_clock::rep
135 steady_full()
136 {
137     static const double factor = compute_steady_factor();
138     return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
139 }
140
141 typedef steady_clock::rep (*FP)();
142
143 static
144 FP
145 init_steady_clock()
146 {
147     mach_timebase_info_data_t MachInfo;
148     mach_timebase_info(&MachInfo);
149     if (MachInfo.numer == MachInfo.denom)
150         return &steady_simplified;
151     return &steady_full;
152 }
153
154 steady_clock::time_point
155 steady_clock::now() _NOEXCEPT
156 {
157     static FP fp = init_steady_clock();
158     return time_point(duration(fp()));
159 }
160
161 #elif defined(_LIBCPP_WIN32API)
162
163 steady_clock::time_point
164 steady_clock::now() _NOEXCEPT
165 {
166   static LARGE_INTEGER freq;
167   static BOOL initialized = FALSE;
168   if (!initialized)
169     initialized = QueryPerformanceFrequency(&freq); // always succceeds
170
171   LARGE_INTEGER counter;
172   QueryPerformanceCounter(&counter);
173   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
174 }
175
176 #elif defined(CLOCK_MONOTONIC)
177
178 steady_clock::time_point
179 steady_clock::now() _NOEXCEPT
180 {
181     struct timespec tp;
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));
185 }
186
187 #else
188 #error "Monotonic clock not implemented"
189 #endif
190
191 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
192
193 }
194
195 _LIBCPP_END_NAMESPACE_STD