1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include "qmutexpool_p.h"
49
Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool, globalMutexPool, (QMutex::Recursive))
54
\brief The QMutexPool class provides a pool of QMutex objects.
60
QMutexPool is a convenience class that provides access to a fixed
61
number of QMutex objects.
63
Typical use of a QMutexPool is in situations where it is not
64
possible or feasible to use one QMutex for every protected object.
65
The mutex pool will return a mutex based on the address of the
66
object that needs protection.
68
For example, consider this simple class:
70
\snippet code/src_corelib_thread_qmutexpool.cpp 0
72
Adding a QMutex member to the Number class does not make sense,
73
because it is so small. However, in order to ensure that access to
74
each Number is protected, you need to use a mutex. In this case, a
75
QMutexPool would be ideal.
77
Code to calculate the square of a number would then look something
80
\snippet code/src_corelib_thread_qmutexpool.cpp 1
82
This function will safely calculate the square of a number, since
83
it uses a mutex from a QMutexPool. The mutex is locked and
84
unlocked automatically by the QMutexLocker class. See the
85
QMutexLocker documentation for more details.
89
Constructs a QMutexPool, reserving space for \a size QMutexes. All
90
mutexes in the pool are created with \a recursionMode. By default,
91
all mutexes are non-recursive.
93
The QMutexes are created when needed, and deleted when the
94
QMutexPool is destructed.
96
QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size)
97
: mutexes(size), recursionMode(recursionMode)
99
for (int index = 0; index < mutexes.count(); ++index) {
100
mutexes[index].store(0);
105
Destructs a QMutexPool. All QMutexes that were created by the pool
108
QMutexPool::~QMutexPool()
110
for (int index = 0; index < mutexes.count(); ++index)
111
delete mutexes[index].load();
115
Returns the global QMutexPool instance.
117
QMutexPool *QMutexPool::instance()
119
return globalMutexPool();
123
\fn QMutexPool::get(const void *address)
124
Returns a QMutex from the pool. QMutexPool uses the value \a address
125
to determine which mutex is returned from the pool.
130
create the mutex for the given index
132
QMutex *QMutexPool::createMutex(int index)
134
// mutex not created, create one
135
QMutex *newMutex = new QMutex(recursionMode);
136
if (!mutexes[index].testAndSetRelease(0, newMutex))
138
return mutexes[index].load();
142
Returns a QMutex from the global mutex pool.
144
QMutex *QMutexPool::globalInstanceGet(const void *address)
146
QMutexPool * const globalInstance = globalMutexPool();
147
if (globalInstance == 0)
149
return globalInstance->get(address);
154
#endif // QT_NO_THREAD