1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include "qmutexpool_p.h"
49 // qt_global_mutexpool is here for backwards compatability only,
50 // use QMutexpool::instance() in new clode.
51 Q_CORE_EXPORT QMutexPool
*qt_global_mutexpool
= 0;
52 Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool
, globalMutexPool
, (QMutex::Recursive
))
56 \brief The QMutexPool class provides a pool of QMutex objects.
62 QMutexPool is a convenience class that provides access to a fixed
63 number of QMutex objects.
65 Typical use of a QMutexPool is in situations where it is not
66 possible or feasible to use one QMutex for every protected object.
67 The mutex pool will return a mutex based on the address of the
68 object that needs protection.
70 For example, consider this simple class:
72 \snippet doc/src/snippets/code/src_corelib_thread_qmutexpool.cpp 0
74 Adding a QMutex member to the Number class does not make sense,
75 because it is so small. However, in order to ensure that access to
76 each Number is protected, you need to use a mutex. In this case, a
77 QMutexPool would be ideal.
79 Code to calculate the square of a number would then look something
82 \snippet doc/src/snippets/code/src_corelib_thread_qmutexpool.cpp 1
84 This function will safely calculate the square of a number, since
85 it uses a mutex from a QMutexPool. The mutex is locked and
86 unlocked automatically by the QMutexLocker class. See the
87 QMutexLocker documentation for more details.
91 Constructs a QMutexPool, reserving space for \a size QMutexes. All
92 mutexes in the pool are created with \a recursionMode. By default,
93 all mutexes are non-recursive.
95 The QMutexes are created when needed, and deleted when the
96 QMutexPool is destructed.
98 QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode
, int size
)
99 : mutexes(size
), recursionMode(recursionMode
)
101 for (int index
= 0; index
< mutexes
.count(); ++index
) {
107 Destructs a QMutexPool. All QMutexes that were created by the pool
110 QMutexPool::~QMutexPool()
112 for (int index
= 0; index
< mutexes
.count(); ++index
) {
113 delete mutexes
[index
];
119 Returns the global QMutexPool instance.
121 QMutexPool
*QMutexPool::instance()
123 return globalMutexPool();
127 Returns a QMutex from the pool. QMutexPool uses the value \a address
128 to determine which mutex is returned from the pool.
130 QMutex
*QMutexPool::get(const void *address
)
132 Q_ASSERT_X(address
!= 0, "QMutexPool::get()", "'address' argument cannot be zero");
133 int index
= int((quintptr(address
) >> (sizeof(address
) >> 1)) % mutexes
.count());
135 if (!mutexes
[index
]) {
136 // mutex not created, create one
137 QMutex
*newMutex
= new QMutex(recursionMode
);
138 if (!mutexes
[index
].testAndSetOrdered(0, newMutex
))
142 return mutexes
[index
];
146 Returns a QMutex from the global mutex pool.
148 QMutex
*QMutexPool::globalInstanceGet(const void *address
)
150 QMutexPool
* const globalInstance
= globalMutexPool();
151 if (globalInstance
== 0)
153 return globalInstance
->get(address
);
158 #endif // QT_NO_THREAD