diff options
author | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2019-09-16 01:00:07 +0200 |
---|---|---|
committer | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2019-09-16 01:00:07 +0200 |
commit | 1cfa636fe2c0cabbf960a02862873d4db3d3a8a8 (patch) | |
tree | c9a6d90736eb20c23706cb60ff7d4e853c2a2207 /src/corelib/thread | |
parent | 147a8bc4c86dd7c818acd2614ee67cd7098cfa5e (diff) | |
parent | 6f8fc4217a722cf89cd3502156cc0586588fda1e (diff) |
Merge remote-tracking branch 'origin/5.14' into 5.15
Change-Id: Ie24be82ee70bf103c2664de1a42741979262b10c
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qreadwritelock.cpp | 50 | ||||
-rw-r--r-- | src/corelib/thread/qreadwritelock_p.h | 32 |
2 files changed, 44 insertions, 38 deletions
diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp index 30e9b95a52..5aba05c1b9 100644 --- a/src/corelib/thread/qreadwritelock.cpp +++ b/src/corelib/thread/qreadwritelock.cpp @@ -48,6 +48,9 @@ #include "qreadwritelock_p.h" #include "qelapsedtimer.h" #include "private/qfreelist_p.h" +#include "private/qlocking_p.h" + +#include <chrono> QT_BEGIN_NAMESPACE @@ -64,6 +67,9 @@ QT_BEGIN_NAMESPACE */ namespace { + +using ms = std::chrono::milliseconds; + enum { StateMask = 0x3, StateLockedForRead = 0x1, @@ -262,7 +268,7 @@ bool QReadWriteLock::tryLockForRead(int timeout) if (d->recursive) return d->recursiveLockForRead(timeout); - QMutexLocker lock(&d->mutex); + auto lock = qt_unique_lock(d->mutex); if (d != d_ptr.loadRelaxed()) { // d_ptr has changed: this QReadWriteLock was unlocked before we had // time to lock d->mutex. @@ -273,7 +279,7 @@ bool QReadWriteLock::tryLockForRead(int timeout) d = d_ptr.loadAcquire(); continue; } - return d->lockForRead(timeout); + return d->lockForRead(lock, timeout); } } @@ -369,7 +375,7 @@ bool QReadWriteLock::tryLockForWrite(int timeout) if (d->recursive) return d->recursiveLockForWrite(timeout); - QMutexLocker lock(&d->mutex); + auto lock = qt_unique_lock(d->mutex); if (d != d_ptr.loadRelaxed()) { // The mutex was unlocked before we had time to lock the mutex. // We are holding to a mutex within a QReadWriteLockPrivate that is already released @@ -377,7 +383,7 @@ bool QReadWriteLock::tryLockForWrite(int timeout) d = d_ptr.loadAcquire(); continue; } - return d->lockForWrite(timeout); + return d->lockForWrite(lock, timeout); } } @@ -418,7 +424,7 @@ void QReadWriteLock::unlock() return; } - QMutexLocker locker(&d->mutex); + const auto lock = qt_scoped_lock(d->mutex); if (d->writerCount) { Q_ASSERT(d->writerCount == 1); Q_ASSERT(d->readerCount == 0); @@ -460,9 +466,9 @@ QReadWriteLock::StateForWaitCondition QReadWriteLock::stateForWaitCondition() co } -bool QReadWriteLockPrivate::lockForRead(int timeout) +bool QReadWriteLockPrivate::lockForRead(std::unique_lock<std::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) @@ -476,10 +482,10 @@ bool QReadWriteLockPrivate::lockForRead(int timeout) if (elapsed > timeout) return false; waitingReaders++; - readerCond.wait(&mutex, timeout - elapsed); + readerCond.wait_for(lock, ms{timeout - elapsed}); } else { waitingReaders++; - readerCond.wait(&mutex); + readerCond.wait(lock); } waitingReaders--; } @@ -488,9 +494,9 @@ bool QReadWriteLockPrivate::lockForRead(int timeout) return true; } -bool QReadWriteLockPrivate::lockForWrite(int timeout) +bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<std::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) @@ -505,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 queueud (probably because of us). Wake the waiting readers. - readerCond.wakeAll(); + readerCond.notify_all(); } return false; } waitingWriters++; - writerCond.wait(&mutex, timeout - elapsed); + writerCond.wait_for(lock, ms{timeout - elapsed}); } else { waitingWriters++; - writerCond.wait(&mutex); + writerCond.wait(lock); } waitingWriters--; } @@ -526,17 +532,17 @@ 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(); } bool QReadWriteLockPrivate::recursiveLockForRead(int timeout) { Q_ASSERT(recursive); - QMutexLocker lock(&mutex); + auto lock = qt_unique_lock(mutex); Qt::HANDLE self = QThread::currentThreadId(); @@ -546,7 +552,7 @@ bool QReadWriteLockPrivate::recursiveLockForRead(int timeout) return true; } - if (!lockForRead(timeout)) + if (!lockForRead(lock, timeout)) return false; currentReaders.insert(self, 1); @@ -556,7 +562,7 @@ bool QReadWriteLockPrivate::recursiveLockForRead(int timeout) bool QReadWriteLockPrivate::recursiveLockForWrite(int timeout) { Q_ASSERT(recursive); - QMutexLocker lock(&mutex); + auto lock = qt_unique_lock(mutex); Qt::HANDLE self = QThread::currentThreadId(); if (currentWriter == self) { @@ -564,7 +570,7 @@ bool QReadWriteLockPrivate::recursiveLockForWrite(int timeout) return true; } - if (!lockForWrite(timeout)) + if (!lockForWrite(lock, timeout)) return false; currentWriter = self; @@ -574,7 +580,7 @@ bool QReadWriteLockPrivate::recursiveLockForWrite(int timeout) void QReadWriteLockPrivate::recursiveUnlock() { Q_ASSERT(recursive); - QMutexLocker lock(&mutex); + auto lock = qt_unique_lock(mutex); Qt::HANDLE self = QThread::currentThreadId(); if (self == currentWriter) { diff --git a/src/corelib/thread/qreadwritelock_p.h b/src/corelib/thread/qreadwritelock_p.h index 31da2401c0..b2e782f9ee 100644 --- a/src/corelib/thread/qreadwritelock_p.h +++ b/src/corelib/thread/qreadwritelock_p.h @@ -54,7 +54,9 @@ #include <QtCore/private/qglobal_p.h> #include <QtCore/qhash.h> -#include <QtCore/qwaitcondition.h> + +#include <mutex> +#include <condition_variable> QT_REQUIRE_CONFIG(thread); @@ -63,38 +65,36 @@ QT_BEGIN_NAMESPACE class QReadWriteLockPrivate { public: - QReadWriteLockPrivate(bool isRecursive = false) - : readerCount(0), writerCount(0), waitingReaders(0), waitingWriters(0), - recursive(isRecursive), id(0), currentWriter(nullptr) {} + explicit QReadWriteLockPrivate(bool isRecursive = false) + : recursive(isRecursive) {} - QMutex mutex; - QWaitCondition writerCond; - QWaitCondition readerCond; - int readerCount; - int writerCount; - int waitingReaders; - int waitingWriters; + std::mutex mutex; + std::condition_variable writerCond; + std::condition_variable readerCond; + int readerCount = 0; + int writerCount = 0; + int waitingReaders = 0; + int waitingWriters = 0; const bool recursive; //Called with the mutex locked - bool lockForWrite(int timeout); - bool lockForRead(int timeout); + bool lockForWrite(std::unique_lock<std::mutex> &lock, int timeout); + bool lockForRead(std::unique_lock<std::mutex> &lock, int timeout); void unlock(); //memory management - int id; + int id = 0; void release(); static QReadWriteLockPrivate *allocate(); // Recusive mutex handling - Qt::HANDLE currentWriter; + Qt::HANDLE currentWriter = {}; QHash<Qt::HANDLE, int> currentReaders; // called with the mutex unlocked bool recursiveLockForWrite(int timeout); bool recursiveLockForRead(int timeout); void recursiveUnlock(); - }; QT_END_NAMESPACE |