2010-05-12 03:42:16 +08:00
|
|
|
//===------------------------- chrono.cpp ---------------------------------===//
|
|
|
|
//
|
2010-05-12 05:36:01 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
2010-11-17 06:09:02 +08:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "chrono"
|
|
|
|
#include <sys/time.h> //for gettimeofday and timeval
|
2010-05-25 01:49:41 +08:00
|
|
|
#if __APPLE__
|
2010-05-12 03:42:16 +08:00
|
|
|
#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
|
2010-05-25 01:49:41 +08:00
|
|
|
#else /* !__APPLE__ */
|
|
|
|
#include <cerrno> // errno
|
|
|
|
#include <system_error> // __throw_system_error
|
|
|
|
#include <time.h> // clock_gettime, CLOCK_MONOTONIC
|
2010-08-22 08:03:27 +08:00
|
|
|
#endif // __APPLE__
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
namespace chrono
|
|
|
|
{
|
|
|
|
|
|
|
|
// system_clock
|
|
|
|
|
|
|
|
system_clock::time_point
|
|
|
|
system_clock::now()
|
|
|
|
{
|
|
|
|
timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
system_clock::to_time_t(const time_point& t)
|
|
|
|
{
|
|
|
|
return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
|
|
|
|
}
|
|
|
|
|
|
|
|
system_clock::time_point
|
|
|
|
system_clock::from_time_t(time_t t)
|
|
|
|
{
|
|
|
|
return system_clock::time_point(seconds(t));
|
|
|
|
}
|
|
|
|
|
2010-11-21 03:16:30 +08:00
|
|
|
// steady_clock
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2010-05-25 01:49:41 +08:00
|
|
|
#if __APPLE__
|
2010-05-12 03:42:16 +08:00
|
|
|
// 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
|
|
|
|
// to the Gregorian calendar. It's main use is as a high resolution timer.
|
|
|
|
|
|
|
|
// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment. Specialize
|
|
|
|
// for that case as an optimization.
|
|
|
|
|
|
|
|
#pragma GCC visibility push(hidden)
|
|
|
|
|
|
|
|
static
|
2010-11-21 03:16:30 +08:00
|
|
|
steady_clock::rep
|
|
|
|
steady_simplified()
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
return mach_absolute_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
double
|
2010-11-21 03:16:30 +08:00
|
|
|
compute_steady_factor()
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
mach_timebase_info_data_t MachInfo;
|
|
|
|
mach_timebase_info(&MachInfo);
|
|
|
|
return static_cast<double>(MachInfo.numer) / MachInfo.denom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2010-11-21 03:16:30 +08:00
|
|
|
steady_clock::rep
|
|
|
|
steady_full()
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2010-11-21 03:16:30 +08:00
|
|
|
static const double factor = compute_steady_factor();
|
|
|
|
return static_cast<steady_clock::rep>(mach_absolute_time() * factor);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
2010-11-21 03:16:30 +08:00
|
|
|
typedef steady_clock::rep (*FP)();
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
static
|
|
|
|
FP
|
2010-11-21 03:16:30 +08:00
|
|
|
init_steady_clock()
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
mach_timebase_info_data_t MachInfo;
|
|
|
|
mach_timebase_info(&MachInfo);
|
|
|
|
if (MachInfo.numer == MachInfo.denom)
|
2010-11-21 03:16:30 +08:00
|
|
|
return &steady_simplified;
|
|
|
|
return &steady_full;
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 11:15:51 +08:00
|
|
|
#pragma GCC visibility pop
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2010-11-21 03:16:30 +08:00
|
|
|
steady_clock::time_point
|
|
|
|
steady_clock::now()
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2010-11-21 03:16:30 +08:00
|
|
|
static FP fp = init_steady_clock();
|
2010-05-12 03:42:16 +08:00
|
|
|
return time_point(duration(fp()));
|
|
|
|
}
|
|
|
|
|
2010-08-22 08:03:27 +08:00
|
|
|
#else // __APPLE__
|
2010-05-25 01:49:41 +08:00
|
|
|
// FIXME: We assume that clock_gettime(CLOCK_MONOTONIC) works on
|
|
|
|
// non-apple systems. Instead, we should check _POSIX_TIMERS and
|
|
|
|
// _POSIX_MONOTONIC_CLOCK and fall back to something else if those
|
|
|
|
// don't exist.
|
|
|
|
|
2010-11-21 03:16:30 +08:00
|
|
|
// Warning: If this is not truly steady, then it is non-conforming. It is
|
2010-05-25 01:49:41 +08:00
|
|
|
// better for it to not exist and have the rest of libc++ use system_clock
|
|
|
|
// instead.
|
|
|
|
|
2010-11-21 03:16:30 +08:00
|
|
|
steady_clock::time_point
|
|
|
|
steady_clock::now()
|
2010-05-25 01:49:41 +08:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
2010-08-22 08:03:27 +08:00
|
|
|
#endif // __APPLE__
|
2010-05-25 01:49:41 +08:00
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|