q_core_unix_p.h: use std::chrono for time intervals

Also move some timespec helpers from qtimerinfo_unix.cpp to
q_core_unix_p.h, so that similar functions are grouped in one place.

Change-Id: I817733dd70607a1f4243a9745626f5c9b37ddc2a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ahmad Samir 2023-02-21 15:54:04 +02:00
parent 738c48244b
commit 39a4cd126f
2 changed files with 43 additions and 30 deletions

View File

@ -20,6 +20,7 @@
#include <QtCore/private/qglobal_p.h>
#include "qatomic.h"
#include "qbytearray.h"
#include <QtCore/private/qtools_p.h>
#ifndef Q_OS_UNIX
# error "qcore_unix_p.h included on a non-Unix system"
@ -68,16 +69,18 @@ QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(pollfd, Q_PRIMITIVE_TYPE);
static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
// Internal operator functions for timespecs
constexpr inline timespec &normalizedTimespec(timespec &t)
{
while (t.tv_nsec >= 1000000000) {
while (t.tv_nsec >= OneSecAsNsecs) {
++t.tv_sec;
t.tv_nsec -= 1000000000;
t.tv_nsec -= OneSecAsNsecs;
}
while (t.tv_nsec < 0) {
--t.tv_sec;
t.tv_nsec += 1000000000;
t.tv_nsec += OneSecAsNsecs;
}
return t;
}
@ -104,7 +107,7 @@ constexpr inline timespec operator-(const timespec &t1, const timespec &t2)
{
timespec tmp = {};
tmp.tv_sec = t1.tv_sec - (t2.tv_sec - 1);
tmp.tv_nsec = t1.tv_nsec - (t2.tv_nsec + 1000000000);
tmp.tv_nsec = t1.tv_nsec - (t2.tv_nsec + OneSecAsNsecs);
return normalizedTimespec(tmp);
}
constexpr inline timespec operator*(const timespec &t1, int mul)
@ -122,6 +125,42 @@ inline timeval timespecToTimeval(const timespec &ts)
return tv;
}
inline timespec &operator+=(timespec &t1, std::chrono::milliseconds msecs)
{
t1 += QtMiscUtils::durationToTimespec(msecs);
return t1;
}
inline timespec &operator+=(timespec &t1, int ms)
{
t1 += std::chrono::milliseconds{ms};
return t1;
}
inline timespec operator+(const timespec &t1, std::chrono::milliseconds msecs)
{
timespec tmp = t1;
tmp += msecs;
return tmp;
}
inline timespec operator+(const timespec &t1, int ms)
{
return t1 + std::chrono::milliseconds{ms};
}
inline timespec qAbsTimespec(const timespec &t)
{
timespec tmp = t;
if (tmp.tv_sec < 0) {
tmp.tv_sec = -tmp.tv_sec - 1;
tmp.tv_nsec -= OneSecAsNsecs;
}
if (tmp.tv_sec == 0 && tmp.tv_nsec < 0) {
tmp.tv_nsec = -tmp.tv_nsec;
}
return normalizedTimespec(tmp);
}
inline void qt_ignore_sigpipe()
{

View File

@ -58,19 +58,6 @@ timespec QTimerInfoList::updateCurrentTime()
#if ((_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) && !defined(Q_OS_INTEGRITY)) || defined(QT_BOOTSTRAPPED)
timespec qAbsTimespec(const timespec &t)
{
timespec tmp = t;
if (tmp.tv_sec < 0) {
tmp.tv_sec = -tmp.tv_sec - 1;
tmp.tv_nsec -= 1000000000;
}
if (tmp.tv_sec == 0 && tmp.tv_nsec < 0) {
tmp.tv_nsec = -tmp.tv_nsec;
}
return normalizedTimespec(tmp);
}
/*
Returns \c true if the real time clock has changed by more than 10%
relative to the processor time since the last time this function was
@ -149,19 +136,6 @@ void QTimerInfoList::timerInsert(QTimerInfo *ti)
insert(index+1, ti);
}
inline timespec &operator+=(timespec &t1, int ms)
{
t1.tv_sec += ms / 1000;
t1.tv_nsec += ms % 1000 * 1000 * 1000;
return normalizedTimespec(t1);
}
inline timespec operator+(const timespec &t1, int ms)
{
timespec t2 = t1;
return t2 += ms;
}
static constexpr timespec roundToMillisecond(timespec val)
{
// always round up