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 !defined(__APPLE__) && _POSIX_TIMERS > 0
20 #define _LIBCPP_USE_CLOCK_GETTIME
21 #endif
22 
23 #if defined(_LIBCPP_WIN32API)
24 #  define WIN32_LEAN_AND_MEAN
25 #  define VC_EXTRA_LEAN
26 #  include <windows.h>
27 #  if _WIN32_WINNT >= _WIN32_WINNT_WIN8
28 #    include <winapifamily.h>
29 #  endif
30 #else
31 #  if !defined(CLOCK_REALTIME)
32 #    include <sys/time.h>        // for gettimeofday and timeval
33 #  endif // !defined(CLOCK_REALTIME)
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 // system_clock
46 
47 const bool system_clock::is_steady;
48 
49 system_clock::time_point
50 system_clock::now() _NOEXCEPT
51 {
52 #if defined(_LIBCPP_WIN32API)
53   // FILETIME is in 100ns units
54   using filetime_duration =
55       _VSTD::chrono::duration<__int64,
56                               _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
57                                                     nanoseconds::period>>;
58 
59   // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
60   static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
61 
62   FILETIME ft;
63 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
64 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
65   GetSystemTimePreciseAsFileTime(&ft);
66 #else
67   GetSystemTimeAsFileTime(&ft);
68 #endif
69 #else
70   GetSystemTimeAsFileTime(&ft);
71 #endif
72 
73   filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
74                        static_cast<__int64>(ft.dwLowDateTime)};
75   return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
76 #else
77 #if defined(CLOCK_REALTIME)
78   struct timespec tp;
79   if (0 != clock_gettime(CLOCK_REALTIME, &tp))
80     __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
81   return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
82 #else
83     timeval tv;
84     gettimeofday(&tv, 0);
85     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
86 #endif // CLOCK_REALTIME
87 #endif
88 }
89 
90 time_t
91 system_clock::to_time_t(const time_point& t) _NOEXCEPT
92 {
93     return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
94 }
95 
96 system_clock::time_point
97 system_clock::from_time_t(time_t t) _NOEXCEPT
98 {
99     return system_clock::time_point(seconds(t));
100 }
101 
102 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
103 // steady_clock
104 //
105 // Warning:  If this is not truly steady, then it is non-conforming.  It is
106 //  better for it to not exist and have the rest of libc++ use system_clock
107 //  instead.
108 
109 const bool steady_clock::is_steady;
110 
111 #if defined(__APPLE__)
112 
113 #if !defined(CLOCK_MONOTONIC_RAW)
114 #  error "Building libc++ on Apple platforms requires CLOCK_MONOTONIC_RAW"
115 #endif
116 
117 // On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
118 // mach_absolute_time are able to time functions in the nanosecond range.
119 // Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
120 // also counts cycles when the system is asleep. Thus, it is the only
121 // acceptable implementation of steady_clock.
122 steady_clock::time_point
123 steady_clock::now() _NOEXCEPT
124 {
125     struct timespec tp;
126     if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
127         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
128     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
129 }
130 
131 #elif defined(_LIBCPP_WIN32API)
132 
133 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
134 //    If the function fails, the return value is zero. <snip>
135 //    On systems that run Windows XP or later, the function will always succeed
136 //      and will thus never return zero.
137 
138 static LARGE_INTEGER
139 __QueryPerformanceFrequency()
140 {
141 	LARGE_INTEGER val;
142 	(void) QueryPerformanceFrequency(&val);
143 	return val;
144 }
145 
146 steady_clock::time_point
147 steady_clock::now() _NOEXCEPT
148 {
149   static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
150 
151   LARGE_INTEGER counter;
152   (void) QueryPerformanceCounter(&counter);
153   return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
154 }
155 
156 #elif defined(CLOCK_MONOTONIC)
157 
158 steady_clock::time_point
159 steady_clock::now() _NOEXCEPT
160 {
161     struct timespec tp;
162     if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
163         __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
164     return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
165 }
166 
167 #else
168 #  error "Monotonic clock not implemented"
169 #endif
170 
171 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
172 
173 }
174 
175 _LIBCPP_END_NAMESPACE_STD
176