Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- chrono.cpp ---------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "chrono" |
| 11 | #include <sys/time.h> //for gettimeofday and timeval |
Marshall Clow | dece7fe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 12 | #ifdef __APPLE__ |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 14 | #else /* !__APPLE__ */ |
| 15 | #include <cerrno> // errno |
| 16 | #include <system_error> // __throw_system_error |
| 17 | #include <time.h> // clock_gettime, CLOCK_MONOTONIC |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 18 | #endif // __APPLE__ |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | |
| 20 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 21 | |
| 22 | namespace chrono |
| 23 | { |
| 24 | |
| 25 | // system_clock |
| 26 | |
Howard Hinnant | 0a69fa1 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 27 | const bool system_clock::is_steady; |
| 28 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | system_clock::time_point |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 30 | system_clock::now() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | { |
| 32 | timeval tv; |
| 33 | gettimeofday(&tv, 0); |
| 34 | return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
| 35 | } |
| 36 | |
| 37 | time_t |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 38 | system_clock::to_time_t(const time_point& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | { |
| 40 | return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); |
| 41 | } |
| 42 | |
| 43 | system_clock::time_point |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 44 | system_clock::from_time_t(time_t t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | { |
| 46 | return system_clock::time_point(seconds(t)); |
| 47 | } |
| 48 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 49 | // steady_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | |
Howard Hinnant | 0a69fa1 | 2012-12-12 21:14:28 +0000 | [diff] [blame] | 51 | const bool steady_clock::is_steady; |
| 52 | |
Marshall Clow | dece7fe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 53 | #ifdef __APPLE__ |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of |
| 55 | // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom |
| 56 | // are run time constants supplied by the OS. This clock has no relationship |
| 57 | // to the Gregorian calendar. It's main use is as a high resolution timer. |
| 58 | |
| 59 | // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize |
| 60 | // for that case as an optimization. |
| 61 | |
| 62 | #pragma GCC visibility push(hidden) |
| 63 | |
| 64 | static |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 65 | steady_clock::rep |
| 66 | steady_simplified() |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 67 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 68 | return static_cast<steady_clock::rep>(mach_absolute_time()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static |
| 72 | double |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 73 | compute_steady_factor() |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | { |
| 75 | mach_timebase_info_data_t MachInfo; |
| 76 | mach_timebase_info(&MachInfo); |
| 77 | return static_cast<double>(MachInfo.numer) / MachInfo.denom; |
| 78 | } |
| 79 | |
| 80 | static |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 81 | steady_clock::rep |
| 82 | steady_full() |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 84 | static const double factor = compute_steady_factor(); |
| 85 | return static_cast<steady_clock::rep>(mach_absolute_time() * factor); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 88 | typedef steady_clock::rep (*FP)(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | |
| 90 | static |
| 91 | FP |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 92 | init_steady_clock() |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | { |
| 94 | mach_timebase_info_data_t MachInfo; |
| 95 | mach_timebase_info(&MachInfo); |
| 96 | if (MachInfo.numer == MachInfo.denom) |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 97 | return &steady_simplified; |
| 98 | return &steady_full; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Daniel Dunbar | 04acaca | 2010-09-04 03:15:51 +0000 | [diff] [blame] | 101 | #pragma GCC visibility pop |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 102 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 103 | steady_clock::time_point |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 104 | steady_clock::now() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 106 | static FP fp = init_steady_clock(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | return time_point(duration(fp())); |
| 108 | } |
| 109 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 110 | #else // __APPLE__ |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 111 | // FIXME: We assume that clock_gettime(CLOCK_MONOTONIC) works on |
| 112 | // non-apple systems. Instead, we should check _POSIX_TIMERS and |
| 113 | // _POSIX_MONOTONIC_CLOCK and fall back to something else if those |
| 114 | // don't exist. |
| 115 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 116 | // Warning: If this is not truly steady, then it is non-conforming. It is |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 117 | // better for it to not exist and have the rest of libc++ use system_clock |
| 118 | // instead. |
| 119 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 120 | steady_clock::time_point |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 121 | steady_clock::now() _NOEXCEPT |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 122 | { |
| 123 | struct timespec tp; |
| 124 | if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) |
| 125 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); |
| 126 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 127 | } |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 128 | #endif // __APPLE__ |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 129 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | _LIBCPP_END_NAMESPACE_STD |