diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-06-25 09:21:21 (GMT) |
---|---|---|
committer | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-06-25 09:43:03 (GMT) |
commit | a144416f28ff256eed9913edc8453acb00760876 (patch) | |
tree | 12343de823d29c82ff19bd0d7c2c5a1e2006b748 /src/corelib/thread | |
parent | 65725d65882bd821c2a704307d201d246d8342b5 (diff) | |
download | Qt-a144416f28ff256eed9913edc8453acb00760876.zip Qt-a144416f28ff256eed9913edc8453acb00760876.tar.gz Qt-a144416f28ff256eed9913edc8453acb00760876.tar.bz2 |
QSemaphore::tryAquire(timeout) -- never times out on an active semaphore
If a thread trying to acquire multiple resources is continuously
preempted by threads acquiring smaller amounts, the larger consumer
would end up waiting forever (instead of for the given timeout).
Fix this by keeping track of elapsed time between wakeups using
QElapsedTimer.
Task-number: QTBUG-11500
Reviewed-by: thiago
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qsemaphore.cpp | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/corelib/thread/qsemaphore.cpp b/src/corelib/thread/qsemaphore.cpp index 9dc828d..8e8a88a 100644 --- a/src/corelib/thread/qsemaphore.cpp +++ b/src/corelib/thread/qsemaphore.cpp @@ -44,6 +44,8 @@ #ifndef QT_NO_THREAD #include "qmutex.h" #include "qwaitcondition.h" +#include "qelapsedtimer.h" +#include "qdatetime.h" QT_BEGIN_NAMESPACE @@ -218,8 +220,11 @@ bool QSemaphore::tryAcquire(int n, int timeout) while (n > d->avail) d->cond.wait(locker.mutex()); } else { + QElapsedTimer timer; + timer.start(); while (n > d->avail) { - if (!d->cond.wait(locker.mutex(), timeout)) + if (timer.hasExpired(timeout) + || !d->cond.wait(locker.mutex(), timeout - timer.elapsed())) return false; } } |