summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-01-27 22:34:46 +0200
committerAhmad Samir <a.samirh78@gmail.com>2023-02-20 21:28:38 +0200
commitdd82f399106a66ffabdc05d55cc72d46f8bca1c7 (patch)
treec78ca0096c68031cc5d57bd4b4c33dab85049fc7 /src
parent37032b15900d2ae777d83e61a521aa6a0d01836a (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/io/qfilesystemengine_unix.cpp9
-rw-r--r--src/corelib/kernel/qtimerinfo_unix.cpp4
-rw-r--r--src/corelib/thread/qthread_unix.cpp21
-rw-r--r--src/corelib/tools/qtools_p.h27
4 files changed, 38 insertions, 23 deletions
diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp
index 3fd2170c92..1dbd2c8976 100644
--- a/src/corelib/io/qfilesystemengine_unix.cpp
+++ b/src/corelib/io/qfilesystemengine_unix.cpp
@@ -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) {
diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp
index 7ec23de488..2aeb7eebf0 100644
--- a/src/corelib/kernel/qtimerinfo_unix.cpp
+++ b/src/corelib/kernel/qtimerinfo_unix.cpp
@@ -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;
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
index 563c032db4..0bf7c9e27f 100644
--- a/src/corelib/thread/qthread_unix.cpp
+++ b/src/corelib/thread/qthread_unix.cpp
@@ -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)
diff --git a/src/corelib/tools/qtools_p.h b/src/corelib/tools/qtools_p.h
index 1423a672d6..2c7ad6f0de 100644
--- a/src/corelib/tools/qtools_p.h
+++ b/src/corelib/tools/qtools_p.h
@@ -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)();