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 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "chrono" |
| 11 | #include <sys/time.h> //for gettimeofday and timeval |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 12 | #if __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 |
| 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 | |
| 27 | system_clock::time_point |
| 28 | system_clock::now() |
| 29 | { |
| 30 | timeval tv; |
| 31 | gettimeofday(&tv, 0); |
| 32 | return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
| 33 | } |
| 34 | |
| 35 | time_t |
| 36 | system_clock::to_time_t(const time_point& t) |
| 37 | { |
| 38 | return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); |
| 39 | } |
| 40 | |
| 41 | system_clock::time_point |
| 42 | system_clock::from_time_t(time_t t) |
| 43 | { |
| 44 | return system_clock::time_point(seconds(t)); |
| 45 | } |
| 46 | |
| 47 | // monotonic_clock |
| 48 | |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 49 | #if __APPLE__ |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of |
| 51 | // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom |
| 52 | // are run time constants supplied by the OS. This clock has no relationship |
| 53 | // to the Gregorian calendar. It's main use is as a high resolution timer. |
| 54 | |
| 55 | // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize |
| 56 | // for that case as an optimization. |
| 57 | |
| 58 | #pragma GCC visibility push(hidden) |
| 59 | |
| 60 | static |
| 61 | monotonic_clock::rep |
| 62 | monotonic_simplified() |
| 63 | { |
| 64 | return mach_absolute_time(); |
| 65 | } |
| 66 | |
| 67 | static |
| 68 | double |
| 69 | compute_monotonic_factor() |
| 70 | { |
| 71 | mach_timebase_info_data_t MachInfo; |
| 72 | mach_timebase_info(&MachInfo); |
| 73 | return static_cast<double>(MachInfo.numer) / MachInfo.denom; |
| 74 | } |
| 75 | |
| 76 | static |
| 77 | monotonic_clock::rep |
| 78 | monotonic_full() |
| 79 | { |
| 80 | static const double factor = compute_monotonic_factor(); |
| 81 | return static_cast<monotonic_clock::rep>(mach_absolute_time() * factor); |
| 82 | } |
| 83 | |
| 84 | typedef monotonic_clock::rep (*FP)(); |
| 85 | |
| 86 | static |
| 87 | FP |
| 88 | init_monotonic_clock() |
| 89 | { |
| 90 | mach_timebase_info_data_t MachInfo; |
| 91 | mach_timebase_info(&MachInfo); |
| 92 | if (MachInfo.numer == MachInfo.denom) |
| 93 | return &monotonic_simplified; |
| 94 | return &monotonic_full; |
| 95 | } |
| 96 | |
| 97 | #pragma GCC visiblity pop |
| 98 | |
| 99 | monotonic_clock::time_point |
| 100 | monotonic_clock::now() |
| 101 | { |
| 102 | static FP fp = init_monotonic_clock(); |
| 103 | return time_point(duration(fp())); |
| 104 | } |
| 105 | |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 106 | #else /* !APPLE */ |
| 107 | // FIXME: We assume that clock_gettime(CLOCK_MONOTONIC) works on |
| 108 | // non-apple systems. Instead, we should check _POSIX_TIMERS and |
| 109 | // _POSIX_MONOTONIC_CLOCK and fall back to something else if those |
| 110 | // don't exist. |
| 111 | |
| 112 | // Warning: If this is not truly monotonic, then it is non-conforming. It is |
| 113 | // better for it to not exist and have the rest of libc++ use system_clock |
| 114 | // instead. |
| 115 | |
| 116 | monotonic_clock::time_point |
| 117 | monotonic_clock::now() |
| 118 | { |
| 119 | struct timespec tp; |
| 120 | if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) |
| 121 | __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); |
| 122 | return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); |
| 123 | } |
| 124 | #endif /* APPLE */ |
| 125 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | _LIBCPP_END_NAMESPACE_STD |