summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/thread')
-rw-r--r--src/corelib/thread/qreadwritelock.cpp4
-rw-r--r--src/corelib/thread/qthread.cpp30
-rw-r--r--src/corelib/thread/qthread.h8
-rw-r--r--src/corelib/thread/qthread_unix.cpp4
-rw-r--r--src/corelib/thread/qthread_win.cpp6
-rw-r--r--src/corelib/thread/qthreadpool.cpp2
-rw-r--r--src/corelib/thread/qwaitcondition.h39
-rw-r--r--src/corelib/thread/qwaitcondition.qdoc34
-rw-r--r--src/corelib/thread/qwaitcondition_unix.cpp4
9 files changed, 86 insertions, 45 deletions
diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp
index 14654986a0..c8463de402 100644
--- a/src/corelib/thread/qreadwritelock.cpp
+++ b/src/corelib/thread/qreadwritelock.cpp
@@ -477,7 +477,7 @@ bool QReadWriteLockPrivate::lockForRead(int timeout)
if (elapsed > timeout)
return false;
waitingReaders++;
- readerCond.wait(&mutex, timeout - elapsed);
+ readerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
} else {
waitingReaders++;
readerCond.wait(&mutex);
@@ -511,7 +511,7 @@ bool QReadWriteLockPrivate::lockForWrite(int timeout)
return false;
}
waitingWriters++;
- writerCond.wait(&mutex, timeout - elapsed);
+ writerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
} else {
waitingWriters++;
writerCond.wait(&mutex);
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index 6a59043881..1248a53160 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -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)
/*!
diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h
index 8141f945b6..635dd94522 100644
--- a/src/corelib/thread/qthread.h
+++ b/src/corelib/thread/qthread.h
@@ -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);
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
index 21abe372eb..efa3cb944e 100644
--- a/src/corelib/thread/qthread_unix.cpp
+++ b/src/corelib/thread/qthread_unix.cpp
@@ -771,7 +771,7 @@ void QThread::terminate()
#endif
}
-bool QThread::wait(unsigned long time)
+bool QThread::wait(QDeadlineTimer deadline)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
@@ -785,7 +785,7 @@ bool QThread::wait(unsigned long time)
return true;
while (d->running) {
- if (!d->thread_done.wait(locker.mutex(), time))
+ if (!d->thread_done.wait(locker.mutex(), deadline))
return false;
}
return true;
diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp
index 996bcf0a71..44cb5653bf 100644
--- a/src/corelib/thread/qthread_win.cpp
+++ b/src/corelib/thread/qthread_win.cpp
@@ -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;
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp
index 4d2389f699..5f23a78c8a 100644
--- a/src/corelib/thread/qthreadpool.cpp
+++ b/src/corelib/thread/qthreadpool.cpp
@@ -136,7 +136,7 @@ void QThreadPoolThread::run()
manager->waitingThreads.enqueue(this);
registerThreadInactive();
// wait for work, exiting after the expiry timeout is reached
- runnableReady.wait(locker.mutex(), manager->expiryTimeout);
+ runnableReady.wait(locker.mutex(), QDeadlineTimer(manager->expiryTimeout));
++manager->activeThreads;
if (manager->waitingThreads.removeOne(this))
expired = true;
diff --git a/src/corelib/thread/qwaitcondition.h b/src/corelib/thread/qwaitcondition.h
index 11520e4cfe..079049af25 100644
--- a/src/corelib/thread/qwaitcondition.h
+++ b/src/corelib/thread/qwaitcondition.h
@@ -40,15 +40,12 @@
#ifndef QWAITCONDITION_H
#define QWAITCONDITION_H
-#include <QtCore/qglobal.h>
-
-#include <limits.h>
+#include <QtCore/QDeadlineTimer>
QT_BEGIN_NAMESPACE
#if QT_CONFIG(thread)
-class QDeadlineTimer;
class QWaitConditionPrivate;
class QMutex;
class QReadWriteLock;
@@ -59,11 +56,16 @@ public:
QWaitCondition();
~QWaitCondition();
- // ### Qt 6: remove unsigned long overloads
- bool wait(QMutex *lockedMutex, unsigned long time = ULONG_MAX);
- bool wait(QMutex *lockedMutex, QDeadlineTimer deadline);
- bool wait(QReadWriteLock *lockedReadWriteLock, unsigned long time = ULONG_MAX);
- bool wait(QReadWriteLock *lockedReadWriteLock, QDeadlineTimer deadline);
+ bool wait(QMutex *lockedMutex,
+ QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever));
+ bool wait(QReadWriteLock *lockedReadWriteLock,
+ QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever));
+#if QT_DEPRECATED_SINCE(5, 15)
+ QT_DEPRECATED_VERSION_X_5_15("Use wait(QMutex *lockedMutex, QDeadlineTimer deadline) instead")
+ bool wait(QMutex *lockedMutex, unsigned long time);
+ QT_DEPRECATED_VERSION_X_5_15("Use wait(QReadWriteLock *lockedReadWriteLock, QDeadlineTimer deadline) instead")
+ bool wait(QReadWriteLock *lockedReadWriteLock, unsigned long time);
+#endif
void wakeOne();
void wakeAll();
@@ -80,21 +82,28 @@ private:
#else
class QMutex;
+class QReadWriteLock;
+
class Q_CORE_EXPORT QWaitCondition
{
public:
QWaitCondition() {}
~QWaitCondition() {}
- bool wait(QMutex *mutex, unsigned long time = ULONG_MAX)
- {
- Q_UNUSED(mutex);
- Q_UNUSED(time);
- return true;
- }
+ bool wait(QMutex *, QDeadlineTimer = QDeadlineTimer(QDeadlineTimer::Forever))
+ { return true; }
+ bool wait(QReadWriteLock *, QDeadlineTimer = QDeadlineTimer(QDeadlineTimer::Forever))
+ { return true; }
+#if QT_DEPRECATED_SINCE(5, 15)
+ bool wait(QMutex *, unsigned long) { return true; }
+ bool wait(QReadWriteLock *, unsigned long) { return true; }
+#endif
void wakeOne() {}
void wakeAll() {}
+
+ void notify_one() { wakeOne(); }
+ void notify_all() { wakeAll(); }
};
#endif // QT_CONFIG(thread)
diff --git a/src/corelib/thread/qwaitcondition.qdoc b/src/corelib/thread/qwaitcondition.qdoc
index eebc28f059..9da6f6f25c 100644
--- a/src/corelib/thread/qwaitcondition.qdoc
+++ b/src/corelib/thread/qwaitcondition.qdoc
@@ -119,10 +119,22 @@
\sa wakeOne()
*/
+#if QT_DEPRECATED_SINCE(5, 15)
/*!
\fn bool QWaitCondition::wait(QMutex *lockedMutex, unsigned long time)
+ \obsolete use wait(QMutex *lockedMutex, QDeadlineTimer deadline) instead
+*/
+/*!
+ \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, unsigned long time)
+ \obsolete use wait(QReadWriteLock *lockedReadWriteLock, QDeadlineTimer deadline) instead
+*/
+#endif
- Releases the \a lockedMutex and waits on the wait condition. The
+/*!
+ \fn bool QWaitCondition::wait(QMutex *lockedMutex, QDeadlineTimer deadline)
+ \since 5.12
+
+ Releases the \a lockedMutex and waits on the wait condition. The
\a lockedMutex must be initially locked by the calling thread. If \a
lockedMutex is not in a locked state, the behavior is undefined. If
\a lockedMutex is a recursive mutex, this function
@@ -132,10 +144,10 @@
\list
\li Another thread signals it using wakeOne() or wakeAll(). This
function will return true in this case.
- \li \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
- (the default), then the wait will never timeout (the event
- must be signalled). This function will return false if the
- wait timed out.
+ \li the deadline given by \a deadline is reached. If \a deadline is
+ \c QDeadlineTimer::Forever (the default), then the wait will never
+ timeout (the event must be signalled). This function will return
+ false if the wait timed out.
\endlist
The \a lockedMutex will be returned to the same locked state. This
@@ -146,8 +158,8 @@
*/
/*!
- \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, unsigned long time)
- \since 4.4
+ \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, QDeadlineTimer deadline)
+ \since 5.12
Releases the \a lockedReadWriteLock and waits on the wait
condition. The \a lockedReadWriteLock must be initially locked by the
@@ -160,10 +172,10 @@
\list
\li Another thread signals it using wakeOne() or wakeAll(). This
function will return true in this case.
- \li \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
- (the default), then the wait will never timeout (the event
- must be signalled). This function will return false if the
- wait timed out.
+ \li the deadline given by \a deadline is reached. If \a deadline is
+ \c QDeadlineTimer::Forever (the default), then the wait will never
+ timeout (the event must be signalled). This function will return
+ false if the wait timed out.
\endlist
The \a lockedReadWriteLock will be returned to the same locked
diff --git a/src/corelib/thread/qwaitcondition_unix.cpp b/src/corelib/thread/qwaitcondition_unix.cpp
index dd7475cec5..a8dfb9999c 100644
--- a/src/corelib/thread/qwaitcondition_unix.cpp
+++ b/src/corelib/thread/qwaitcondition_unix.cpp
@@ -202,12 +202,14 @@ void QWaitCondition::wakeAll()
report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeAll()", "mutex unlock");
}
+#if QT_DEPRECATED_SINCE(5, 15)
bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
{
if (time == std::numeric_limits<unsigned long>::max())
return wait(mutex, QDeadlineTimer(QDeadlineTimer::Forever));
return wait(mutex, QDeadlineTimer(time));
}
+#endif
bool QWaitCondition::wait(QMutex *mutex, QDeadlineTimer deadline)
{
@@ -229,12 +231,14 @@ bool QWaitCondition::wait(QMutex *mutex, QDeadlineTimer deadline)
return returnValue;
}
+#if QT_DEPRECATED_SINCE(5, 15)
bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time)
{
if (time == std::numeric_limits<unsigned long>::max())
return wait(readWriteLock, QDeadlineTimer(QDeadlineTimer::Forever));
return wait(readWriteLock, QDeadlineTimer(time));
}
+#endif
bool QWaitCondition::wait(QReadWriteLock *readWriteLock, QDeadlineTimer deadline)
{