1 //===------------------------- chrono.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "cerrno" // errno
12 #include "system_error" // __throw_system_error
13 #include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
15 #if !defined(CLOCK_REALTIME)
16 #include <sys/time.h> // for gettimeofday and timeval
19 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) && !defined(CLOCK_MONOTONIC)
21 #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
23 #error "Monotonic clock not implemented"
27 _LIBCPP_BEGIN_NAMESPACE_STD
34 const bool system_clock::is_steady
;
36 system_clock::time_point
37 system_clock::now() _NOEXCEPT
41 if (0 != clock_gettime(CLOCK_REALTIME
, &tp
))
42 __throw_system_error(errno
, "clock_gettime(CLOCK_REALTIME) failed");
43 return time_point(seconds(tp
.tv_sec
) + microseconds(tp
.tv_nsec
/ 1000));
44 #else // !CLOCK_REALTIME
47 return time_point(seconds(tv
.tv_sec
) + microseconds(tv
.tv_usec
));
48 #endif // CLOCK_REALTIME
52 system_clock::to_time_t(const time_point
& t
) _NOEXCEPT
54 return time_t(duration_cast
<seconds
>(t
.time_since_epoch()).count());
57 system_clock::time_point
58 system_clock::from_time_t(time_t t
) _NOEXCEPT
60 return system_clock::time_point(seconds(t
));
63 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
66 // Warning: If this is not truly steady, then it is non-conforming. It is
67 // better for it to not exist and have the rest of libc++ use system_clock
70 const bool steady_clock::is_steady
;
72 #ifdef CLOCK_MONOTONIC
74 steady_clock::time_point
75 steady_clock::now() _NOEXCEPT
78 if (0 != clock_gettime(CLOCK_MONOTONIC
, &tp
))
79 __throw_system_error(errno
, "clock_gettime(CLOCK_MONOTONIC) failed");
80 return time_point(seconds(tp
.tv_sec
) + nanoseconds(tp
.tv_nsec
));
83 #elif defined(__APPLE__)
85 // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
86 // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
87 // are run time constants supplied by the OS. This clock has no relationship
88 // to the Gregorian calendar. It's main use is as a high resolution timer.
90 // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
91 // for that case as an optimization.
93 #pragma GCC visibility push(hidden)
99 return static_cast<steady_clock::rep
>(mach_absolute_time());
104 compute_steady_factor()
106 mach_timebase_info_data_t MachInfo
;
107 mach_timebase_info(&MachInfo
);
108 return static_cast<double>(MachInfo
.numer
) / MachInfo
.denom
;
115 static const double factor
= compute_steady_factor();
116 return static_cast<steady_clock::rep
>(mach_absolute_time() * factor
);
119 typedef steady_clock::rep (*FP
)();
125 mach_timebase_info_data_t MachInfo
;
126 mach_timebase_info(&MachInfo
);
127 if (MachInfo
.numer
== MachInfo
.denom
)
128 return &steady_simplified
;
132 #pragma GCC visibility pop
134 steady_clock::time_point
135 steady_clock::now() _NOEXCEPT
137 static FP fp
= init_steady_clock();
138 return time_point(duration(fp()));
142 #error "Monotonic clock not implemented"
145 #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
149 _LIBCPP_END_NAMESPACE_STD