Port QThread::wait() to QDeadlineTimer

So we are in sync with QWaitCondition::wait().

Task-number: QTBUG-64266
Change-Id: I1d7487786513241cedd35d202c4ddee4937b08ec
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Christian Ehrlicher 2019-11-08 21:16:55 +01:00
parent f2cc6fd4a0
commit f2cf5f5417
7 changed files with 35 additions and 19 deletions

View File

@ -49,6 +49,8 @@
#include "qthread_p.h"
#include "private/qcoreapplication_p.h"
#include <limits>
QT_BEGIN_NAMESPACE
/*
@ -726,7 +728,8 @@ QThread::Priority QThread::priority() const
*/
/*!
\fn bool QThread::wait(unsigned long time)
\fn bool QThread::wait(QDeadlineTimer deadline)
\since 5.15
Blocks the thread until either of these conditions is met:
@ -735,12 +738,14 @@ QThread::Priority QThread::priority() const
execution (i.e. when it returns from \l{run()}). This function
will return true if the thread has finished. It also returns
true if the thread has not been started yet.
\li \a time milliseconds has elapsed. If \a time is ULONG_MAX (the
default), then the wait will never timeout (the thread must
return from \l{run()}). This function will return false if the
wait timed out.
\li The \a deadline is reached. This function will return false if the
deadline is reached.
\endlist
A deadline timer set to \c QDeadlineTimer::Forever (the default) will never
time out: in this case, the function only returns when the thread returns
from \l{run()} or if the thread has not yet started.
This provides similar functionality to the POSIX \c
pthread_join() function.
@ -833,9 +838,9 @@ void QThread::exit(int returnCode)
}
}
bool QThread::wait(unsigned long time)
bool QThread::wait(QDeadlineTimer deadline)
{
Q_UNUSED(time);
Q_UNUSED(deadline);
return false;
}
@ -966,6 +971,17 @@ void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
}
}
/*!
\fn bool QThread::wait(unsigned long time)
\overload
*/
bool QThread::wait(unsigned long time)
{
if (time == std::numeric_limits<unsigned long>::max())
return wait(QDeadlineTimer(QDeadlineTimer::Forever));
return wait(QDeadlineTimer(time));
}
#if QT_CONFIG(thread)
/*!

View File

@ -42,6 +42,7 @@
#define QTHREAD_H
#include <QtCore/qobject.h>
#include <QtCore/qdeadlinetimer.h>
// For QThread::create. The configure-time test just checks for the availability
// of std::future and std::async; for the C++17 codepath we perform some extra
@ -57,8 +58,6 @@
# endif
#endif
#include <limits.h>
QT_BEGIN_NAMESPACE
@ -135,8 +134,9 @@ public Q_SLOTS:
void quit();
public:
// default argument causes thread to block indefinetely
bool wait(unsigned long time = ULONG_MAX);
bool wait(QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever));
// ### Qt6 inline this function
bool wait(unsigned long time);
static void sleep(unsigned long);
static void msleep(unsigned long);

View File

@ -751,7 +751,7 @@ void QThread::terminate()
#endif
}
bool QThread::wait(unsigned long time)
bool QThread::wait(QDeadlineTimer deadline)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
@ -765,7 +765,7 @@ bool QThread::wait(unsigned long time)
return true;
while (d->running) {
if (!d->thread_done.wait(locker.mutex(), QDeadlineTimer(time)))
if (!d->thread_done.wait(locker.mutex(), deadline))
return false;
}
return true;

View File

@ -610,7 +610,7 @@ void QThread::terminate()
QThreadPrivate::finish(this, false);
}
bool QThread::wait(unsigned long time)
bool QThread::wait(QDeadlineTimer deadline)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
@ -627,9 +627,9 @@ bool QThread::wait(unsigned long time)
bool ret = false;
#ifndef Q_OS_WINRT
switch (WaitForSingleObject(d->handle, time)) {
switch (WaitForSingleObject(d->handle, deadline.remainingTime())) {
#else
switch (WaitForSingleObjectEx(d->handle, time, false)) {
switch (WaitForSingleObjectEx(d->handle, deadline.remainingTime(), false)) {
#endif
case WAIT_OBJECT_0:
ret = true;

View File

@ -1981,7 +1981,7 @@ void QNetworkAccessManagerPrivate::destroyThread()
{
if (thread) {
thread->quit();
thread->wait(5000);
thread->wait(QDeadlineTimer(5000));
if (thread->isFinished())
delete thread;
else

View File

@ -986,7 +986,7 @@ void QNetworkReplyHttpImplPrivate::postRequest(const QNetworkRequest &newHttpReq
}
thread->quit();
thread->wait(5000);
thread->wait(QDeadlineTimer(5000));
if (thread->isFinished())
delete thread;
else

View File

@ -93,7 +93,7 @@ void QNetworkConfigurationManagerPrivate::cleanup()
{
QThread* thread = bearerThread;
deleteLater();
if (thread->wait(5000))
if (thread->wait(QDeadlineTimer(5000)))
delete thread;
}