diff options
author | Liang Qi <liang.qi@qt.io> | 2017-07-04 15:19:36 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@qt.io> | 2017-07-04 16:05:53 +0200 |
commit | c2b224a758ce7e6dcf3748444fa8e29ab81904be (patch) | |
tree | 277cb99bf054190c935579142506caa4ec9861dd /src/corelib/thread/qsemaphore.cpp | |
parent | 10de063ff12cdba07b4620182aced8ed05ee3505 (diff) | |
parent | eaee1209f0ead5be786e81db8aee604ccfea85b0 (diff) |
Merge remote-tracking branch 'origin/5.9' into dev
Conflicts:
src/corelib/io/qprocess_unix.cpp
src/plugins/platforms/xcb/qxcbconnection.cpp
src/plugins/platforms/xcb/qxcbwindow.cpp
src/widgets/util/util.pri
tests/auto/corelib/thread/qthread/qthread.pro
tests/auto/corelib/thread/qthread/tst_qthread.cpp
Change-Id: I5c45ab54d46d3c75a5c6c116777ebf5bc47a871b
Diffstat (limited to 'src/corelib/thread/qsemaphore.cpp')
-rw-r--r-- | src/corelib/thread/qsemaphore.cpp | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/corelib/thread/qsemaphore.cpp b/src/corelib/thread/qsemaphore.cpp index 397d6203aa..96c031eec6 100644 --- a/src/corelib/thread/qsemaphore.cpp +++ b/src/corelib/thread/qsemaphore.cpp @@ -42,7 +42,7 @@ #ifndef QT_NO_THREAD #include "qmutex.h" #include "qwaitcondition.h" -#include "qelapsedtimer.h" +#include "qdeadlinetimer.h" #include "qdatetime.h" QT_BEGIN_NAMESPACE @@ -217,20 +217,19 @@ 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); + + QDeadlineTimer timer(timeout); QMutexLocker locker(&d->mutex); - if (timeout < 0) { - while (n > d->avail) - d->cond.wait(locker.mutex()); - } else { - QElapsedTimer timer; - timer.start(); - while (n > d->avail) { - const qint64 elapsed = timer.elapsed(); - if (timeout - elapsed <= 0 - || !d->cond.wait(locker.mutex(), timeout - elapsed)) - return false; - } + qint64 remainingTime = timer.remainingTime(); + while (n > d->avail && remainingTime > 0) { + if (!d->cond.wait(locker.mutex(), remainingTime)) + return false; + remainingTime = timer.remainingTime(); } + if (n > d->avail) + return false; d->avail -= n; return true; |