xref: /llvm-project-15.0.7/libcxx/src/chrono.cpp (revision 71e108cd)
1 //===------------------------- chrono.cpp ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "chrono"
10 #include "cerrno"        // errno
11 #include "system_error"  // __throw_system_error
12 #include <time.h>        // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
13 #include "include/apple_availability.h"
14 
15 #if __has_include(<unistd.h>)
16 # include <unistd.h>
17 #endif
18 
19 #if __has_include(<sys/time.h>)
20 # include <sys/time.h> // for gettimeofday and timeval
21 #endif
22 
23 #if !defined(__APPLE__) && _POSIX_TIMERS > 0
24 # define _LIBCPP_USE_CLOCK_GETTIME
25 #endif
26 
27 #if defined(_LIBCPP_WIN32API)
28 #  define WIN32_LEAN_AND_MEAN
29 #  define VC_EXTRA_LEAN
30 #  include <windows.h>
31 #  if _WIN32_WINNT >= _WIN32_WINNT_WIN8
32 #    include <winapifamily.h>
33 #  endif
34 #endif // defined(_LIBCPP_WIN32API)
35 
36 #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
37 #  pragma comment(lib, "rt")
38 #endif
39 
40 _LIBCPP_BEGIN_NAMESPACE_STD
41 
42 namespace chrono
43 {
44 
45 //
46 // system_clock
47 //
48 
49 #if defined(_LIBCPP_WIN32API)
50 
51 static system_clock::time_point __libcpp_system_clock_now() {
52   // FILETIME is in 100ns units
53   using filetime_duration =
54       _VSTD::chrono::duration<__int64,
55                               _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
56                                                     nanoseconds::period>>;
57 
58   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
59   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
60 
61   FILETIME ft;
62 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
63   GetSystemTimePreciseAsFileTime(&ft);
64 #else
65   GetSystemTimeAsFileTime(&ft);
66 #endif
67 
68   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
69                        static_cast<__int64>(ft.dwLowDateTime)};
70   return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
71 }
72 
73 #elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
74 
75 static system_clock::time_point __libcpp_system_clock_now() {
76   struct timespec tp;
77   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
78     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
79   return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
80 }
81 
82 #else
83 
84 static system_clock::time_point __libcpp_system_clock_now() {
85     timeval tv;
86     gettimeofday(&tv, 0);
87     return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
88 }
89 
90 #endif
91 
92 const bool system_clock::is_steady;
93 
94 system_clock::time_point
95 system_clock::now() _NOEXCEPT
96 {
97     return __libcpp_system_clock_now();
98 }
99 
100 time_t
101 system_clock::to_time_t(const time_point& t) _NOEXCEPT
102 {
103     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
104 }
105 
106 system_clock::time_point
107 system_clock::from_time_t(time_t t) _NOEXCEPT
108 {
109     return system_clock::time_point(seconds(t));
110 }
111 
112 //
113 // steady_clock
114 //
115 // Warning:  If this is not truly steady, then it is non-conforming.  It is
116 //  better for it to not exist and have the rest of libc++ use system_clock
117 //  instead.
118 //
119 
120 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
121 
122 #if defined(__APPLE__)
123 
124 // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
125 // mach_absolute_time are able to time functions in the nanosecond range.
126 // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
127 // also counts cycles when the system is asleep. Thus, it is the only
128 // acceptable implementation of steady_clock.
129 static steady_clock::time_point __libcpp_steady_clock_now() {
130     struct timespec tp;
131     if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
132         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
133     return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
134 }
135 
136 #elif defined(_LIBCPP_WIN32API)
137 
138 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
139 //    If the function fails, the return value is zero. <snip>
140 //    On systems that run Windows XP or later, the function will always succeed
141 //      and will thus never return zero.
142 
143 static LARGE_INTEGER
144 __QueryPerformanceFrequency()
145 {
146     LARGE_INTEGER val;
147     (void) QueryPerformanceFrequency(&val);
148     return val;
149 }
150 
151 static steady_clock::time_point __libcpp_steady_clock_now() {
152   static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
153 
154   LARGE_INTEGER counter;
155   (void) QueryPerformanceCounter(&counter);
156   return steady_clock::time_point(steady_clock::duration(counter.QuadPart * nano::den / freq.QuadPart));
157 }
158 
159 #elif defined(CLOCK_MONOTONIC)
160 
161 static steady_clock::time_point __libcpp_steady_clock_now() {
162     struct timespec tp;
163     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
164         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
165     return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
166 }
167 
168 #else
169 #   error "Monotonic clock not implemented on this platform"
170 #endif
171 
172 const bool steady_clock::is_steady;
173 
174 steady_clock::time_point
175 steady_clock::now() _NOEXCEPT
176 {
177     return __libcpp_steady_clock_now();
178 }
179 
180 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
181 
182 }
183 
184 _LIBCPP_END_NAMESPACE_STD
185