blob: c88224c61bdb589c73847e46c968310aa7dd443c [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===------------------------- chrono.cpp ---------------------------------===//
2//
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
Howard Hinnantc51e1022010-05-11 19:42:16 +00009#include "chrono"
Marshall Clowccb92572015-06-23 14:45:02 +000010#include "cerrno" // errno
11#include "system_error" // __throw_system_error
Louis Dionne678dc852020-02-12 17:01:19 +010012#include <time.h> // clock_gettime and CLOCK_{MONOTONIC,REALTIME,MONOTONIC_RAW}
Eric Fiselier7eba47e2018-07-25 20:51:49 +000013#include "include/apple_availability.h"
Ed Schoutenf7805c32015-05-14 20:54:18 +000014
John Brawnfeea69a2020-05-14 11:51:13 +010015#if __has_include(<unistd.h>)
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040016# include <unistd.h>
17#endif
18
19#if __has_include(<sys/time.h>)
20# include <sys/time.h> // for gettimeofday and timeval
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040021#endif
22
23#if !defined(__APPLE__) && _POSIX_TIMERS > 0
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040024# define _LIBCPP_USE_CLOCK_GETTIME
Mara Sophie Grosch1bd707a2020-05-07 12:10:33 -040025#endif
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000026
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000027#if defined(_LIBCPP_WIN32API)
Louis Dionne678dc852020-02-12 17:01:19 +010028# 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
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000034#endif // defined(_LIBCPP_WIN32API)
Ed Schoutenf7805c32015-05-14 20:54:18 +000035
Michał Górny8d676fb2019-12-02 11:49:20 +010036#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Louis Dionne678dc852020-02-12 17:01:19 +010037# pragma comment(lib, "rt")
Petr Hosek99575aa2019-05-30 01:34:41 +000038#endif
39
Howard Hinnantc51e1022010-05-11 19:42:16 +000040_LIBCPP_BEGIN_NAMESPACE_STD
41
42namespace chrono
43{
44
45// system_clock
46
Howard Hinnant2c45cb42012-12-12 21:14:28 +000047const bool system_clock::is_steady;
48
Howard Hinnantc51e1022010-05-11 19:42:16 +000049system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000050system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000051{
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +000052#if defined(_LIBCPP_WIN32API)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000053 // 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>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000058
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000059 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
Saleem Abdulrasool25c0d402017-01-02 18:41:50 +000060 static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000061
62 FILETIME ft;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000063#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
64#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000065 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000066#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000068#endif
69#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000070 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000071#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000072
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));
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000076#else
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040077#if defined(CLOCK_REALTIME) && defined(_LIBCPP_USE_CLOCK_GETTIME)
Eric Fiselier7eba47e2018-07-25 20:51:49 +000078 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));
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +000082#else
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 timeval tv;
84 gettimeofday(&tv, 0);
85 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Hafiz Abid Qadeerde28d5f2020-10-05 17:28:25 -040086#endif
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000087#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000088}
89
90time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000091system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000092{
93 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
94}
95
96system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000097system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000098{
99 return system_clock::time_point(seconds(t));
100}
101
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000102#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000103// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000104//
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.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000109const bool steady_clock::is_steady;
110
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000111#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000112
Louis Dionne678dc852020-02-12 17:01:19 +0100113#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.
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000122steady_clock::time_point
123steady_clock::now() _NOEXCEPT
124{
125 struct timespec tp;
Louis Dionnea2dac172020-02-10 18:30:43 +0100126 if (0 != clock_gettime(CLOCK_MONOTONIC_RAW, &tp))
127 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC_RAW) failed");
Bruno Cardoso Lopesd0027142017-01-09 19:21:48 +0000128 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
129}
130
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000131#elif defined(_LIBCPP_WIN32API)
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000132
Marshall Clow132ccac2019-04-01 17:23:30 +0000133// 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>
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -0700135// On systems that run Windows XP or later, the function will always succeed
Marshall Clow132ccac2019-04-01 17:23:30 +0000136// and will thus never return zero.
137
138static LARGE_INTEGER
139__QueryPerformanceFrequency()
140{
141 LARGE_INTEGER val;
142 (void) QueryPerformanceFrequency(&val);
143 return val;
144}
145
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000146steady_clock::time_point
147steady_clock::now() _NOEXCEPT
148{
Marshall Clow132ccac2019-04-01 17:23:30 +0000149 static const LARGE_INTEGER freq = __QueryPerformanceFrequency();
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000150
Marshall Clow5dbb0d22019-04-02 14:00:36 +0000151 LARGE_INTEGER counter;
152 (void) QueryPerformanceCounter(&counter);
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000153 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000154}
155
156#elif defined(CLOCK_MONOTONIC)
157
158steady_clock::time_point
159steady_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
Ed Schoutenf7805c32015-05-14 20:54:18 +0000167#else
Louis Dionne678dc852020-02-12 17:01:19 +0100168# error "Monotonic clock not implemented"
Ed Schoutenf7805c32015-05-14 20:54:18 +0000169#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000170
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000171#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
172
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173}
174
175_LIBCPP_END_NAMESPACE_STD