QMutexPool: avoid QVarLengthArray of QAtomicPointers
QAtomicPointer is CopyConstructible, but std::atomic<T*> is not, for a reason. So avoid putting them in a QVarLengthArray, using a dynamic heap allocation instead. This sounds wasteful until you realize that virtually all users of QMutexPool (and we know them all) use the global instance(), and that each QMutex (131, by default) is heap-allocated, too. Change-Id: Ie9c95671ec42a1f51919c18631b623aad2c0d6ba Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
8586cccc07
commit
4579d966af
@ -92,11 +92,10 @@ Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool, globalMutexPool, (QMutex::Recursive))
|
|||||||
QMutexPool is destructed.
|
QMutexPool is destructed.
|
||||||
*/
|
*/
|
||||||
QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size)
|
QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size)
|
||||||
: mutexes(size), recursionMode(recursionMode)
|
: count(size),
|
||||||
|
mutexes(new QAtomicPointer<QMutex>[size]()), // (): zero-initialize
|
||||||
|
recursionMode(recursionMode)
|
||||||
{
|
{
|
||||||
for (int index = 0; index < mutexes.count(); ++index) {
|
|
||||||
mutexes[index].store(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -105,8 +104,8 @@ QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size)
|
|||||||
*/
|
*/
|
||||||
QMutexPool::~QMutexPool()
|
QMutexPool::~QMutexPool()
|
||||||
{
|
{
|
||||||
for (int index = 0; index < mutexes.count(); ++index)
|
qDeleteAll(mutexes, mutexes + count);
|
||||||
delete mutexes[index].load();
|
delete[] mutexes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -66,7 +66,7 @@ public:
|
|||||||
~QMutexPool();
|
~QMutexPool();
|
||||||
|
|
||||||
inline QMutex *get(const void *address) {
|
inline QMutex *get(const void *address) {
|
||||||
int index = uint(quintptr(address)) % mutexes.count();
|
int index = uint(quintptr(address)) % count;
|
||||||
QMutex *m = mutexes[index].load();
|
QMutex *m = mutexes[index].load();
|
||||||
if (m)
|
if (m)
|
||||||
return m;
|
return m;
|
||||||
@ -78,7 +78,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QMutex *createMutex(int index);
|
QMutex *createMutex(int index);
|
||||||
QVarLengthArray<QAtomicPointer<QMutex>, 131> mutexes;
|
int count;
|
||||||
|
QAtomicPointer<QMutex> *mutexes;
|
||||||
QMutex::RecursionMode recursionMode;
|
QMutex::RecursionMode recursionMode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user