QtMiscUtils: add std::chrono::duration <-> timespec helpers

Change-Id: I91f36a3d651fd57443072fde4c3e8f811682328e
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-01-27 22:34:46 +02:00
parent 37032b1590
commit dd82f39910
4 changed files with 38 additions and 23 deletions

View File

@ -12,6 +12,7 @@
#include <QtCore/qoperatingsystemversion.h>
#include <QtCore/private/qcore_unix_p.h>
#include <QtCore/private/qfiledevice_p.h>
#include <QtCore/private/qtools_p.h>
#include <QtCore/qvarlengtharray.h>
#ifndef QT_BOOTSTRAPPED
# include <QtCore/qstandardpaths.h>
@ -1556,13 +1557,9 @@ bool QFileSystemEngine::setFileTime(int fd, const QDateTime &newDate, QAbstractF
struct timespec ts[2] = {{0, UTIME_OMIT}, {0, UTIME_OMIT}};
if (time == QAbstractFileEngine::AccessTime || time == QAbstractFileEngine::ModificationTime) {
using namespace std::chrono;
const milliseconds msecs{newDate.toMSecsSinceEpoch()};
const seconds secs = duration_cast<seconds>(msecs);
const nanoseconds frac = msecs - secs;
const int idx = time == QAbstractFileEngine::AccessTime ? 0 : 1;
ts[idx].tv_sec = secs.count();
ts[idx].tv_nsec = frac.count();
const std::chrono::milliseconds msecs{newDate.toMSecsSinceEpoch()};
ts[idx] = QtMiscUtils::durationToTimespec(msecs);
}
if (futimens(fd, ts) == -1) {

View File

@ -9,6 +9,7 @@
#include "private/qtimerinfo_unix_p.h"
#include "private/qobject_p.h"
#include "private/qabstracteventdispatcher_p.h"
#include <QtCore/private/qtools_p.h>
#ifdef QTIMERINFO_DEBUG
# include <QDebug>
@ -396,8 +397,7 @@ qint64 QTimerInfoList::timerRemainingTime(int timerId)
if (currentTime < t->timeout) {
// time to wait
tm = roundToMillisecond(t->timeout - currentTime);
using namespace std::chrono;
const auto dur = duration_cast<milliseconds>(seconds{tm.tv_sec} + nanoseconds{tm.tv_nsec});
const std::chrono::milliseconds dur = QtMiscUtils::timespecToChronoMs(&tm);
return dur.count();
} else {
return 0;

View File

@ -8,6 +8,7 @@
#include <private/qcoreapplication_p.h>
#include <private/qcore_unix_p.h>
#include <private/qtools_p.h>
#if defined(Q_OS_DARWIN)
# include <private/qeventdispatcher_cf_p.h>
@ -70,6 +71,8 @@
QT_BEGIN_NAMESPACE
using namespace QtMiscUtils;
#if QT_CONFIG(thread)
static_assert(sizeof(pthread_t) <= sizeof(Qt::HANDLE));
@ -488,31 +491,19 @@ void QThread::yieldCurrentThread()
#endif // QT_CONFIG(thread)
static timespec makeTimespec(std::chrono::nanoseconds nsecs)
{
using namespace std::chrono;
const seconds secs = duration_cast<seconds>(nsecs);
const nanoseconds frac = nsecs - secs;
struct timespec ts;
ts.tv_sec = secs.count();
ts.tv_nsec = frac.count();
return ts;
}
void QThread::sleep(unsigned long secs)
{
qt_nanosleep(makeTimespec(std::chrono::seconds{secs}));
qt_nanosleep(durationToTimespec(std::chrono::seconds{secs}));
}
void QThread::msleep(unsigned long msecs)
{
qt_nanosleep(makeTimespec(std::chrono::milliseconds{msecs}));
qt_nanosleep(durationToTimespec(std::chrono::milliseconds{msecs}));
}
void QThread::usleep(unsigned long usecs)
{
qt_nanosleep(makeTimespec(std::chrono::microseconds{usecs}));
qt_nanosleep(durationToTimespec(std::chrono::microseconds{usecs}));
}
#if QT_CONFIG(thread)

View File

@ -16,7 +16,10 @@
//
#include "QtCore/private/qglobal_p.h"
#include <chrono>
#include <limits.h>
#include <time.h>
QT_BEGIN_NAMESPACE
@ -104,8 +107,32 @@ constexpr inline int qt_lencmp(qsizetype lhs, qsizetype rhs) noexcept
lhs > rhs ? 1 :
/* else */ -1 ;
}
inline timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
{
using namespace std::chrono;
const seconds secs = duration_cast<seconds>(timeout);
const nanoseconds frac = timeout - secs;
struct timespec ts;
ts.tv_sec = secs.count();
ts.tv_nsec = frac.count();
return ts;
}
template <typename Duration>
inline Duration timespecToChrono(struct timespec *ts) noexcept
{
using namespace std::chrono;
return duration_cast<Duration>(seconds{ts->tv_sec} + nanoseconds{ts->tv_nsec});
}
inline std::chrono::milliseconds timespecToChronoMs(struct timespec *ts) noexcept
{
return timespecToChrono<std::chrono::milliseconds>(ts);
}
} // namespace QtMiscUtils
// We typically need an extra bit for qNextPowerOfTwo when determining the next allocation size.
constexpr qsizetype MaxAllocSize = (std::numeric_limits<qsizetype>::max)();