Qt 4.8
Public Functions | Static Public Functions | Private Functions | Properties | List of all members
QMutexPool Class Reference

The QMutexPool class provides a pool of QMutex objects. More...

#include <qmutexpool_p.h>

Public Functions

QMutexget (const void *address)
 Returns a QMutex from the pool. More...
 
 QMutexPool (QMutex::RecursionMode recursionMode=QMutex::NonRecursive, int size=131)
 Constructs a QMutexPool, reserving space for size QMutexes. More...
 
 ~QMutexPool ()
 Destructs a QMutexPool. More...
 

Static Public Functions

static QMutexglobalInstanceGet (const void *address)
 Returns a QMutex from the global mutex pool. More...
 
static QMutexPoolinstance ()
 Returns the global QMutexPool instance. More...
 

Private Functions

QMutexcreateMutex (int index)
 

Properties

QVarLengthArray< QAtomicPointer< QMutex >, 131 > mutexes
 
QMutex::RecursionMode recursionMode
 

Detailed Description

The QMutexPool class provides a pool of QMutex objects.

Warning
This function is not part of the public interface.

QMutexPool is a convenience class that provides access to a fixed number of QMutex objects.

Typical use of a QMutexPool is in situations where it is not possible or feasible to use one QMutex for every protected object. The mutex pool will return a mutex based on the address of the object that needs protection.

For example, consider this simple class:

class Number {
public:
Number(double n) : num (n) { }
void setNumber(double n) { num = n; }
double number() const { return num; }
private:
double num;
};

Adding a QMutex member to the Number class does not make sense, because it is so small. However, in order to ensure that access to each Number is protected, you need to use a mutex. In this case, a QMutexPool would be ideal.

Code to calculate the square of a number would then look something like this:

void calcSquare(Number *num)
{
QMutexLocker locker(mutexpool.get(num));
num.setNumber(num.number() * num.number());
}

This function will safely calculate the square of a number, since it uses a mutex from a QMutexPool. The mutex is locked and unlocked automatically by the QMutexLocker class. See the QMutexLocker documentation for more details.

Definition at line 64 of file qmutexpool_p.h.

Constructors and Destructors

◆ QMutexPool()

QMutexPool::QMutexPool ( QMutex::RecursionMode  recursionMode = QMutex::NonRecursive,
int  size = 131 
)
explicit

Constructs a QMutexPool, reserving space for size QMutexes.

All mutexes in the pool are created with recursionMode. By default, all mutexes are non-recursive.

The QMutexes are created when needed, and deleted when the QMutexPool is destructed.

Definition at line 101 of file qmutexpool.cpp.

103 {
104  for (int index = 0; index < mutexes.count(); ++index) {
105  mutexes[index] = 0;
106  }
107 }
QMutex::RecursionMode recursionMode
Definition: qmutexpool_p.h:84
int count() const
QVarLengthArray< QAtomicPointer< QMutex >, 131 > mutexes
Definition: qmutexpool_p.h:83
quint16 index

◆ ~QMutexPool()

QMutexPool::~QMutexPool ( )

Destructs a QMutexPool.

All QMutexes that were created by the pool are deleted.

Definition at line 113 of file qmutexpool.cpp.

114 {
115  for (int index = 0; index < mutexes.count(); ++index) {
116  delete mutexes[index];
117  mutexes[index] = 0;
118  }
119 }
int count() const
QVarLengthArray< QAtomicPointer< QMutex >, 131 > mutexes
Definition: qmutexpool_p.h:83
quint16 index

Functions

◆ createMutex()

QMutex * QMutexPool::createMutex ( int  index)
private
Warning
This function is not part of the public interface. create the mutex for the given index

Definition at line 138 of file qmutexpool.cpp.

139 {
140  // mutex not created, create one
141  QMutex *newMutex = new QMutex(recursionMode);
142  if (!mutexes[index].testAndSetOrdered(0, newMutex))
143  delete newMutex;
144  return mutexes[index];
145 }
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
QMutex::RecursionMode recursionMode
Definition: qmutexpool_p.h:84
QVarLengthArray< QAtomicPointer< QMutex >, 131 > mutexes
Definition: qmutexpool_p.h:83
quint16 index

◆ get()

QMutexPool::get ( const void *  address)
inline

Returns a QMutex from the pool.

QMutexPool uses the value address to determine which mutex is returned from the pool.

Definition at line 70 of file qmutexpool_p.h.

Referenced by globalInstanceGet(), and signalSlotLock().

70  {
71  int index = uint(quintptr(address)) % mutexes.count();
72  QMutex *m = mutexes[index];
73  if (m)
74  return m;
75  else
76  return createMutex(index);
77  }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
unsigned int uint
Definition: qglobal.h:996
int count() const
QVarLengthArray< QAtomicPointer< QMutex >, 131 > mutexes
Definition: qmutexpool_p.h:83
QMutex * createMutex(int index)
Definition: qmutexpool.cpp:138
quint16 index

◆ globalInstanceGet()

QMutex * QMutexPool::globalInstanceGet ( const void *  address)
static

Returns a QMutex from the global mutex pool.

Definition at line 150 of file qmutexpool.cpp.

Referenced by QHostInfoAgent::fromName(), QVariantAnimationPrivate::getInterpolator(), ignoreSigPipe(), QSslCertificate::issuerInfo(), q_resolveOpenSslSymbols(), qdbus_loadLibDBus(), resolveUNCLibs(), QSslCertificate::serialNumber(), QSslCertificate::subjectInfo(), QPropertyAnimation::updateState(), and QSslCertificate::version().

151 {
152  QMutexPool * const globalInstance = globalMutexPool();
153  if (globalInstance == 0)
154  return 0;
155  return globalInstance->get(address);
156 }
QMutex * get(const void *address)
Returns a QMutex from the pool.
Definition: qmutexpool_p.h:70
The QMutexPool class provides a pool of QMutex objects.
Definition: qmutexpool_p.h:64

◆ instance()

QMutexPool * QMutexPool::instance ( )
static

Returns the global QMutexPool instance.

Definition at line 124 of file qmutexpool.cpp.

Referenced by QThread::initialize(), and QEventDispatcherGlibPrivate::QEventDispatcherGlibPrivate().

125 {
126  return globalMutexPool();
127 }

Properties

◆ mutexes

QVarLengthArray<QAtomicPointer<QMutex>, 131> QMutexPool::mutexes
private

Definition at line 83 of file qmutexpool_p.h.

Referenced by createMutex(), and ~QMutexPool().

◆ recursionMode

QMutex::RecursionMode QMutexPool::recursionMode
private

Definition at line 84 of file qmutexpool_p.h.

Referenced by createMutex().


The documentation for this class was generated from the following files: