Qt 4.8
qmutexpool.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qatomic.h"
43 #include "qmutexpool_p.h"
44 
45 #ifndef QT_NO_THREAD
46 
48 
49 // qt_global_mutexpool is here for backwards compatibility only,
50 // use QMutexpool::instance() in new clode.
53 
54 
101 QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size)
102  : mutexes(size), recursionMode(recursionMode)
103 {
104  for (int index = 0; index < mutexes.count(); ++index) {
105  mutexes[index] = 0;
106  }
107 }
108 
114 {
115  for (int index = 0; index < mutexes.count(); ++index) {
116  delete mutexes[index];
117  mutexes[index] = 0;
118  }
119 }
120 
125 {
126  return globalMutexPool();
127 }
128 
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 }
146 
151 {
152  QMutexPool * const globalInstance = globalMutexPool();
153  if (globalInstance == 0)
154  return 0;
155  return globalInstance->get(address);
156 }
157 
159 
160 #endif // QT_NO_THREAD
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
QMutex::RecursionMode recursionMode
Definition: qmutexpool_p.h:84
~QMutexPool()
Destructs a QMutexPool.
Definition: qmutexpool.cpp:113
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QMutexPool * instance()
Returns the global QMutexPool instance.
Definition: qmutexpool.cpp:124
QMutex * get(const void *address)
Returns a QMutex from the pool.
Definition: qmutexpool_p.h:70
Q_CORE_EXPORT QMutexPool * qt_global_mutexpool
Definition: qmutexpool.cpp:51
int count() const
QVarLengthArray< QAtomicPointer< QMutex >, 131 > mutexes
Definition: qmutexpool_p.h:83
QMutex * createMutex(int index)
Definition: qmutexpool.cpp:138
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
The QMutexPool class provides a pool of QMutex objects.
Definition: qmutexpool_p.h:64
quint16 index
static QMutex * globalInstanceGet(const void *address)
Returns a QMutex from the global mutex pool.
Definition: qmutexpool.cpp:150
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
Declares a global static variable with the specified type and name.
Definition: qglobal.h:1982