| Line | Source Code | Coverage | 
|---|
| 1 | /**************************************************************************** | - | 
| 2 | ** | - | 
| 3 | ** Copyright (C) 2013 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 |  | - | 
| 47 | QT_BEGIN_NAMESPACE | - | 
| 48 |  | - | 
| 49 | Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool, globalMutexPool, (QMutex::Recursive)) never executed: delete x; executed:  return thisGlobalStatic.pointer.load();Execution Count:44701 partially evaluated:  !thisGlobalStatic.pointer.testAndSetOrdered(0, x)| no Evaluation Count:0 | yes Evaluation Count:33 | 
 evaluated:  !thisGlobalStatic.pointer.load()| yes Evaluation Count:33 | yes Evaluation Count:44668 | 
 partially evaluated:  !thisGlobalStatic.destroyed| yes Evaluation Count:33 | no Evaluation Count:0 | 
 | 0-44701 | 
| 50 |  | - | 
| 51 | /*! | - | 
| 52 | \class QMutexPool | - | 
| 53 | \inmodule QtCore | - | 
| 54 | \brief The QMutexPool class provides a pool of QMutex objects. | - | 
| 55 |  | - | 
| 56 | \internal | - | 
| 57 |  | - | 
| 58 | \ingroup thread | - | 
| 59 |  | - | 
| 60 | QMutexPool is a convenience class that provides access to a fixed | - | 
| 61 | number of QMutex objects. | - | 
| 62 |  | - | 
| 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. | - | 
| 67 |  | - | 
| 68 | For example, consider this simple class: | - | 
| 69 |  | - | 
| 70 | \snippet code/src_corelib_thread_qmutexpool.cpp 0 | - | 
| 71 |  | - | 
| 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. | - | 
| 76 |  | - | 
| 77 | Code to calculate the square of a number would then look something | - | 
| 78 | like this: | - | 
| 79 |  | - | 
| 80 | \snippet code/src_corelib_thread_qmutexpool.cpp 1 | - | 
| 81 |  | - | 
| 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. | - | 
| 86 | */ | - | 
| 87 |  | - | 
| 88 | /*! | - | 
| 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. | - | 
| 92 |  | - | 
| 93 | The QMutexes are created when needed, and deleted when the | - | 
| 94 | QMutexPool is destructed. | - | 
| 95 | */ | - | 
| 96 | QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size) | - | 
| 97 | : mutexes(size), recursionMode(recursionMode) | - | 
| 98 | { | - | 
| 99 | for (int index = 0; index < mutexes.count(); ++index) { evaluated:  index < mutexes.count()| yes Evaluation Count:4323 | yes Evaluation Count:33 | 
 | 33-4323 | 
| 100 | mutexes[index].store(0); executed (the execution status of this line is deduced):  mutexes[index].store(0); | - | 
| 101 | } executed:  }Execution Count:4323 | 4323 | 
| 102 | } executed:  }Execution Count:33 | 33 | 
| 103 |  | - | 
| 104 | /*! | - | 
| 105 | Destructs a QMutexPool. All QMutexes that were created by the pool | - | 
| 106 | are deleted. | - | 
| 107 | */ | - | 
| 108 | QMutexPool::~QMutexPool() | - | 
| 109 | { | - | 
| 110 | for (int index = 0; index < mutexes.count(); ++index) evaluated:  index < mutexes.count()| yes Evaluation Count:29213 | yes Evaluation Count:223 | 
 | 223-29213 | 
| 111 | delete mutexes[index].load(); executed:  delete mutexes[index].load();Execution Count:29213 | 29213 | 
| 112 | } executed:  }Execution Count:223 | 223 | 
| 113 |  | - | 
| 114 | /*! | - | 
| 115 | Returns the global QMutexPool instance. | - | 
| 116 | */ | - | 
| 117 | QMutexPool *QMutexPool::instance() | - | 
| 118 | { | - | 
| 119 | return globalMutexPool(); never executed: return globalMutexPool(); | 0 | 
| 120 | } | - | 
| 121 |  | - | 
| 122 | /*! | - | 
| 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. | - | 
| 126 | */ | - | 
| 127 |  | - | 
| 128 | /*! | - | 
| 129 | \internal | - | 
| 130 | create the mutex for the given index | - | 
| 131 | */ | - | 
| 132 | QMutex *QMutexPool::createMutex(int index) | - | 
| 133 | { | - | 
| 134 | // mutex not created, create one | - | 
| 135 | QMutex *newMutex = new QMutex(recursionMode); executed (the execution status of this line is deduced):  QMutex *newMutex = new QMutex(recursionMode); | - | 
| 136 | if (!mutexes[index].testAndSetRelease(0, newMutex)) partially evaluated:  !mutexes[index].testAndSetRelease(0, newMutex)| no Evaluation Count:0 | yes Evaluation Count:157 | 
 | 0-157 | 
| 137 | delete newMutex; never executed: delete newMutex; | 0 | 
| 138 | return mutexes[index].load(); executed:  return mutexes[index].load();Execution Count:157 | 157 | 
| 139 | } | - | 
| 140 |  | - | 
| 141 | /*! | - | 
| 142 | Returns a QMutex from the global mutex pool. | - | 
| 143 | */ | - | 
| 144 | QMutex *QMutexPool::globalInstanceGet(const void *address) | - | 
| 145 | { | - | 
| 146 | QMutexPool * const globalInstance = globalMutexPool(); executed (the execution status of this line is deduced):  QMutexPool * const globalInstance = globalMutexPool(); | - | 
| 147 | if (globalInstance == 0) partially evaluated:  globalInstance == 0| no Evaluation Count:0 | yes Evaluation Count:44701 | 
 | 0-44701 | 
| 148 | return 0; never executed: return 0; | 0 | 
| 149 | return globalInstance->get(address); executed:  return globalInstance->get(address);Execution Count:44701 | 44701 | 
| 150 | } | - | 
| 151 |  | - | 
| 152 | QT_END_NAMESPACE | - | 
| 153 |  | - | 
| 154 | #endif // QT_NO_THREAD | - | 
| 155 |  | - | 
|  |  |  |