Re-apply "QReadWriteLock: replace (QWaitCondition, QMutex) with std::(condition_variable, mutex)"

This reverts commit 1283ee324578e4cf5cc210d8d3c89647d6c56ec3.

We now have wrappers around std::mutex and std::condition_variable
that fall back to QMutex and QWaitCondition on the broken Integrity
toolchain. Use them.

Change-Id: I881aa931167b845b489713048b57ccc5f79d4237
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2021-07-14 12:43:22 +00:00 committed by Marc Mutz
parent 229f356cef
commit fd8f81f385
2 changed files with 25 additions and 22 deletions

View File

@ -67,6 +67,9 @@ QT_BEGIN_NAMESPACE
*/
namespace {
using ms = std::chrono::milliseconds;
enum {
StateMask = 0x3,
StateLockedForRead = 0x1,
@ -276,7 +279,7 @@ bool QReadWriteLock::tryLockForRead(int timeout)
d = d_ptr.loadAcquire();
continue;
}
return d->lockForRead(timeout);
return d->lockForRead(lock, timeout);
}
}
@ -380,7 +383,7 @@ bool QReadWriteLock::tryLockForWrite(int timeout)
d = d_ptr.loadAcquire();
continue;
}
return d->lockForWrite(timeout);
return d->lockForWrite(lock, timeout);
}
}
@ -463,9 +466,9 @@ QReadWriteLock::StateForWaitCondition QReadWriteLock::stateForWaitCondition() co
}
bool QReadWriteLockPrivate::lockForRead(int timeout)
bool QReadWriteLockPrivate::lockForRead(std::unique_lock<QtPrivate::mutex> &lock, int timeout)
{
Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
QElapsedTimer t;
if (timeout > 0)
@ -479,10 +482,10 @@ bool QReadWriteLockPrivate::lockForRead(int timeout)
if (elapsed > timeout)
return false;
waitingReaders++;
readerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
readerCond.wait_for(lock, ms{timeout - elapsed});
} else {
waitingReaders++;
readerCond.wait(&mutex);
readerCond.wait(lock);
}
waitingReaders--;
}
@ -491,9 +494,9 @@ bool QReadWriteLockPrivate::lockForRead(int timeout)
return true;
}
bool QReadWriteLockPrivate::lockForWrite(int timeout)
bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<QtPrivate::mutex> &lock, int timeout)
{
Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
QElapsedTimer t;
if (timeout > 0)
@ -508,15 +511,15 @@ bool QReadWriteLockPrivate::lockForWrite(int timeout)
if (waitingReaders && !waitingWriters && !writerCount) {
// We timed out and now there is no more writers or waiting writers, but some
// readers were queued (probably because of us). Wake the waiting readers.
readerCond.wakeAll();
readerCond.notify_all();
}
return false;
}
waitingWriters++;
writerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
writerCond.wait_for(lock, ms{timeout - elapsed});
} else {
waitingWriters++;
writerCond.wait(&mutex);
writerCond.wait(lock);
}
waitingWriters--;
}
@ -529,11 +532,11 @@ bool QReadWriteLockPrivate::lockForWrite(int timeout)
void QReadWriteLockPrivate::unlock()
{
Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
if (waitingWriters)
writerCond.wakeOne();
writerCond.notify_one();
else if (waitingReaders)
readerCond.wakeAll();
readerCond.notify_all();
}
static auto handleEquals(Qt::HANDLE handle)
@ -555,7 +558,7 @@ bool QReadWriteLockPrivate::recursiveLockForRead(int timeout)
return true;
}
if (!lockForRead(timeout))
if (!lockForRead(lock, timeout))
return false;
Reader r = {self, 1};
@ -574,7 +577,7 @@ bool QReadWriteLockPrivate::recursiveLockForWrite(int timeout)
return true;
}
if (!lockForWrite(timeout))
if (!lockForWrite(lock, timeout))
return false;
currentWriter = self;

View File

@ -53,8 +53,8 @@
//
#include <QtCore/private/qglobal_p.h>
#include <QtCore/private/qwaitcondition_p.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qwaitcondition.h>
QT_REQUIRE_CONFIG(thread);
@ -66,9 +66,9 @@ public:
explicit QReadWriteLockPrivate(bool isRecursive = false)
: recursive(isRecursive) {}
QMutex mutex;
QWaitCondition writerCond;
QWaitCondition readerCond;
QtPrivate::mutex mutex;
QtPrivate::condition_variable writerCond;
QtPrivate::condition_variable readerCond;
int readerCount = 0;
int writerCount = 0;
int waitingReaders = 0;
@ -76,8 +76,8 @@ public:
const bool recursive;
//Called with the mutex locked
bool lockForWrite(int timeout);
bool lockForRead(int timeout);
bool lockForWrite(std::unique_lock<QtPrivate::mutex> &lock, int timeout);
bool lockForRead(std::unique_lock<QtPrivate::mutex> &lock, int timeout);
void unlock();
//memory management