diff options
author | Ed Schouten <ed@nuxi.nl> | 2015-05-14 20:54:18 +0000 |
---|---|---|
committer | Ed Schouten <ed@nuxi.nl> | 2015-05-14 20:54:18 +0000 |
commit | 4f0f708bf1a6f2604ce15929ad4deb10f462a85f (patch) | |
tree | 272468a6cbf2617551503649c52c30880cc85ebc /libcxx | |
parent | 4936030ed38ff30852e056927a29df854105bcf8 (diff) | |
download | bcm5719-llvm-4f0f708bf1a6f2604ce15929ad4deb10f462a85f.tar.gz bcm5719-llvm-4f0f708bf1a6f2604ce15929ad4deb10f462a85f.zip |
Use clock_gettime()'s CLOCK_REALTIME instead of gettimeofday().
The system_clock::now() function currently uses gettimeofday(). The
problem with gettimeofday() is that it is an obsolete XSI function,
hence unavailable on CloudABI. See:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html
Change this code to use clock_gettime() with CLOCK_REALTIME instead,
which is more consistent, as clock_gettime() is already used for
steady_clock.
A previous version of this change actually attempted to change
system_clock::duration, but I reverted this part as it breaks the
existing ABI.
Differential Revision: http://reviews.llvm.org/D8253
Approved by: jroelofs
llvm-svn: 237390
Diffstat (limited to 'libcxx')
-rw-r--r-- | libcxx/src/chrono.cpp | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp index 456941144e0..57c74175b3f 100644 --- a/libcxx/src/chrono.cpp +++ b/libcxx/src/chrono.cpp @@ -8,14 +8,21 @@ //===----------------------------------------------------------------------===// #include "chrono" -#include <sys/time.h> //for gettimeofday and timeval -#ifdef __APPLE__ -#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t -#else /* !__APPLE__ */ #include <cerrno> // errno #include <system_error> // __throw_system_error -#include <time.h> // clock_gettime, CLOCK_MONOTONIC -#endif // __APPLE__ +#include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME + +#if !defined(CLOCK_REALTIME) +#include <sys/time.h> // for gettimeofday and timeval +#endif + +#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) && !defined(CLOCK_MONOTONIC) +#if __APPLE__ +#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t +#else +#error "Monotonic clock not implemented" +#endif +#endif _LIBCPP_BEGIN_NAMESPACE_STD @@ -29,9 +36,16 @@ const bool system_clock::is_steady; system_clock::time_point system_clock::now() _NOEXCEPT { +#ifdef CLOCK_REALTIME + struct timespec tp; + if (0 != clock_gettime(CLOCK_REALTIME, &tp)) + __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); + return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000)); +#else // !CLOCK_REALTIME timeval tv; gettimeofday(&tv, 0); return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec)); +#endif // CLOCK_REALTIME } time_t @@ -48,10 +62,26 @@ system_clock::from_time_t(time_t t) _NOEXCEPT #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK // steady_clock +// +// Warning: If this is not truly steady, then it is non-conforming. It is +// better for it to not exist and have the rest of libc++ use system_clock +// instead. const bool steady_clock::is_steady; -#ifdef __APPLE__ +#ifdef CLOCK_MONOTONIC + +steady_clock::time_point +steady_clock::now() _NOEXCEPT +{ + struct timespec tp; + if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) + __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); + return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); +} + +#elif defined(__APPLE__) + // mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of // nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom // are run time constants supplied by the OS. This clock has no relationship @@ -108,23 +138,9 @@ steady_clock::now() _NOEXCEPT return time_point(duration(fp())); } -#else // __APPLE__ -// FIXME: if _LIBCPP_HAS_NO_MONOTONIC_CLOCK, then clock_gettime isn't going to -// work. It may be possible to fall back on something else, depending on the system. - -// Warning: If this is not truly steady, then it is non-conforming. It is -// better for it to not exist and have the rest of libc++ use system_clock -// instead. - -steady_clock::time_point -steady_clock::now() _NOEXCEPT -{ - struct timespec tp; - if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) - __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed"); - return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec)); -} -#endif // __APPLE__ +#else +#error "Monotonic clock not implemented" +#endif #endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK |