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