summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2009-03-27 14:19:36 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2009-03-27 15:45:18 (GMT)
commita3bb85673a4bf9aa7c629ab74b483a2faaa30369 (patch)
tree272a297ac872ca30eb094ea01b0911de453ca0bb /src
parent43c5656d834739e86f2895cfbf3d539bb8c7c498 (diff)
downloadQt-a3bb85673a4bf9aa7c629ab74b483a2faaa30369.zip
Qt-a3bb85673a4bf9aa7c629ab74b483a2faaa30369.tar.gz
Qt-a3bb85673a4bf9aa7c629ab74b483a2faaa30369.tar.bz2
Make QMutexPool actually be thread-safe (double-checked locking doesn't
work) This code was apparently mostly unchanged since 2005. Update it to match the atomics code. Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/thread/qmutexpool.cpp15
-rw-r--r--src/corelib/thread/qmutexpool_p.h4
2 files changed, 6 insertions, 13 deletions
diff --git a/src/corelib/thread/qmutexpool.cpp b/src/corelib/thread/qmutexpool.cpp
index 71ecab5..96a9940 100644
--- a/src/corelib/thread/qmutexpool.cpp
+++ b/src/corelib/thread/qmutexpool.cpp
@@ -96,9 +96,8 @@ Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool, globalMutexPool, (true))
QMutexPool is destructed.
*/
QMutexPool::QMutexPool(bool recursive, int size)
- : count(size), recurs(recursive)
+ : mutexes(size), count(size), recurs(recursive)
{
- mutexes = new QMutex*[count];
for (int index = 0; index < count; ++index) {
mutexes[index] = 0;
}
@@ -110,13 +109,10 @@ QMutexPool::QMutexPool(bool recursive, int size)
*/
QMutexPool::~QMutexPool()
{
- QMutexLocker locker(&mutex);
for (int index = 0; index < count; ++index) {
delete mutexes[index];
mutexes[index] = 0;
}
- delete [] mutexes;
- mutexes = 0;
}
/*!
@@ -138,12 +134,9 @@ QMutex *QMutexPool::get(const void *address)
if (!mutexes[index]) {
// mutex not created, create one
-
- QMutexLocker locker(&mutex);
- // we need to check once again that the mutex hasn't been created, since
- // 2 threads could be trying to create a mutex at the same index...
- if (!mutexes[index])
- mutexes[index] = new QMutex(recurs ? QMutex::Recursive : QMutex::NonRecursive);
+ QMutex *newMutex = new QMutex(recurs ? QMutex::Recursive : QMutex::NonRecursive);
+ 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 65a3b54..4b91e5b 100644
--- a/src/corelib/thread/qmutexpool_p.h
+++ b/src/corelib/thread/qmutexpool_p.h
@@ -54,6 +54,7 @@
//
#include "QtCore/qmutex.h"
+#include "QtCore/qvarlengtharray.h"
#ifndef QT_NO_THREAD
@@ -70,8 +71,7 @@ public:
static QMutex *globalInstanceGet(const void *address);
private:
- QMutex mutex;
- QMutex **mutexes;
+ QVarLengthArray<QAtomicPointer<QMutex>, 128> mutexes;
int count;
bool recurs;
};