Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qmutexpool.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
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 The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qatomic.h" | - | ||||||
41 | #include "qmutexpool_p.h" | - | ||||||
42 | - | |||||||
43 | #ifndef QT_NO_THREAD | - | ||||||
44 | - | |||||||
45 | QT_BEGIN_NAMESPACE | - | ||||||
46 | - | |||||||
47 | Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool, globalMutexPool, (QMutex::Recursive)) executed 16 times by 16 tests: end of block Executed by:
executed 16 times by 16 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 143976 times by 32 tests: return &holder.value; Executed by:
| 0-143976 | ||||||
48 | - | |||||||
49 | /*! | - | ||||||
50 | \class QMutexPool | - | ||||||
51 | \inmodule QtCore | - | ||||||
52 | \brief The QMutexPool class provides a pool of QMutex objects. | - | ||||||
53 | - | |||||||
54 | \internal | - | ||||||
55 | - | |||||||
56 | \ingroup thread | - | ||||||
57 | - | |||||||
58 | QMutexPool is a convenience class that provides access to a fixed | - | ||||||
59 | number of QMutex objects. | - | ||||||
60 | - | |||||||
61 | Typical use of a QMutexPool is in situations where it is not | - | ||||||
62 | possible or feasible to use one QMutex for every protected object. | - | ||||||
63 | The mutex pool will return a mutex based on the address of the | - | ||||||
64 | object that needs protection. | - | ||||||
65 | - | |||||||
66 | For example, consider this simple class: | - | ||||||
67 | - | |||||||
68 | \snippet code/src_corelib_thread_qmutexpool.cpp 0 | - | ||||||
69 | - | |||||||
70 | Adding a QMutex member to the Number class does not make sense, | - | ||||||
71 | because it is so small. However, in order to ensure that access to | - | ||||||
72 | each Number is protected, you need to use a mutex. In this case, a | - | ||||||
73 | QMutexPool would be ideal. | - | ||||||
74 | - | |||||||
75 | Code to calculate the square of a number would then look something | - | ||||||
76 | like this: | - | ||||||
77 | - | |||||||
78 | \snippet code/src_corelib_thread_qmutexpool.cpp 1 | - | ||||||
79 | - | |||||||
80 | This function will safely calculate the square of a number, since | - | ||||||
81 | it uses a mutex from a QMutexPool. The mutex is locked and | - | ||||||
82 | unlocked automatically by the QMutexLocker class. See the | - | ||||||
83 | QMutexLocker documentation for more details. | - | ||||||
84 | */ | - | ||||||
85 | - | |||||||
86 | /*! | - | ||||||
87 | Constructs a QMutexPool, reserving space for \a size QMutexes. All | - | ||||||
88 | mutexes in the pool are created with \a recursionMode. By default, | - | ||||||
89 | all mutexes are non-recursive. | - | ||||||
90 | - | |||||||
91 | The QMutexes are created when needed, and deleted when the | - | ||||||
92 | QMutexPool is destructed. | - | ||||||
93 | */ | - | ||||||
94 | QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size) | - | ||||||
95 | : mutexes(size), recursionMode(recursionMode) | - | ||||||
96 | { | - | ||||||
97 | for (int index = 0; index < mutexes.count(); ++index) {
| 16-2096 | ||||||
98 | mutexes[index].store(0); | - | ||||||
99 | } executed 2096 times by 16 tests: end of block Executed by:
| 2096 | ||||||
100 | } executed 16 times by 16 tests: end of block Executed by:
| 16 | ||||||
101 | - | |||||||
102 | /*! | - | ||||||
103 | Destructs a QMutexPool. All QMutexes that were created by the pool | - | ||||||
104 | are deleted. | - | ||||||
105 | */ | - | ||||||
106 | QMutexPool::~QMutexPool() | - | ||||||
107 | { | - | ||||||
108 | for (int index = 0; index < mutexes.count(); ++index)
| 16-2096 | ||||||
109 | delete mutexes[index].load(); executed 2096 times by 16 tests: delete mutexes[index].load(); Executed by:
| 2096 | ||||||
110 | } executed 16 times by 16 tests: end of block Executed by:
| 16 | ||||||
111 | - | |||||||
112 | /*! | - | ||||||
113 | Returns the global QMutexPool instance. | - | ||||||
114 | */ | - | ||||||
115 | QMutexPool *QMutexPool::instance() | - | ||||||
116 | { | - | ||||||
117 | return globalMutexPool(); never executed: return globalMutexPool(); | 0 | ||||||
118 | } | - | ||||||
119 | - | |||||||
120 | /*! | - | ||||||
121 | \fn QMutexPool::get(const void *address) | - | ||||||
122 | Returns a QMutex from the pool. QMutexPool uses the value \a address | - | ||||||
123 | to determine which mutex is returned from the pool. | - | ||||||
124 | */ | - | ||||||
125 | - | |||||||
126 | /*! | - | ||||||
127 | \internal | - | ||||||
128 | create the mutex for the given index | - | ||||||
129 | */ | - | ||||||
130 | QMutex *QMutexPool::createMutex(int index) | - | ||||||
131 | { | - | ||||||
132 | // mutex not created, create one | - | ||||||
133 | QMutex *newMutex = new QMutex(recursionMode); | - | ||||||
134 | if (!mutexes[index].testAndSetRelease(0, newMutex))
| 0-340 | ||||||
135 | delete newMutex; never executed: delete newMutex; | 0 | ||||||
136 | return mutexes[index].load(); executed 340 times by 17 tests: return mutexes[index].load(); Executed by:
| 340 | ||||||
137 | } | - | ||||||
138 | - | |||||||
139 | /*! | - | ||||||
140 | Returns a QMutex from the global mutex pool. | - | ||||||
141 | */ | - | ||||||
142 | QMutex *QMutexPool::globalInstanceGet(const void *address) | - | ||||||
143 | { | - | ||||||
144 | QMutexPool * const globalInstance = globalMutexPool(); | - | ||||||
145 | if (globalInstance == 0)
| 0-143976 | ||||||
146 | return 0; never executed: return 0; | 0 | ||||||
147 | return globalInstance->get(address); executed 143976 times by 32 tests: return globalInstance->get(address); Executed by:
| 143976 | ||||||
148 | } | - | ||||||
149 | - | |||||||
150 | QT_END_NAMESPACE | - | ||||||
151 | - | |||||||
152 | #endif // QT_NO_THREAD | - | ||||||
Source code | Switch to Preprocessed file |