blob: 578b0a9017b0d8a0ce3685bdc2244a91041914d6 [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
28#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) && !defined(CLOCK_MONOTONIC)
29#if __APPLE__
30#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
31#else
32#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)
49 // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970. The difference
50 // in nanoseconds is the windows epoch offset.
51 static const constexpr __int64 kWindowsEpochOffset = 0x19db1ded53e8000;
52
53 FILETIME ftSystemTime;
54#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
55#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
56 GetSystemTimePreciseAsFileTime(&ftSystemTime);
57#else
58 GetSystemTimeAsFileTime(&ftSystemTime);
59#endif
60#else
61 GetSystemTimeAsFileTime(&ftSystemTime);
62#endif
63 __int64 llWinTimeNS =
64 ((static_cast<__int64>(ftSystemTime.dwHighDateTime) << 32) |
65 static_cast<__int64>(ftSystemTime.dwLowDateTime)) *
66 100;
67 return time_point(duration_cast<duration>(
68 (nanoseconds(llWinTimeNS - kWindowsEpochOffset))));
69#else
Ed Schoutenf7805c32015-05-14 20:54:18 +000070#ifdef CLOCK_REALTIME
71 struct timespec tp;
72 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
73 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
74 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
75#else // !CLOCK_REALTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000076 timeval tv;
77 gettimeofday(&tv, 0);
78 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Ed Schoutenf7805c32015-05-14 20:54:18 +000079#endif // CLOCK_REALTIME
Saleem Abdulrasool594b0422017-01-01 20:20:41 +000080#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000081}
82
83time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000084system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000085{
86 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
87}
88
89system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000090system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000091{
92 return system_clock::time_point(seconds(t));
93}
94
Jonathan Roelofscce96eb2014-09-02 21:14:38 +000095#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +000096// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +000097//
98// Warning: If this is not truly steady, then it is non-conforming. It is
99// better for it to not exist and have the rest of libc++ use system_clock
100// instead.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101
Howard Hinnant2c45cb42012-12-12 21:14:28 +0000102const bool steady_clock::is_steady;
103
Ed Schoutenf7805c32015-05-14 20:54:18 +0000104#ifdef CLOCK_MONOTONIC
105
106steady_clock::time_point
107steady_clock::now() _NOEXCEPT
108{
109 struct timespec tp;
110 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
111 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
112 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
113}
114
115#elif defined(__APPLE__)
116
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
118// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
119// are run time constants supplied by the OS. This clock has no relationship
120// to the Gregorian calendar. It's main use is as a high resolution timer.
121
122// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
123// for that case as an optimization.
124
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000126steady_clock::rep
127steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128{
Howard Hinnant28b24882011-12-01 20:21:04 +0000129 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130}
131
132static
133double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000134compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135{
136 mach_timebase_info_data_t MachInfo;
137 mach_timebase_info(&MachInfo);
138 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
139}
140
141static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000142steady_clock::rep
143steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000145 static const double factor = compute_steady_factor();
146 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147}
148
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000149typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150
151static
152FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000153init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154{
155 mach_timebase_info_data_t MachInfo;
156 mach_timebase_info(&MachInfo);
157 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000158 return &steady_simplified;
159 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160}
161
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000162steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000163steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000165 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 return time_point(duration(fp()));
167}
168
Ed Schoutenf7805c32015-05-14 20:54:18 +0000169#else
170#error "Monotonic clock not implemented"
171#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000172
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000173#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
174
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175}
176
177_LIBCPP_END_NAMESPACE_STD