summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-11-09 15:49:26 -0800
committerJani Heikkinen <jani.heikkinen@qt.io>2017-11-14 11:07:30 +0000
commit4870282117b43242d9c2cd6fbde8175b2a907b08 (patch)
tree2a70cd866481b6d54dab9735b37c0177206f0de2 /src
parentd583030f2ca29dacce1805ad75855353f678f9d5 (diff)
QSemaphore: fix regression when the timeout < 0
The issue was introduced by eaee1209f0ead5be786e81db8aee604ccfea85b0, so it affected only 5.9.2. [ChangeLog][QtCore][QSemaphore] Fixed a regression that would make tryAcquire() not to wait forever if the timeout was a negative value. Note: new code is advised to only use -1 to indicate "forever", as some other functions taking timeout periods do not accept other values. Task-number: QTBUG-64413 Change-Id: I57a1bd6e0c194530b732fffd14f58fce60d5dfc9 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/thread/qsemaphore.cpp8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/corelib/thread/qsemaphore.cpp b/src/corelib/thread/qsemaphore.cpp
index ce0c1c91df..f63a08774c 100644
--- a/src/corelib/thread/qsemaphore.cpp
+++ b/src/corelib/thread/qsemaphore.cpp
@@ -214,13 +214,15 @@ bool QSemaphore::tryAcquire(int n)
bool QSemaphore::tryAcquire(int n, int timeout)
{
Q_ASSERT_X(n >= 0, "QSemaphore::tryAcquire", "parameter 'n' must be non-negative");
- if (timeout < 0)
- return tryAcquire(n);
+
+ // We're documented to accept any negative value as "forever"
+ // but QDeadlineTimer only accepts -1.
+ timeout = qMax(timeout, -1);
QDeadlineTimer timer(timeout);
QMutexLocker locker(&d->mutex);
qint64 remainingTime = timer.remainingTime();
- while (n > d->avail && remainingTime > 0) {
+ while (n > d->avail && remainingTime != 0) {
if (!d->cond.wait(locker.mutex(), remainingTime))
return false;
remainingTime = timer.remainingTime();