Don't recheck about timeout == 0 during mutex locking

If the timeout wasn't zero, it can only become zero if we return from
futex() with a non-timeout reason but subsequently expires while we're
recalculating something.

A side effect is that we try-lock a non-recursive mutex exactly
once. Before this change, we'd fastTryLock() twice even with
timeout == 0.

Change-Id: I0af09fc2a84669a683a843fcf1513203b075dfb7
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Thiago Macieira 2012-08-11 14:08:39 +02:00 committed by Qt by Nokia
parent 3acaa648f0
commit 870bd84a4e

View File

@ -123,17 +123,18 @@ bool QBasicMutex::lockInternal(int timeout) Q_DECL_NOTHROW
return static_cast<QRecursiveMutexPrivate *>(d)->lock(timeout);
}
if (timeout == 0)
return false;
QElapsedTimer elapsedTimer;
if (timeout >= 1)
elapsedTimer.start();
while (!fastTryLock()) {
d = d_ptr.load();
if (!d) // if d is 0, the mutex is unlocked
continue;
if (timeout == 0)
return false;
// the mutex is locked already, set a bit indicating we're waiting
while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) != 0) {
struct timespec ts, *pts = 0;