blob: 266eed84ea8e645c778b3babbba9575e4bc19118 [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
15#if !defined(CLOCK_REALTIME)
16#include <sys/time.h> // for gettimeofday and timeval
17#endif
18
19#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) && !defined(CLOCK_MONOTONIC)
20#if __APPLE__
21#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
22#else
23#error "Monotonic clock not implemented"
24#endif
25#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000026
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29namespace chrono
30{
31
32// system_clock
33
Howard Hinnant2c45cb42012-12-12 21:14:28 +000034const bool system_clock::is_steady;
Eric Fiseliere2a74402016-07-01 23:22:25 +000035// Make is_steady non-discardable in C++17
36// See PR28395 (https://llvm.org/bugs/show_bug.cgi?id=28395)
37static const bool& __is_steady_force_use1 __attribute__((used)) = system_clock::is_steady;
Howard Hinnant2c45cb42012-12-12 21:14:28 +000038
Howard Hinnantc51e1022010-05-11 19:42:16 +000039system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000040system_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000041{
Ed Schoutenf7805c32015-05-14 20:54:18 +000042#ifdef CLOCK_REALTIME
43 struct timespec tp;
44 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
45 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
46 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
47#else // !CLOCK_REALTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 timeval tv;
49 gettimeofday(&tv, 0);
50 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
Ed Schoutenf7805c32015-05-14 20:54:18 +000051#endif // CLOCK_REALTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000052}
53
54time_t
Howard Hinnantaa54ac42011-05-28 18:34:36 +000055system_clock::to_time_t(const time_point& t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000056{
57 return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
58}
59
60system_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +000061system_clock::from_time_t(time_t t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000062{
63 return system_clock::time_point(seconds(t));
64}
65
Jonathan Roelofscce96eb2014-09-02 21:14:38 +000066#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnantc8dbd222010-11-20 19:16:30 +000067// steady_clock
Ed Schoutenf7805c32015-05-14 20:54:18 +000068//
69// Warning: If this is not truly steady, then it is non-conforming. It is
70// better for it to not exist and have the rest of libc++ use system_clock
71// instead.
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Howard Hinnant2c45cb42012-12-12 21:14:28 +000073const bool steady_clock::is_steady;
Eric Fiseliere2a74402016-07-01 23:22:25 +000074// Make is_steady non-discardable in C++17
75// See PR28395 (https://llvm.org/bugs/show_bug.cgi?id=28395)
76static const bool& __is_steady_force_use2 __attribute__((used)) = steady_clock::is_steady;
77
Howard Hinnant2c45cb42012-12-12 21:14:28 +000078
Ed Schoutenf7805c32015-05-14 20:54:18 +000079#ifdef CLOCK_MONOTONIC
80
81steady_clock::time_point
82steady_clock::now() _NOEXCEPT
83{
84 struct timespec tp;
85 if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
86 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
87 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
88}
89
90#elif defined(__APPLE__)
91
Howard Hinnantc51e1022010-05-11 19:42:16 +000092// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
93// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
94// are run time constants supplied by the OS. This clock has no relationship
95// to the Gregorian calendar. It's main use is as a high resolution timer.
96
97// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
98// for that case as an optimization.
99
100#pragma GCC visibility push(hidden)
101
102static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000103steady_clock::rep
104steady_simplified()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
Howard Hinnant28b24882011-12-01 20:21:04 +0000106 return static_cast<steady_clock::rep>(mach_absolute_time());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107}
108
109static
110double
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000111compute_steady_factor()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112{
113 mach_timebase_info_data_t MachInfo;
114 mach_timebase_info(&MachInfo);
115 return static_cast<double>(MachInfo.numer) / MachInfo.denom;
116}
117
118static
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000119steady_clock::rep
120steady_full()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000122 static const double factor = compute_steady_factor();
123 return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124}
125
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000126typedef steady_clock::rep (*FP)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
128static
129FP
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000130init_steady_clock()
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131{
132 mach_timebase_info_data_t MachInfo;
133 mach_timebase_info(&MachInfo);
134 if (MachInfo.numer == MachInfo.denom)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000135 return &steady_simplified;
136 return &steady_full;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137}
138
Daniel Dunbar063d0732010-09-04 03:15:51 +0000139#pragma GCC visibility pop
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000141steady_clock::time_point
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000142steady_clock::now() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000144 static FP fp = init_steady_clock();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145 return time_point(duration(fp()));
146}
147
Ed Schoutenf7805c32015-05-14 20:54:18 +0000148#else
149#error "Monotonic clock not implemented"
150#endif
Howard Hinnant155c2af2010-05-24 17:49:41 +0000151
Jonathan Roelofscce96eb2014-09-02 21:14:38 +0000152#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
153
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154}
155
156_LIBCPP_END_NAMESPACE_STD