Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame^] | 1 | //===------------------------- chrono.cpp ---------------------------------===// |
| 2 | // |
| 3 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 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 |
| 12 | #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t |
| 13 | |
| 14 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 15 | |
| 16 | namespace chrono |
| 17 | { |
| 18 | |
| 19 | // system_clock |
| 20 | |
| 21 | system_clock::time_point |
| 22 | system_clock::now() |
| 23 | { |
| 24 | timeval tv; |
| 25 | gettimeofday(&tv, 0); |
| 26 | return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); |
| 27 | } |
| 28 | |
| 29 | time_t |
| 30 | system_clock::to_time_t(const time_point& t) |
| 31 | { |
| 32 | return time_t(duration_cast<seconds>(t.time_since_epoch()).count()); |
| 33 | } |
| 34 | |
| 35 | system_clock::time_point |
| 36 | system_clock::from_time_t(time_t t) |
| 37 | { |
| 38 | return system_clock::time_point(seconds(t)); |
| 39 | } |
| 40 | |
| 41 | // monotonic_clock |
| 42 | |
| 43 | // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of |
| 44 | // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom |
| 45 | // are run time constants supplied by the OS. This clock has no relationship |
| 46 | // to the Gregorian calendar. It's main use is as a high resolution timer. |
| 47 | |
| 48 | // MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize |
| 49 | // for that case as an optimization. |
| 50 | |
| 51 | #pragma GCC visibility push(hidden) |
| 52 | |
| 53 | static |
| 54 | monotonic_clock::rep |
| 55 | monotonic_simplified() |
| 56 | { |
| 57 | return mach_absolute_time(); |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | double |
| 62 | compute_monotonic_factor() |
| 63 | { |
| 64 | mach_timebase_info_data_t MachInfo; |
| 65 | mach_timebase_info(&MachInfo); |
| 66 | return static_cast<double>(MachInfo.numer) / MachInfo.denom; |
| 67 | } |
| 68 | |
| 69 | static |
| 70 | monotonic_clock::rep |
| 71 | monotonic_full() |
| 72 | { |
| 73 | static const double factor = compute_monotonic_factor(); |
| 74 | return static_cast<monotonic_clock::rep>(mach_absolute_time() * factor); |
| 75 | } |
| 76 | |
| 77 | typedef monotonic_clock::rep (*FP)(); |
| 78 | |
| 79 | static |
| 80 | FP |
| 81 | init_monotonic_clock() |
| 82 | { |
| 83 | mach_timebase_info_data_t MachInfo; |
| 84 | mach_timebase_info(&MachInfo); |
| 85 | if (MachInfo.numer == MachInfo.denom) |
| 86 | return &monotonic_simplified; |
| 87 | return &monotonic_full; |
| 88 | } |
| 89 | |
| 90 | #pragma GCC visiblity pop |
| 91 | |
| 92 | monotonic_clock::time_point |
| 93 | monotonic_clock::now() |
| 94 | { |
| 95 | static FP fp = init_monotonic_clock(); |
| 96 | return time_point(duration(fp())); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | _LIBCPP_END_NAMESPACE_STD |