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 "qthread_p.h"
#include "private/qcoreapplication_p.h" #include "private/qcoreapplication_p.h"
#include <limits>
QT_BEGIN_NAMESPACE 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: 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 execution (i.e. when it returns from \l{run()}). This function
will return true if the thread has finished. It also returns will return true if the thread has finished. It also returns
true if the thread has not been started yet. true if the thread has not been started yet.
\li \a time milliseconds has elapsed. If \a time is ULONG_MAX (the \li The \a deadline is reached. This function will return false if the
default), then the wait will never timeout (the thread must deadline is reached.
return from \l{run()}). This function will return false if the
wait timed out.
\endlist \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 This provides similar functionality to the POSIX \c
pthread_join() function. 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; 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) #if QT_CONFIG(thread)
/*! /*!

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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