Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/concurrent/qtconcurrentthreadengine.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 "qtconcurrentthreadengine.h" | - | ||||||||||||
41 | - | |||||||||||||
42 | #ifndef QT_NO_CONCURRENT | - | ||||||||||||
43 | - | |||||||||||||
44 | QT_BEGIN_NAMESPACE | - | ||||||||||||
45 | - | |||||||||||||
46 | namespace QtConcurrent { | - | ||||||||||||
47 | - | |||||||||||||
48 | ThreadEngineBarrier::ThreadEngineBarrier() | - | ||||||||||||
49 | : executed 81088 times by 5 tests: count(0) { }end of block Executed by:
executed 81088 times by 5 tests: end of block Executed by:
| 81088 | ||||||||||||
50 | - | |||||||||||||
51 | void ThreadEngineBarrier::acquire() | - | ||||||||||||
52 | { | - | ||||||||||||
53 | forever { | - | ||||||||||||
54 | int localCount = count.load(); | - | ||||||||||||
55 | if (localCount < 0) {
| 42-161488 | ||||||||||||
56 | if (count.testAndSetOrdered(localCount, localCount -1))
| 0-42 | ||||||||||||
57 | return; executed 42 times by 2 tests: return; Executed by:
| 42 | ||||||||||||
58 | } else { never executed: end of block | 0 | ||||||||||||
59 | if (count.testAndSetOrdered(localCount, localCount + 1))
| 0-161488 | ||||||||||||
60 | return; executed 161488 times by 5 tests: return; Executed by:
| 161488 | ||||||||||||
61 | } never executed: end of block | 0 | ||||||||||||
62 | } | - | ||||||||||||
63 | } never executed: end of block | 0 | ||||||||||||
64 | - | |||||||||||||
65 | int ThreadEngineBarrier::release() | - | ||||||||||||
66 | { | - | ||||||||||||
67 | forever { | - | ||||||||||||
68 | int localCount = count.load(); | - | ||||||||||||
69 | if (localCount == -1) {
| 73-161446 | ||||||||||||
70 | if (count.testAndSetOrdered(-1, 0)) {
| 0-73 | ||||||||||||
71 | semaphore.release(); | - | ||||||||||||
72 | return 0; executed 73 times by 4 tests: return 0; Executed by:
| 73 | ||||||||||||
73 | } | - | ||||||||||||
74 | } else if (localCount < 0) { never executed: end of block
| 0-161404 | ||||||||||||
75 | if (count.testAndSetOrdered(localCount, localCount + 1))
| 0-42 | ||||||||||||
76 | return qAbs(localCount + 1); executed 42 times by 2 tests: return qAbs(localCount + 1); Executed by:
| 42 | ||||||||||||
77 | } else { never executed: end of block | 0 | ||||||||||||
78 | if (count.testAndSetOrdered(localCount, localCount - 1))
| 0-161404 | ||||||||||||
79 | return localCount - 1; executed 161404 times by 5 tests: return localCount - 1; Executed by:
| 161404 | ||||||||||||
80 | } never executed: end of block | 0 | ||||||||||||
81 | } | - | ||||||||||||
82 | } never executed: end of block | 0 | ||||||||||||
83 | - | |||||||||||||
84 | // Wait until all threads have been released | - | ||||||||||||
85 | void ThreadEngineBarrier::wait() | - | ||||||||||||
86 | { | - | ||||||||||||
87 | forever { | - | ||||||||||||
88 | int localCount = count.load(); | - | ||||||||||||
89 | if (localCount == 0)
| 73-20496 | ||||||||||||
90 | return; executed 20496 times by 4 tests: return; Executed by:
| 20496 | ||||||||||||
91 | - | |||||||||||||
92 | Q_ASSERT(localCount > 0); // multiple waiters are not allowed. | - | ||||||||||||
93 | if (count.testAndSetOrdered(localCount, -localCount)) {
| 0-73 | ||||||||||||
94 | semaphore.acquire(); | - | ||||||||||||
95 | return; executed 73 times by 4 tests: return; Executed by:
| 73 | ||||||||||||
96 | } | - | ||||||||||||
97 | } never executed: end of block | 0 | ||||||||||||
98 | } never executed: end of block | 0 | ||||||||||||
99 | - | |||||||||||||
100 | int ThreadEngineBarrier::currentCount() | - | ||||||||||||
101 | { | - | ||||||||||||
102 | return count.load(); never executed: return count.load(); | 0 | ||||||||||||
103 | } | - | ||||||||||||
104 | - | |||||||||||||
105 | // releases a thread, unless this is the last thread. | - | ||||||||||||
106 | // returns true if the thread was released. | - | ||||||||||||
107 | bool ThreadEngineBarrier::releaseUnlessLast() | - | ||||||||||||
108 | { | - | ||||||||||||
109 | forever { | - | ||||||||||||
110 | int localCount = count.load(); | - | ||||||||||||
111 | if (qAbs(localCount) == 1) {
| 11-2084 | ||||||||||||
112 | return false; executed 2084 times by 2 tests: return false; Executed by:
| 2084 | ||||||||||||
113 | } else if (localCount < 0) {
| 0-11 | ||||||||||||
114 | if (count.testAndSetOrdered(localCount, localCount + 1))
| 0 | ||||||||||||
115 | return true; never executed: return true; | 0 | ||||||||||||
116 | } else { never executed: end of block | 0 | ||||||||||||
117 | if (count.testAndSetOrdered(localCount, localCount - 1))
| 0-11 | ||||||||||||
118 | return true; executed 11 times by 2 tests: return true; Executed by:
| 11 | ||||||||||||
119 | } never executed: end of block | 0 | ||||||||||||
120 | } | - | ||||||||||||
121 | } never executed: end of block | 0 | ||||||||||||
122 | - | |||||||||||||
123 | ThreadEngineBase::ThreadEngineBase() | - | ||||||||||||
124 | :futureInterface(0), threadPool(QThreadPool::globalInstance()) | - | ||||||||||||
125 | { | - | ||||||||||||
126 | setAutoDelete(false); | - | ||||||||||||
127 | } executed 81088 times by 5 tests: end of block Executed by:
| 81088 | ||||||||||||
128 | - | |||||||||||||
129 | ThreadEngineBase::~ThreadEngineBase() {} | - | ||||||||||||
130 | - | |||||||||||||
131 | void ThreadEngineBase::startSingleThreaded() | - | ||||||||||||
132 | { | - | ||||||||||||
133 | start(); | - | ||||||||||||
134 | while (threadFunction() != ThreadFinished)
| 0-1 | ||||||||||||
135 | ; never executed: ; | 0 | ||||||||||||
136 | finish(); | - | ||||||||||||
137 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
138 | - | |||||||||||||
139 | void ThreadEngineBase::startBlocking() | - | ||||||||||||
140 | { | - | ||||||||||||
141 | start(); | - | ||||||||||||
142 | barrier.acquire(); | - | ||||||||||||
143 | startThreads(); | - | ||||||||||||
144 | - | |||||||||||||
145 | bool throttled = false; | - | ||||||||||||
146 | #ifndef QT_NO_EXCEPTIONS | - | ||||||||||||
147 | try { | - | ||||||||||||
148 | #endif | - | ||||||||||||
149 | while (threadFunction() == ThrottleThread) {
| 11-20554 | ||||||||||||
150 | if (threadThrottleExit()) {
| 0-11 | ||||||||||||
151 | throttled = true; | - | ||||||||||||
152 | break; executed 11 times by 2 tests: break; Executed by:
| 11 | ||||||||||||
153 | } | - | ||||||||||||
154 | } never executed: end of block | 0 | ||||||||||||
155 | #ifndef QT_NO_EXCEPTIONS | - | ||||||||||||
156 | } catch (QException &e) { executed 20565 times by 4 tests: end of block Executed by:
| 20565 | ||||||||||||
157 | handleException(e); | - | ||||||||||||
158 | } catch (...) { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
159 | handleException(QUnhandledException()); | - | ||||||||||||
160 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
161 | #endif | - | ||||||||||||
162 | - | |||||||||||||
163 | if (throttled == false) {
| 11-20558 | ||||||||||||
164 | barrier.release(); | - | ||||||||||||
165 | } executed 20558 times by 4 tests: end of block Executed by:
| 20558 | ||||||||||||
166 | - | |||||||||||||
167 | barrier.wait(); | - | ||||||||||||
168 | finish(); | - | ||||||||||||
169 | exceptionStore.throwPossibleException(); | - | ||||||||||||
170 | } executed 20565 times by 4 tests: end of block Executed by:
| 20565 | ||||||||||||
171 | - | |||||||||||||
172 | void ThreadEngineBase::startThread() | - | ||||||||||||
173 | { | - | ||||||||||||
174 | startThreadInternal(); | - | ||||||||||||
175 | } executed 19222 times by 4 tests: end of block Executed by:
| 19222 | ||||||||||||
176 | - | |||||||||||||
177 | void ThreadEngineBase::acquireBarrierSemaphore() | - | ||||||||||||
178 | { | - | ||||||||||||
179 | barrier.acquire(); | - | ||||||||||||
180 | } executed 60519 times by 5 tests: end of block Executed by:
| 60519 | ||||||||||||
181 | - | |||||||||||||
182 | bool ThreadEngineBase::isCanceled() | - | ||||||||||||
183 | { | - | ||||||||||||
184 | if (futureInterface)
| 48099-133141 | ||||||||||||
185 | return futureInterface->isCanceled(); executed 133141 times by 5 tests: return futureInterface->isCanceled(); Executed by:
| 133141 | ||||||||||||
186 | else | - | ||||||||||||
187 | return false; executed 48099 times by 4 tests: return false; Executed by:
| 48099 | ||||||||||||
188 | } | - | ||||||||||||
189 | - | |||||||||||||
190 | void ThreadEngineBase::waitForResume() | - | ||||||||||||
191 | { | - | ||||||||||||
192 | if (futureInterface)
| 6309-13666 | ||||||||||||
193 | futureInterface->waitForResume(); executed 6309 times by 4 tests: futureInterface->waitForResume(); Executed by:
| 6309 | ||||||||||||
194 | } executed 19975 times by 4 tests: end of block Executed by:
| 19975 | ||||||||||||
195 | - | |||||||||||||
196 | bool ThreadEngineBase::isProgressReportingEnabled() | - | ||||||||||||
197 | { | - | ||||||||||||
198 | // If we don't have a QFuture, there is no-one to report the progress to. | - | ||||||||||||
199 | return (futureInterface != 0); executed 20914 times by 4 tests: return (futureInterface != 0); Executed by:
| 20914 | ||||||||||||
200 | } | - | ||||||||||||
201 | - | |||||||||||||
202 | void ThreadEngineBase::setProgressValue(int progress) | - | ||||||||||||
203 | { | - | ||||||||||||
204 | if (futureInterface)
| 0-5904 | ||||||||||||
205 | futureInterface->setProgressValue(progress); executed 5904 times by 4 tests: futureInterface->setProgressValue(progress); Executed by:
| 5904 | ||||||||||||
206 | } executed 5904 times by 4 tests: end of block Executed by:
| 5904 | ||||||||||||
207 | - | |||||||||||||
208 | void ThreadEngineBase::setProgressRange(int minimum, int maximum) | - | ||||||||||||
209 | { | - | ||||||||||||
210 | if (futureInterface)
| 0-259 | ||||||||||||
211 | futureInterface->setProgressRange(minimum, maximum); executed 259 times by 4 tests: futureInterface->setProgressRange(minimum, maximum); Executed by:
| 259 | ||||||||||||
212 | } executed 259 times by 4 tests: end of block Executed by:
| 259 | ||||||||||||
213 | - | |||||||||||||
214 | bool ThreadEngineBase::startThreadInternal() | - | ||||||||||||
215 | { | - | ||||||||||||
216 | if (this->isCanceled())
| 0-80442 | ||||||||||||
217 | return false; never executed: return false; | 0 | ||||||||||||
218 | - | |||||||||||||
219 | barrier.acquire(); | - | ||||||||||||
220 | if (!threadPool->tryStart(this)) {
| 237-80205 | ||||||||||||
221 | barrier.release(); | - | ||||||||||||
222 | return false; executed 80205 times by 5 tests: return false; Executed by:
| 80205 | ||||||||||||
223 | } | - | ||||||||||||
224 | return true; executed 237 times by 4 tests: return true; Executed by:
| 237 | ||||||||||||
225 | } | - | ||||||||||||
226 | - | |||||||||||||
227 | void ThreadEngineBase::startThreads() | - | ||||||||||||
228 | { | - | ||||||||||||
229 | while (shouldStartThread() && startThreadInternal())
| 236-61220 | ||||||||||||
230 | ; executed 236 times by 4 tests: ; Executed by:
| 236 | ||||||||||||
231 | } executed 81223 times by 5 tests: end of block Executed by:
| 81223 | ||||||||||||
232 | - | |||||||||||||
233 | void ThreadEngineBase::threadExit() | - | ||||||||||||
234 | { | - | ||||||||||||
235 | const bool asynchronous = futureInterface != 0; | - | ||||||||||||
236 | const int lastThread = (barrier.release() == 0); | - | ||||||||||||
237 | - | |||||||||||||
238 | if (lastThread && asynchronous)
| 73-60592 | ||||||||||||
239 | this->asynchronousFinish(); executed 60519 times by 5 tests: this->asynchronousFinish(); Executed by:
| 60519 | ||||||||||||
240 | } executed 60756 times by 5 tests: end of block Executed by:
| 60756 | ||||||||||||
241 | - | |||||||||||||
242 | // Called by a worker thread that wants to be throttled. If the current number | - | ||||||||||||
243 | // of running threads is larger than one the thread is allowed to exit and | - | ||||||||||||
244 | // this function returns one. | - | ||||||||||||
245 | bool ThreadEngineBase::threadThrottleExit() | - | ||||||||||||
246 | { | - | ||||||||||||
247 | return barrier.releaseUnlessLast(); executed 2095 times by 2 tests: return barrier.releaseUnlessLast(); Executed by:
| 2095 | ||||||||||||
248 | } | - | ||||||||||||
249 | - | |||||||||||||
250 | void ThreadEngineBase::run() // implements QRunnable. | - | ||||||||||||
251 | { | - | ||||||||||||
252 | if (this->isCanceled()) {
| 102-60654 | ||||||||||||
253 | threadExit(); | - | ||||||||||||
254 | return; executed 102 times by 3 tests: return; Executed by:
| 102 | ||||||||||||
255 | } | - | ||||||||||||
256 | - | |||||||||||||
257 | startThreads(); | - | ||||||||||||
258 | - | |||||||||||||
259 | #ifndef QT_NO_EXCEPTIONS | - | ||||||||||||
260 | try { | - | ||||||||||||
261 | #endif | - | ||||||||||||
262 | while (threadFunction() == ThrottleThread) {
| 2084-60647 | ||||||||||||
263 | // threadFunction returning ThrottleThread means it that the user | - | ||||||||||||
264 | // struct wants to be throttled by making a worker thread exit. | - | ||||||||||||
265 | // Respect that request unless this is the only worker thread left | - | ||||||||||||
266 | // running, in which case it has to keep going. | - | ||||||||||||
267 | if (threadThrottleExit())
| 0-2084 | ||||||||||||
268 | return; never executed: return; | 0 | ||||||||||||
269 | } executed 2084 times by 2 tests: end of block Executed by:
| 2084 | ||||||||||||
270 | - | |||||||||||||
271 | #ifndef QT_NO_EXCEPTIONS | - | ||||||||||||
272 | } catch (QException &e) { executed 60647 times by 5 tests: end of block Executed by:
| 60647 | ||||||||||||
273 | handleException(e); | - | ||||||||||||
274 | } catch (...) { executed 4 times by 2 tests: end of block Executed by:
| 4 | ||||||||||||
275 | handleException(QUnhandledException()); | - | ||||||||||||
276 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
277 | #endif | - | ||||||||||||
278 | threadExit(); | - | ||||||||||||
279 | } executed 60654 times by 5 tests: end of block Executed by:
| 60654 | ||||||||||||
280 | - | |||||||||||||
281 | #ifndef QT_NO_EXCEPTIONS | - | ||||||||||||
282 | - | |||||||||||||
283 | void ThreadEngineBase::handleException(const QException &exception) | - | ||||||||||||
284 | { | - | ||||||||||||
285 | if (futureInterface)
| 3-8 | ||||||||||||
286 | futureInterface->reportException(exception); executed 3 times by 2 tests: futureInterface->reportException(exception); Executed by:
| 3 | ||||||||||||
287 | else | - | ||||||||||||
288 | exceptionStore.setException(exception); executed 8 times by 1 test: exceptionStore.setException(exception); Executed by:
| 8 | ||||||||||||
289 | } | - | ||||||||||||
290 | #endif | - | ||||||||||||
291 | - | |||||||||||||
292 | - | |||||||||||||
293 | } // namepsace QtConcurrent | - | ||||||||||||
294 | - | |||||||||||||
295 | QT_END_NAMESPACE | - | ||||||||||||
296 | - | |||||||||||||
297 | #endif // QT_NO_CONCURRENT | - | ||||||||||||
Source code | Switch to Preprocessed file |