summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2011-03-29 09:02:20 (GMT)
committerOlivier Goffart <olivier.goffart@nokia.com>2011-03-31 14:32:46 (GMT)
commitb63e0f7c612f7e63831dbee3565292ddca7dba59 (patch)
tree54a26ffe1dd6d0ceb26487863c588bdcab47db34
parent6fcd431d04cf7416179d5ab30f656a6ce3926070 (diff)
downloadQt-b63e0f7c612f7e63831dbee3565292ddca7dba59.zip
Qt-b63e0f7c612f7e63831dbee3565292ddca7dba59.tar.gz
Qt-b63e0f7c612f7e63831dbee3565292ddca7dba59.tar.bz2
Optimize QMutexPool
Make the get() function inline and simplify the hash computation Reviewed-by: brad
-rw-r--r--src/corelib/thread/qmutexpool.cpp22
-rw-r--r--src/corelib/thread/qmutexpool_p.h10
2 files changed, 19 insertions, 13 deletions
diff --git a/src/corelib/thread/qmutexpool.cpp b/src/corelib/thread/qmutexpool.cpp
index 13e29c3..144fa35 100644
--- a/src/corelib/thread/qmutexpool.cpp
+++ b/src/corelib/thread/qmutexpool.cpp
@@ -123,22 +123,20 @@ QMutexPool *QMutexPool::instance()
return globalMutexPool();
}
-/*!
+/*! \fn QMutexPool::get(void *address)
Returns a QMutex from the pool. QMutexPool uses the value \a address
to determine which mutex is returned from the pool.
*/
-QMutex *QMutexPool::get(const void *address)
-{
- Q_ASSERT_X(address != 0, "QMutexPool::get()", "'address' argument cannot be zero");
- int index = int((quintptr(address) >> (sizeof(address) >> 1)) % mutexes.count());
-
- if (!mutexes[index]) {
- // mutex not created, create one
- QMutex *newMutex = new QMutex(recursionMode);
- if (!mutexes[index].testAndSetOrdered(0, newMutex))
- delete newMutex;
- }
+/*! \internal
+ create the mutex for the given index
+ */
+QMutex *QMutexPool::createMutex(int index)
+{
+ // mutex not created, create one
+ QMutex *newMutex = new QMutex(recursionMode);
+ if (!mutexes[index].testAndSetOrdered(0, newMutex))
+ delete newMutex;
return mutexes[index];
}
diff --git a/src/corelib/thread/qmutexpool_p.h b/src/corelib/thread/qmutexpool_p.h
index 1a45ba9..b2cd210 100644
--- a/src/corelib/thread/qmutexpool_p.h
+++ b/src/corelib/thread/qmutexpool_p.h
@@ -67,11 +67,19 @@ public:
explicit QMutexPool(QMutex::RecursionMode recursionMode = QMutex::NonRecursive, int size = 131);
~QMutexPool();
- QMutex *get(const void *address);
+ inline QMutex *get(const void *address) {
+ int index = uint(quintptr(address)) % mutexes.count();
+ QMutex *m = mutexes[index];
+ if (m)
+ return m;
+ else
+ return createMutex(index);
+ }
static QMutexPool *instance();
static QMutex *globalInstanceGet(const void *address);
private:
+ QMutex *createMutex(int index);
QVarLengthArray<QAtomicPointer<QMutex>, 131> mutexes;
QMutex::RecursionMode recursionMode;
};