summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qreadwritelock_p.h
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2019-08-23 15:05:32 +0200
committerMarc Mutz <marc.mutz@kdab.com>2019-09-13 10:47:20 +0200
commit319c4786036b5f45fc95c683cef5cf5ba2ce2a6d (patch)
treef0b9894a92572ba6e831d3b913ce6518a89bc50e /src/corelib/thread/qreadwritelock_p.h
parent68b30a23a8ee66de4da62dcddfe2072c01945c24 (diff)
QReadWriteLock: replace (QWaitCondition, QMutex) with std::(condition_variable, mutex)
It turns out that QWaitCondition is a std::condition_variable_any. The _any variant works with any mutex type, but requires a native mutex for the native condition variable. So, QWaitCondition and std::condition_variable_any both require two different mutexes: the one the user passes in, and an internal one. std::condition_variable, however, only works with std::mutex, and since both are backed by the native API, condition_variable can use the mutex passed in by the user instead of having to use an internal one. So, port from 2 × QWaitCondition + QMutex (2 × native cond + 2 × native mutex + Qt mutex) to std::condition_variable + std::mutex (2 × native cond + native mutex), shaving the overhead of two additional mutexes (one Qt, one native) as well as the memory allocation performed by QWaitCondition (for its Private). Speeds up the writeOnly case by ~1/8th: PASS : tst_QReadWriteLock::writeOnly(QReadWriteLock) RESULT : tst_QReadWriteLock::writeOnly():"QReadWriteLock": - 39,703 msecs per iteration (total: 39,703, iterations: 1) + 34,950 msecs per iteration (total: 34,950, iterations: 1) Change-Id: I196cb13a27242fc1cb99723dfab5b2e5f8522143 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/thread/qreadwritelock_p.h')
-rw-r--r--src/corelib/thread/qreadwritelock_p.h14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/corelib/thread/qreadwritelock_p.h b/src/corelib/thread/qreadwritelock_p.h
index a4d002b7f2..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);
@@ -66,9 +68,9 @@ public:
explicit QReadWriteLockPrivate(bool isRecursive = false)
: recursive(isRecursive) {}
- QMutex mutex;
- QWaitCondition writerCond;
- QWaitCondition readerCond;
+ std::mutex mutex;
+ std::condition_variable writerCond;
+ std::condition_variable readerCond;
int readerCount = 0;
int writerCount = 0;
int waitingReaders = 0;
@@ -76,8 +78,8 @@ public:
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