From fee46831fd722935d126f8ab8f422911dbebb60f Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 31 Oct 2022 01:18:26 +0200 Subject: QThread/Unix: use chrono for time arithmetic Change-Id: I090d204db6126b3b6336637779b190509a9f0778 Reviewed-by: Thiago Macieira --- src/corelib/thread/qthread_unix.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/corelib/thread') diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 777aa362b2..8db27e8209 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -488,27 +488,31 @@ void QThread::yieldCurrentThread() #endif // QT_CONFIG(thread) -static timespec makeTimespec(time_t secs, long nsecs) +static timespec makeTimespec(std::chrono::nanoseconds nsecs) { + using namespace std::chrono; + const seconds secs = duration_cast(nsecs); + const nanoseconds frac = nsecs - secs; struct timespec ts; - ts.tv_sec = secs; - ts.tv_nsec = nsecs; + ts.tv_sec = secs.count(); + ts.tv_nsec = frac.count(); return ts; } + void QThread::sleep(unsigned long secs) { - qt_nanosleep(makeTimespec(secs, 0)); + qt_nanosleep(makeTimespec(std::chrono::seconds{secs})); } void QThread::msleep(unsigned long msecs) { - qt_nanosleep(makeTimespec(msecs / 1000, msecs % 1000 * 1000 * 1000)); + qt_nanosleep(makeTimespec(std::chrono::milliseconds{msecs})); } void QThread::usleep(unsigned long usecs) { - qt_nanosleep(makeTimespec(usecs / 1000 / 1000, usecs % (1000*1000) * 1000)); + qt_nanosleep(makeTimespec(std::chrono::microseconds{usecs})); } #if QT_CONFIG(thread) -- cgit v1.2.3