Revert "QReadWriteLock: replace (QWaitCondition, QMutex) with std::(condition_variable, mutex)"

This reverts commit 319c478603.

Reason for revert: QTBUG-78450

Change-Id: Ifaea83626296508558591d4ff207d4e0c883f841
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
bb10
Jani Heikkinen 2019-09-18 09:31:17 +00:00
parent 57e8fe86bd
commit 1283ee3245
2 changed files with 22 additions and 29 deletions

View File

@ -50,8 +50,6 @@
#include "private/qfreelist_p.h"
#include "private/qlocking_p.h"
#include <chrono>
QT_BEGIN_NAMESPACE
/*
@ -67,9 +65,6 @@ QT_BEGIN_NAMESPACE
*/
namespace {
using ms = std::chrono::milliseconds;
enum {
StateMask = 0x3,
StateLockedForRead = 0x1,
@ -279,7 +274,7 @@ bool QReadWriteLock::tryLockForRead(int timeout)
d = d_ptr.loadAcquire();
continue;
}
return d->lockForRead(lock, timeout);
return d->lockForRead(timeout);
}
}
@ -383,7 +378,7 @@ bool QReadWriteLock::tryLockForWrite(int timeout)
d = d_ptr.loadAcquire();
continue;
}
return d->lockForWrite(lock, timeout);
return d->lockForWrite(timeout);
}
}
@ -466,9 +461,9 @@ QReadWriteLock::StateForWaitCondition QReadWriteLock::stateForWaitCondition() co
}
bool QReadWriteLockPrivate::lockForRead(std::unique_lock<std::mutex> &lock, int timeout)
bool QReadWriteLockPrivate::lockForRead(int timeout)
{
Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
QElapsedTimer t;
if (timeout > 0)
@ -482,10 +477,10 @@ bool QReadWriteLockPrivate::lockForRead(std::unique_lock<std::mutex> &lock, int
if (elapsed > timeout)
return false;
waitingReaders++;
readerCond.wait_for(lock, ms{timeout - elapsed});
readerCond.wait(&mutex, timeout - elapsed);
} else {
waitingReaders++;
readerCond.wait(lock);
readerCond.wait(&mutex);
}
waitingReaders--;
}
@ -494,9 +489,9 @@ bool QReadWriteLockPrivate::lockForRead(std::unique_lock<std::mutex> &lock, int
return true;
}
bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<std::mutex> &lock, int timeout)
bool QReadWriteLockPrivate::lockForWrite(int timeout)
{
Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
QElapsedTimer t;
if (timeout > 0)
@ -511,15 +506,15 @@ bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<std::mutex> &lock, int
if (waitingReaders && !waitingWriters && !writerCount) {
// We timed out and now there is no more writers or waiting writers, but some
// readers were queueud (probably because of us). Wake the waiting readers.
readerCond.notify_all();
readerCond.wakeAll();
}
return false;
}
waitingWriters++;
writerCond.wait_for(lock, ms{timeout - elapsed});
writerCond.wait(&mutex, timeout - elapsed);
} else {
waitingWriters++;
writerCond.wait(lock);
writerCond.wait(&mutex);
}
waitingWriters--;
}
@ -532,11 +527,11 @@ bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<std::mutex> &lock, int
void QReadWriteLockPrivate::unlock()
{
Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
if (waitingWriters)
writerCond.notify_one();
writerCond.wakeOne();
else if (waitingReaders)
readerCond.notify_all();
readerCond.wakeAll();
}
bool QReadWriteLockPrivate::recursiveLockForRead(int timeout)
@ -552,7 +547,7 @@ bool QReadWriteLockPrivate::recursiveLockForRead(int timeout)
return true;
}
if (!lockForRead(lock, timeout))
if (!lockForRead(timeout))
return false;
currentReaders.insert(self, 1);
@ -570,7 +565,7 @@ bool QReadWriteLockPrivate::recursiveLockForWrite(int timeout)
return true;
}
if (!lockForWrite(lock, timeout))
if (!lockForWrite(timeout))
return false;
currentWriter = self;

View File

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