blob: 4f2d51042ff10afd76e6da41d9fd5d3601a16950 [file] [log] [blame]
Louis Dionne9bd93882021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00002//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
Eric Fiselierfc779202016-07-01 23:31:55 +00008
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +00009#if defined(__MVS__)
10// As part of monotonic clock support on z/OS we need macro _LARGE_TIME_API
11// to be defined before any system header to include definition of struct timespec64.
12#define _LARGE_TIME_API
13#endif
14
Howard Hinnantc51e1022010-05-11 19:42:16 +000015#include "chrono"
Marshall Clowccb92572015-06-23 14:45:02 +000016#include "cerrno" // errno
17#include "system_error" // __throw_system_error
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +000018
19#if defined(__MVS__)
20#include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic
21#endif
22
Louis Dionne678dc852020-02-12 17:01:19 +010023#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
Eric Fiselier7eba47e2018-07-25 20:51:49 +000024#include "include/apple_availability.h"
Ed Schoutenf7805c32015-05-14 20:54:18 +000025
John Brawnfeea69a2020-05-14 11:51:13 +010026#if __has_include(<unistd.h>)
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040027# include <unistd.h>
28#endif
29
30#if __has_include(<sys/time.h>)
31# include <sys/time.h> // for gettimeofday and timeval
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040032#endif
33
Martin Storsjö2567ea82021-06-16 15:22:28 +030034#if !defined(__APPLE__) && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040035# define _LIBCPP_USE_CLOCK_GETTIME
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040036#endif
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000037
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000038#if defined(_LIBCPP_WIN32API)
Louis Dionne678dc852020-02-12 17:01:19 +010039# define WIN32_LEAN_AND_MEAN
40# define VC_EXTRA_LEAN
41# include <windows.h>
42# if _WIN32_WINNT >= _WIN32_WINNT_WIN8
43# include <winapifamily.h>
44# endif
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000045#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000046
Roland McGrath7cb0f472022-01-04 10:25:20 -080047#if defined(__Fuchsia__)
48# include <zircon/syscalls.h>
49#endif
50
Louis Dionne69897ab2021-01-21 17:53:29 -050051#if __has_include(<mach/mach_time.h>)
52# include <mach/mach_time.h>
53#endif
54
Michał Górny8d676fb2019-12-02 11:49:20 +010055#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Louis Dionne678dc852020-02-12 17:01:19 +010056# pragma comment(lib, "rt")
Petr Hosek99575aa2019-05-30 01:34:41 +000057#endif
58
Howard Hinnantc51e1022010-05-11 19:42:16 +000059_LIBCPP_BEGIN_NAMESPACE_STD
60
61namespace chrono
62{
63
Louis Dionnedf6a9a82020-11-04 10:14:13 -050064//
Howard Hinnantc51e1022010-05-11 19:42:16 +000065// system_clock
Louis Dionnedf6a9a82020-11-04 10:14:13 -050066//
Howard Hinnantc51e1022010-05-11 19:42:16 +000067
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000068#if defined(_LIBCPP_WIN32API)
Louis Dionnedf6a9a82020-11-04 10:14:13 -050069
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +030070#if _WIN32_WINNT < _WIN32_WINNT_WIN8
71
72namespace {
73
74typedef void(WINAPI *GetSystemTimeAsFileTimePtr)(LPFILETIME);
75
76class GetSystemTimeInit {
77public:
78 GetSystemTimeInit() {
79 fp = (GetSystemTimeAsFileTimePtr)GetProcAddress(
80 GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
81 if (fp == nullptr)
82 fp = GetSystemTimeAsFileTime;
83 }
84 GetSystemTimeAsFileTimePtr fp;
85};
86
Louis Dionne04439612021-12-08 08:26:27 -050087// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
88// attribute with a value that's reserved for the implementation (we're the implementation).
89#include "chrono_system_time_init.h"
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +030090} // namespace
91
92#endif
93
Louis Dionnedf6a9a82020-11-04 10:14:13 -050094static system_clock::time_point __libcpp_system_clock_now() {
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000095 // FILETIME is in 100ns units
96 using filetime_duration =
97 _VSTD::chrono::duration<__int64,
98 _VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
99 nanoseconds::period>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +0000100
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000101 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +0000102 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000103
104 FILETIME ft;
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +0300105#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) || \
106 (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000107 GetSystemTimePreciseAsFileTime(&ft);
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +0300108#elif !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000109 GetSystemTimeAsFileTime(&ft);
Sizhe Zhaoa6efafc2021-08-26 23:11:54 +0300110#else
111 GetSystemTimeAsFileTimeFunc.fp(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +0000112#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +0000113
114 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
115 static_cast<__int64>(ft.dwLowDateTime)};
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500116 return system_clock::time_point(duration_cast<system_clock::duration>(d - nt_to_unix_epoch));
117}
118
119#elif defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
120
121static system_clock::time_point __libcpp_system_clock_now() {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000122 struct timespec tp;
123 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
124 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500125 return system_clock::time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
126}
127
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000128#else
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500129
130static system_clock::time_point __libcpp_system_clock_now() {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 timeval tv;
132 gettimeofday(&tv, 0);
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500133 return system_clock::time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
134}
135
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -0400136#endif
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500137
138const bool system_clock::is_steady;
139
140system_clock::time_point
Louis Dionne65358e12021-03-01 12:09:45 -0500141system_clock::now() noexcept
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500142{
143 return __libcpp_system_clock_now();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144}
145
146time_t
Louis Dionne65358e12021-03-01 12:09:45 -0500147system_clock::to_time_t(const time_point& t) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148{
149 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
150}
151
152system_clock::time_point
Louis Dionne65358e12021-03-01 12:09:45 -0500153system_clock::from_time_t(time_t t) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154{
155 return system_clock::time_point(seconds(t));
156}
157
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500158//
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000159// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000160//
161// Warning: If this is not truly steady, then it is non-conforming. It is
162// better for it to not exist and have the rest of libc++ use system_clock
163// instead.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500164//
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500166#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000167
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000168#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000169
Louis Dionne69897ab2021-01-21 17:53:29 -0500170// TODO(ldionne):
171// This old implementation of steady_clock is retained until Chrome drops supports
172// for macOS < 10.12. The issue is that they link libc++ statically into their
173// application, which means that libc++ must support being built for such deployment
174// targets. See https://llvm.org/D74489 for details.
175#if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
176 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
177 (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
178 (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
179# define _LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME
180#endif
181
182#if defined(_LIBCPP_USE_OLD_MACH_ABSOLUTE_TIME)
183
184// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
185// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
186// are run time constants supplied by the OS. This clock has no relationship
187// to the Gregorian calendar. It's main use is as a high resolution timer.
188
189// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
190// for that case as an optimization.
191
192static steady_clock::rep steady_simplified() {
193 return static_cast<steady_clock::rep>(mach_absolute_time());
194}
195static double compute_steady_factor() {
196 mach_timebase_info_data_t MachInfo;
197 mach_timebase_info(&MachInfo);
198 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
199}
200
201static steady_clock::rep steady_full() {
202 static const double factor = compute_steady_factor();
203 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
204}
205
206typedef steady_clock::rep (*FP)();
207
208static FP init_steady_clock() {
209 mach_timebase_info_data_t MachInfo;
210 mach_timebase_info(&MachInfo);
211 if (MachInfo.numer == MachInfo.denom)
212 return &steady_simplified;
213 return &steady_full;
214}
215
216static steady_clock::time_point __libcpp_steady_clock_now() {
217 static FP fp = init_steady_clock();
218 return steady_clock::time_point(steady_clock::duration(fp()));
219}
220
221#else // vvvvv default behavior for Apple platforms vvvvv
222
Louis Dionne678dc852020-02-12 17:01:19 +0100223// On Apple platforms, only CLOCK_UPTIME_RAW, CLOCK_MONOTONIC_RAW or
224// mach_absolute_time are able to time functions in the nanosecond range.
225// Furthermore, only CLOCK_MONOTONIC_RAW is truly monotonic, because it
226// also counts cycles when the system is asleep. Thus, it is the only
227// acceptable implementation of steady_clock.
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500228static steady_clock::time_point __libcpp_steady_clock_now() {
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000229 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100230 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
231 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500232 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000233}
234
Louis Dionne69897ab2021-01-21 17:53:29 -0500235#endif
236
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000237#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000238
Marshall Clow132ccac2019-04-01 17:23:30 +0000239// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx says:
240// If the function fails, the return value is zero. <snip>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700241// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000242// and will thus never return zero.
243
244static LARGE_INTEGER
245__QueryPerformanceFrequency()
246{
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500247 LARGE_INTEGER val;
248 (void) QueryPerformanceFrequency(&val);
249 return val;
Marshall Clow132ccac2019-04-01 17:23:30 +0000250}
251
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500252static steady_clock::time_point __libcpp_steady_clock_now() {
Marshall Clow132ccac2019-04-01 17:23:30 +0000253 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000254
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000255 LARGE_INTEGER counter;
256 (void) QueryPerformanceCounter(&counter);
Martin Storsjö94011152020-12-17 15:40:06 +0200257 auto seconds = counter.QuadPart / freq.QuadPart;
258 auto fractions = counter.QuadPart % freq.QuadPart;
259 auto dur = seconds * nano::den + fractions * nano::den / freq.QuadPart;
260 return steady_clock::time_point(steady_clock::duration(dur));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000261}
262
Zbigniew Sarbinowski15410732021-02-12 18:36:15 +0000263#elif defined(__MVS__)
264
265static steady_clock::time_point __libcpp_steady_clock_now() {
266 struct timespec64 ts;
267 if (0 != gettimeofdayMonotonic(&ts))
268 __throw_system_error(errno, "failed to obtain time of day");
269
270 return steady_clock::time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
271}
272
Roland McGrath7cb0f472022-01-04 10:25:20 -0800273# elif defined(__Fuchsia__)
274
275static steady_clock::time_point __libcpp_steady_clock_now() noexcept {
276 // Implicitly link against the vDSO system call ABI without
277 // requiring the final link to specify -lzircon explicitly when
278 // statically linking libc++.
279# pragma comment(lib, "zircon")
280
281 return steady_clock::time_point(nanoseconds(_zx_clock_get_monotonic()));
282}
283
284# elif defined(CLOCK_MONOTONIC)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000285
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500286static steady_clock::time_point __libcpp_steady_clock_now() {
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000287 struct timespec tp;
288 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
289 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500290 return steady_clock::time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000291}
292
Roland McGrath7cb0f472022-01-04 10:25:20 -0800293# else
294# error "Monotonic clock not implemented on this platform"
295# endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000296
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500297const bool steady_clock::is_steady;
298
299steady_clock::time_point
Louis Dionne65358e12021-03-01 12:09:45 -0500300steady_clock::now() noexcept
Louis Dionnedf6a9a82020-11-04 10:14:13 -0500301{
302 return __libcpp_steady_clock_now();
303}
304
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000305#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
306
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307}
308
309_LIBCPP_END_NAMESPACE_STD