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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-07-02 07:31:55 +08:00
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
#include "chrono"
|
2015-06-23 22:45:02 +08:00
|
|
|
#include "cerrno" // errno
|
|
|
|
#include "system_error" // __throw_system_error
|
|
|
|
#include <time.h> // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
|
2015-05-15 04:54:18 +08:00
|
|
|
|
2017-01-10 03:21:48 +08:00
|
|
|
#if (__APPLE__)
|
|
|
|
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
|
|
|
|
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
|
|
|
|
#define _LIBCXX_USE_CLOCK_GETTIME
|
|
|
|
#endif
|
|
|
|
#elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
|
|
|
|
#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 100000
|
|
|
|
#define _LIBCXX_USE_CLOCK_GETTIME
|
|
|
|
#endif
|
|
|
|
#elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
|
|
|
|
#if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 100000
|
|
|
|
#define _LIBCXX_USE_CLOCK_GETTIME
|
|
|
|
#endif
|
|
|
|
#elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
|
|
|
|
#if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 30000
|
|
|
|
#define _LIBCXX_USE_CLOCK_GETTIME
|
|
|
|
#endif
|
|
|
|
#endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
|
|
|
|
#else
|
|
|
|
#define _LIBCXX_USE_CLOCK_GETTIME
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
2017-01-04 05:53:51 +08:00
|
|
|
#if defined(_LIBCPP_WIN32API)
|
2017-01-02 04:20:41 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define VC_EXTRA_LEAN
|
2017-06-01 06:14:05 +08:00
|
|
|
#include <windows.h>
|
2017-01-02 04:20:41 +08:00
|
|
|
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
|
|
|
|
#include <winapifamily.h>
|
|
|
|
#endif
|
|
|
|
#else
|
2017-01-10 08:51:02 +08:00
|
|
|
#if !defined(CLOCK_REALTIME) || !defined(_LIBCXX_USE_CLOCK_GETTIME)
|
2015-05-15 04:54:18 +08:00
|
|
|
#include <sys/time.h> // for gettimeofday and timeval
|
2017-01-04 05:53:51 +08:00
|
|
|
#endif // !defined(CLOCK_REALTIME)
|
|
|
|
#endif // defined(_LIBCPP_WIN32API)
|
2015-05-15 04:54:18 +08:00
|
|
|
|
2017-01-02 04:20:43 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
|
2015-05-15 04:54:18 +08:00
|
|
|
#if __APPLE__
|
|
|
|
#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
|
2017-01-04 05:53:51 +08:00
|
|
|
#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
|
2015-05-15 04:54:18 +08:00
|
|
|
#error "Monotonic clock not implemented"
|
|
|
|
#endif
|
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
namespace chrono
|
|
|
|
{
|
|
|
|
|
|
|
|
// system_clock
|
|
|
|
|
2012-12-13 05:14:28 +08:00
|
|
|
const bool system_clock::is_steady;
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
system_clock::time_point
|
2011-05-29 02:34:36 +08:00
|
|
|
system_clock::now() _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2017-01-04 05:53:51 +08:00
|
|
|
#if defined(_LIBCPP_WIN32API)
|
2017-01-02 06:04:38 +08:00
|
|
|
// FILETIME is in 100ns units
|
|
|
|
using filetime_duration =
|
|
|
|
_VSTD::chrono::duration<__int64,
|
|
|
|
_VSTD::ratio_multiply<_VSTD::ratio<100, 1>,
|
|
|
|
nanoseconds::period>>;
|
2017-01-02 04:20:41 +08:00
|
|
|
|
2017-01-02 06:04:38 +08:00
|
|
|
// The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
|
2017-01-03 02:41:50 +08:00
|
|
|
static _LIBCPP_CONSTEXPR const seconds nt_to_unix_epoch{11644473600};
|
2017-01-02 06:04:38 +08:00
|
|
|
|
|
|
|
FILETIME ft;
|
2017-01-02 04:20:41 +08:00
|
|
|
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
|
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
2017-01-02 06:04:38 +08:00
|
|
|
GetSystemTimePreciseAsFileTime(&ft);
|
2017-01-02 04:20:41 +08:00
|
|
|
#else
|
2017-01-02 06:04:38 +08:00
|
|
|
GetSystemTimeAsFileTime(&ft);
|
2017-01-02 04:20:41 +08:00
|
|
|
#endif
|
|
|
|
#else
|
2017-01-02 06:04:38 +08:00
|
|
|
GetSystemTimeAsFileTime(&ft);
|
2017-01-02 04:20:41 +08:00
|
|
|
#endif
|
2017-01-02 06:04:38 +08:00
|
|
|
|
|
|
|
filetime_duration d{(static_cast<__int64>(ft.dwHighDateTime) << 32) |
|
|
|
|
static_cast<__int64>(ft.dwLowDateTime)};
|
|
|
|
return time_point(duration_cast<duration>(d - nt_to_unix_epoch));
|
2017-01-02 04:20:41 +08:00
|
|
|
#else
|
2017-01-10 03:21:48 +08:00
|
|
|
#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
|
2015-05-15 04:54:18 +08:00
|
|
|
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));
|
2017-01-10 03:21:48 +08:00
|
|
|
#else
|
2010-05-12 03:42:16 +08:00
|
|
|
timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
|
2017-01-10 03:21:48 +08:00
|
|
|
#endif // _LIBCXX_USE_CLOCK_GETTIME && CLOCK_REALTIME
|
2017-01-02 04:20:41 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2011-05-29 02:34:36 +08:00
|
|
|
system_clock::to_time_t(const time_point& t) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
|
|
|
|
}
|
|
|
|
|
|
|
|
system_clock::time_point
|
2011-05-29 02:34:36 +08:00
|
|
|
system_clock::from_time_t(time_t t) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
return system_clock::time_point(seconds(t));
|
|
|
|
}
|
|
|
|
|
2014-09-03 05:14:38 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
|
2010-11-21 03:16:30 +08:00
|
|
|
// steady_clock
|
2015-05-15 04:54:18 +08:00
|
|
|
//
|
|
|
|
// 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.
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2012-12-13 05:14:28 +08:00
|
|
|
const bool steady_clock::is_steady;
|
|
|
|
|
2017-01-02 04:20:43 +08:00
|
|
|
#if defined(__APPLE__)
|
2015-05-15 04:54:18 +08:00
|
|
|
|
2017-01-10 03:21:48 +08:00
|
|
|
// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
|
|
|
|
#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
|
|
|
|
steady_clock::time_point
|
|
|
|
steady_clock::now() _NOEXCEPT
|
|
|
|
{
|
|
|
|
struct timespec tp;
|
|
|
|
if (0 != clock_gettime(CLOCK_UPTIME_RAW, &tp))
|
|
|
|
__throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
|
|
|
|
return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
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.
|
|
|
|
|
|
|
|
static
|
2010-11-21 03:16:30 +08:00
|
|
|
steady_clock::rep
|
|
|
|
steady_simplified()
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2011-12-02 04:21:04 +08:00
|
|
|
return static_cast<steady_clock::rep>(mach_absolute_time());
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-21 03:16:30 +08:00
|
|
|
steady_clock::time_point
|
2011-05-29 02:34:36 +08:00
|
|
|
steady_clock::now() _NOEXCEPT
|
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()));
|
|
|
|
}
|
2017-01-10 03:21:48 +08:00
|
|
|
#endif // defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-01-04 05:53:51 +08:00
|
|
|
#elif defined(_LIBCPP_WIN32API)
|
2017-01-02 04:20:43 +08:00
|
|
|
|
|
|
|
steady_clock::time_point
|
|
|
|
steady_clock::now() _NOEXCEPT
|
|
|
|
{
|
2017-01-02 06:04:36 +08:00
|
|
|
static LARGE_INTEGER freq;
|
|
|
|
static BOOL initialized = FALSE;
|
|
|
|
if (!initialized)
|
|
|
|
initialized = QueryPerformanceFrequency(&freq); // always succceeds
|
|
|
|
|
|
|
|
LARGE_INTEGER counter;
|
|
|
|
QueryPerformanceCounter(&counter);
|
|
|
|
return time_point(duration(counter.QuadPart * nano::den / freq.QuadPart));
|
2017-01-02 04:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(CLOCK_MONOTONIC)
|
|
|
|
|
2017-01-10 03:21:48 +08:00
|
|
|
// On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
|
|
|
|
// time functions in the nanosecond range. Thus, they are the only acceptable
|
|
|
|
// implementations of steady_clock.
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
|
|
|
|
#endif
|
|
|
|
|
2017-01-02 04:20:43 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:54:18 +08:00
|
|
|
#else
|
|
|
|
#error "Monotonic clock not implemented"
|
|
|
|
#endif
|
2010-05-25 01:49:41 +08:00
|
|
|
|
2014-09-03 05:14:38 +08:00
|
|
|
#endif // !_LIBCPP_HAS_NO_MONOTONIC_CLOCK
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|