blob: b9d6bf97be33aaf9a520345c47f3335a825b9292 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===------------------------- chrono.cpp ---------------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
Howard Hinnantee11c312010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
Eric Fiselierfc779202016-07-01 23:31:55 +00009
Howard Hinnantc51e1022010-05-11 19:42:16 +000010#include "chrono"
Marshall Clowccb92572015-06-23 14:45:02 +000011#include "cerrno" // errno
12#include "system_error" // __throw_system_error
13#include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
Ed Schoutenf7805c32015-05-14 20:54:18 +000014
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000015#if defined(_WIN32)
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
Ed Schoutenf7805c32015-05-14 20:54:18 +000023#if !defined(CLOCK_REALTIME)
24#include <sys/time.h> // for gettimeofday and timeval
25#endif
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000026#endif
Ed Schoutenf7805c32015-05-14 20:54:18 +000027
Saleem Abdulrasoolae249922017-01-01 20:20:43 +000028#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
Ed Schoutenf7805c32015-05-14 20:54:18 +000029#if __APPLE__
30#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
Saleem Abdulrasoolae249922017-01-01 20:20:43 +000031#elif !defined(_WIN32) && !defined(CLOCK_MONOTONIC)
Ed Schoutenf7805c32015-05-14 20:54:18 +000032#error "Monotonic clock not implemented"
33#endif
34#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000035
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38namespace chrono
39{
40
41// system_clock
42
Howard Hinnant2c45cb42012-12-12 21:14:28 +000043const bool system_clock::is_steady;
44
Howard Hinnantc51e1022010-05-11 19:42:16 +000045system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000046system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000047{
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000048#if defined(_WIN32)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000049 // 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>>;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000054
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000055 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
56 static _LIBCPP_CONSTEXPR const filetime_duration
57 nt_to_unix_epoch{11644473600};
58
59 FILETIME ft;
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000060#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
61#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000062 GetSystemTimePreciseAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000063#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000064 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000065#endif
66#else
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000067 GetSystemTimeAsFileTime(&ft);
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000068#endif
Saleem Abdulrasool9e367702017-01-01 22:04:38 +000069
70 filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
71 static_cast<__int64>(ft.dwLowDateTime)};
72 return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000073#else
Ed Schoutenf7805c32015-05-14 20:54:18 +000074#ifdef CLOCK_REALTIME
75 struct timespec tp;
76 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
77 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
78 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
79#else // !CLOCK_REALTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000080 timeval tv;
81 gettimeofday(&tv, 0);
82 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Ed Schoutenf7805c32015-05-14 20:54:18 +000083#endif // CLOCK_REALTIME
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000084#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000085}
86
87time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000088system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000089{
90 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
91}
92
93system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000094system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000095{
96 return system_clock::time_point(seconds(t));
97}
98
Jonathan Roelofscce96eb2014-09-02 21:14:38 +000099#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000100// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +0000101//
102// Warning: If this is not truly steady, then it is non-conforming. It is
103// better for it to not exist and have the rest of libc++ use system_clock
104// instead.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000106const bool steady_clock::is_steady;
107
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000108#if defined(__APPLE__)
Ed Schoutenf7805c32015-05-14 20:54:18 +0000109
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
111// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
112// are run time constants supplied by the OS. This clock has no relationship
113// to the Gregorian calendar. It's main use is as a high resolution timer.
114
115// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
116// for that case as an optimization.
117
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000119steady_clock::rep
120steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121{
Howard Hinnant28b24882011-12-01 20:21:04 +0000122 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123}
124
125static
126double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000127compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128{
129 mach_timebase_info_data_t MachInfo;
130 mach_timebase_info(&MachInfo);
131 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
132}
133
134static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000135steady_clock::rep
136steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000138 static const double factor = compute_steady_factor();
139 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140}
141
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000142typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144static
145FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000146init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147{
148 mach_timebase_info_data_t MachInfo;
149 mach_timebase_info(&MachInfo);
150 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000151 return &steady_simplified;
152 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153}
154
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000155steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000156steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000158 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 return time_point(duration(fp()));
160}
161
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000162#elif defined(_WIN32)
163
164steady_clock::time_point
165steady_clock::now() _NOEXCEPT
166{
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000167 static LARGE_INTEGER freq;
168 static BOOL initialized = FALSE;
169 if (!initialized)
170 initialized = QueryPerformanceFrequency(&freq); // always succceeds
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000171
Saleem Abdulrasool91e1d612017-01-01 22:04:36 +0000172 LARGE_INTEGER counter;
173 QueryPerformanceCounter(&counter);
174 return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
Saleem Abdulrasoolae249922017-01-01 20:20:43 +0000175}
176
177#elif defined(CLOCK_MONOTONIC)
178
179steady_clock::time_point
180steady_clock::now() _NOEXCEPT
181{
182 struct timespec tp;
183 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
184 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
185 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
186}
187
Ed Schoutenf7805c32015-05-14 20:54:18 +0000188#else
189#error "Monotonic clock not implemented"
190#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000191
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000192#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
193
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194}
195
196_LIBCPP_END_NAMESPACE_STD