From b63e0f7c612f7e63831dbee3565292ddca7dba59 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Tue, 29 Mar 2011 11:02:20 +0200 Subject: Optimize QMutexPool Make the get() function inline and simplify the hash computation Reviewed-by: brad --- src/corelib/thread/qmutexpool.cpp | 22 ++++++++++------------ src/corelib/thread/qmutexpool_p.h | 10 +++++++++- 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, 131> mutexes; QMutex::RecursionMode recursionMode; }; -- cgit v0.12