summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2021-07-14 12:43:22 +0000
committerMarc Mutz <marc.mutz@qt.io>2021-11-20 02:38:41 +0100
commitfd8f81f3859fe92082caec925223e08cf61c69d4 (patch)
treef4421cdc6b02883969636de43c4a0dba97dee8ee /src/corelib/thread
parent229f356cefaaacc75c2414048ac4c70db3de4927 (diff)
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>
Diffstat (limited to 'src/corelib/thread')
-rw-r--r--src/corelib/thread/qreadwritelock.cpp35
-rw-r--r--src/corelib/thread/qreadwritelock_p.h12
2 files changed, 25 insertions, 22 deletions
diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp
index f22f3dab40..4c2a9c4251 100644
--- a/src/corelib/thread/qreadwritelock.cpp
+++ b/src/corelib/thread/qreadwritelock.cpp
@@ -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;
diff --git a/src/corelib/thread/qreadwritelock_p.h b/src/corelib/thread/qreadwritelock_p.h
index 1adbce6658..f9725f05af 100644
--- a/src/corelib/thread/qreadwritelock_p.h
+++ b/src/corelib/thread/qreadwritelock_p.h
@@ -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