check for clock_gettime() during build

using it as general monotonic clock availability check, while at it use
CLOCK_REALTIME_COARSE if defined

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2020-11-17 00:12:44 +02:00
parent df852837c3
commit a52b7a53ea
4 changed files with 21 additions and 32 deletions

View file

@ -364,6 +364,7 @@ katie_check_function(getpwuid_r "pwd.h")
katie_check_function(getgrgid_r "grp.h")
katie_check_function(nl_langinfo "langinfo.h")
katie_check_function(getaddrinfo "netdb.h")
katie_check_function(clock_gettime "time.h")
# XSI/POSIX.1-2001
katie_check_function(strerror_r "string.h")
# SUSv2

View file

@ -50,38 +50,31 @@ enum {
static qint64 getticks()
{
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
clockid_t clockId;
#ifdef QT_HAVE_CLOCK_GETTIME
#ifdef CLOCK_REALTIME_COARSE // Linux specific
clockid_t clockId = CLOCK_REALTIME_COARSE;
#else
clockid_t clockId = CLOCK_REALTIME;
#endif
#ifndef _POSIX_THREAD_CPUTIME
clockId = CLOCK_REALTIME;
#elif (_POSIX_THREAD_CPUTIME-0 <= 0)
// if we don't have CLOCK_THREAD_CPUTIME_ID, we have to just use elapsed realtime instead
clockId = CLOCK_REALTIME;
# if (_POSIX_THREAD_CPUTIME-0 == 0)
#if defined(_SC_THREAD_CPUTIME) && defined(CLOCK_THREAD_CPUTIME_ID)
// detect availablility of CLOCK_THREAD_CPUTIME_ID,
// sysconf() will return either -1 or _POSIX_VERSION (don't care about thread races here)
static long useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME);
if (useThreadCpuTime != -1)
clockId = CLOCK_THREAD_CPUTIME_ID;
# endif
#else
clockId = CLOCK_THREAD_CPUTIME_ID;
#endif
struct timespec ts;
if (clock_gettime(clockId, &ts) == -1)
if (::clock_gettime(clockId, &ts) == -1)
return 0;
return (ts.tv_sec * 1000000000) + ts.tv_nsec;
#else
// no clock_gettime(), fall back to wall time
struct timeval tv;
gettimeofday(&tv, Q_NULLPTR);
::gettimeofday(&tv, Q_NULLPTR);
return (tv.tv_sec * 1000000) + tv.tv_usec;
#endif
#endif // QT_HAVE_CLOCK_GETTIME
}
static inline double elapsed(qint64 after, qint64 before)

View file

@ -140,7 +140,6 @@
#cmakedefine QT_NO_CAST_FROM_BYTEARRAY
#cmakedefine QT_NO_CAST_TO_ASCII
#cmakedefine QT_NO_CLIPBOARD
#cmakedefine QT_NO_CLOCK_MONOTONIC
#cmakedefine QT_NO_CODEC_FOR_C_STRINGS
#cmakedefine QT_NO_COLORDIALOG
#cmakedefine QT_NO_COLORNAMES

View file

@ -39,19 +39,15 @@
QT_BEGIN_NAMESPACE
#ifndef QT_NO_CLOCK_MONOTONIC
# if defined(_SC_MONOTONIC_CLOCK)
#if defined(QT_HAVE_CLOCK_GETTIME) && defined(_SC_MONOTONIC_CLOCK)
static const bool monotonicClockAvailable = (sysconf(_SC_MONOTONIC_CLOCK) >= 200112L);
# elif (_POSIX_MONOTONIC_CLOCK-0 != 0)
#elif defined(QT_HAVE_CLOCK_GETTIME)
static const bool monotonicClockAvailable = (_POSIX_MONOTONIC_CLOCK > 0);
# else
# define QT_NO_CLOCK_MONOTONIC
# endif
#endif
static inline qint64 fractionAdjustment()
{
#ifndef QT_NO_CLOCK_MONOTONIC
#ifdef QT_HAVE_CLOCK_GETTIME
if (Q_LIKELY(monotonicClockAvailable)) {
// the monotonic timer is measured in nanoseconds
// 1 ms = 1000000 ns
@ -65,7 +61,7 @@ static inline qint64 fractionAdjustment()
bool QElapsedTimer::isMonotonic()
{
#ifndef QT_NO_CLOCK_MONOTONIC
#ifdef QT_HAVE_CLOCK_GETTIME
return monotonicClockAvailable;
#else
return false;
@ -74,7 +70,7 @@ bool QElapsedTimer::isMonotonic()
QElapsedTimer::ClockType QElapsedTimer::clockType()
{
#ifndef QT_NO_CLOCK_MONOTONIC
#ifdef QT_HAVE_CLOCK_GETTIME
if (Q_LIKELY(monotonicClockAvailable)) {
return QElapsedTimer::MonotonicClock;
}
@ -84,13 +80,13 @@ QElapsedTimer::ClockType QElapsedTimer::clockType()
static inline void do_gettime(qint64 *sec, qint64 *frac)
{
#ifndef QT_NO_CLOCK_MONOTONIC
#ifdef QT_HAVE_CLOCK_GETTIME
if (Q_LIKELY(monotonicClockAvailable)) {
timespec ts;
#ifdef CLOCK_MONOTONIC_COARSE // Linux specific
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
::clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
::clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
*sec = ts.tv_sec;
*frac = ts.tv_nsec;
@ -112,7 +108,7 @@ timeval qt_gettime()
timeval tv;
tv.tv_sec = sec;
#ifndef QT_NO_CLOCK_MONOTONIC
#ifdef QT_HAVE_CLOCK_GETTIME
if (Q_LIKELY(monotonicClockAvailable))
tv.tv_usec = frac / 1000;
else
@ -147,7 +143,7 @@ qint64 QElapsedTimer::nsecsElapsed() const
do_gettime(&sec, &frac);
sec = sec - t1;
frac = frac - t2;
#ifndef QT_NO_CLOCK_MONOTONIC
#ifdef QT_HAVE_CLOCK_GETTIME
if (Q_UNLIKELY(!monotonicClockAvailable))
frac *= 1000;
#endif