Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2012 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 "qtconcurrentthreadengine.h" | - |
43 | | - |
44 | #ifndef QT_NO_CONCURRENT | - |
45 | | - |
46 | QT_BEGIN_NAMESPACE | - |
47 | | - |
48 | namespace QtConcurrent { | - |
49 | | - |
50 | ThreadEngineBarrier::ThreadEngineBarrier() | - |
51 | :count(0) { } executed: } Execution Count:81049 | 81049 |
52 | | - |
53 | void ThreadEngineBarrier::acquire() | - |
54 | { | - |
55 | forever { executed (the execution status of this line is deduced): for(;;) { | - |
56 | int localCount = count.load(); executed (the execution status of this line is deduced): int localCount = count.load(); | - |
57 | if (localCount < 0) { evaluated: localCount < 0 yes Evaluation Count:917 | yes Evaluation Count:556928 |
| 917-556928 |
58 | if (count.testAndSetOrdered(localCount, localCount -1)) evaluated: count.testAndSetOrdered(localCount, localCount -1) yes Evaluation Count:900 | yes Evaluation Count:24 |
| 24-900 |
59 | return; executed: return; Execution Count:899 | 899 |
60 | } else { executed: } Execution Count:24 | 24 |
61 | if (count.testAndSetOrdered(localCount, localCount + 1)) evaluated: count.testAndSetOrdered(localCount, localCount + 1) yes Evaluation Count:542479 | yes Evaluation Count:20300 |
| 20300-542479 |
62 | return; executed: return; Execution Count:542454 | 542454 |
63 | } executed: } Execution Count:20263 | 20263 |
64 | } | - |
65 | } | 0 |
66 | | - |
67 | int ThreadEngineBarrier::release() | - |
68 | { | - |
69 | forever { executed (the execution status of this line is deduced): for(;;) { | - |
70 | int localCount = count.load(); executed (the execution status of this line is deduced): int localCount = count.load(); | - |
71 | if (localCount == -1) { evaluated: localCount == -1 yes Evaluation Count:501 | yes Evaluation Count:558093 |
| 501-558093 |
72 | if (count.testAndSetOrdered(-1, 0)) { partially evaluated: count.testAndSetOrdered(-1, 0) yes Evaluation Count:501 | no Evaluation Count:0 |
| 0-501 |
73 | semaphore.release(); executed (the execution status of this line is deduced): semaphore.release(); | - |
74 | return 0; executed: return 0; Execution Count:501 | 501 |
75 | } | - |
76 | } else if (localCount < 0) { never executed: } evaluated: localCount < 0 yes Evaluation Count:2371 | yes Evaluation Count:556199 |
| 0-556199 |
77 | if (count.testAndSetOrdered(localCount, localCount + 1)) evaluated: count.testAndSetOrdered(localCount, localCount + 1) yes Evaluation Count:2349 | yes Evaluation Count:41 |
| 41-2349 |
78 | return qAbs(localCount + 1); executed: return qAbs(localCount + 1); Execution Count:2346 | 2346 |
79 | } else { executed: } Execution Count:41 | 41 |
80 | if (count.testAndSetOrdered(localCount, localCount - 1)) evaluated: count.testAndSetOrdered(localCount, localCount - 1) yes Evaluation Count:539740 | yes Evaluation Count:23585 |
| 23585-539740 |
81 | return localCount - 1; executed: return localCount - 1; Execution Count:539406 | 539406 |
82 | } executed: } Execution Count:23424 | 23424 |
83 | } | - |
84 | } | 0 |
85 | | - |
86 | // Wait until all threads have been released | - |
87 | void ThreadEngineBarrier::wait() | - |
88 | { | - |
89 | forever { executed (the execution status of this line is deduced): for(;;) { | - |
90 | int localCount = count.load(); executed (the execution status of this line is deduced): int localCount = count.load(); | - |
91 | if (localCount == 0) evaluated: localCount == 0 yes Evaluation Count:20051 | yes Evaluation Count:515 |
| 515-20051 |
92 | return; executed: return; Execution Count:20051 | 20051 |
93 | | - |
94 | Q_ASSERT(localCount > 0); // multiple waiters are not allowed. executed (the execution status of this line is deduced): qt_noop(); | - |
95 | if (count.testAndSetOrdered(localCount, -localCount)) { evaluated: count.testAndSetOrdered(localCount, -localCount) yes Evaluation Count:501 | yes Evaluation Count:14 |
| 14-501 |
96 | semaphore.acquire(); executed (the execution status of this line is deduced): semaphore.acquire(); | - |
97 | return; executed: return; Execution Count:501 | 501 |
98 | } | - |
99 | } executed: } Execution Count:14 | 14 |
100 | } | 0 |
101 | | - |
102 | int ThreadEngineBarrier::currentCount() | - |
103 | { | - |
104 | return count.load(); never executed: return count.load(); | 0 |
105 | } | - |
106 | | - |
107 | // releases a thread, unless this is the last thread. | - |
108 | // returns true if the thread was released. | - |
109 | bool ThreadEngineBarrier::releaseUnlessLast() | - |
110 | { | - |
111 | forever { executed (the execution status of this line is deduced): for(;;) { | - |
112 | int localCount = count.load(); executed (the execution status of this line is deduced): int localCount = count.load(); | - |
113 | if (qAbs(localCount) == 1) { evaluated: qAbs(localCount) == 1 yes Evaluation Count:1302 | yes Evaluation Count:814 |
| 814-1302 |
114 | return false; executed: return false; Execution Count:1302 | 1302 |
115 | } else if (localCount < 0) { evaluated: localCount < 0 yes Evaluation Count:369 | yes Evaluation Count:444 |
| 369-444 |
116 | if (count.testAndSetOrdered(localCount, localCount + 1)) evaluated: count.testAndSetOrdered(localCount, localCount + 1) yes Evaluation Count:366 | yes Evaluation Count:5 |
| 5-366 |
117 | return true; executed: return true; Execution Count:366 | 366 |
118 | } else { executed: } Execution Count:5 | 5 |
119 | if (count.testAndSetOrdered(localCount, localCount - 1)) evaluated: count.testAndSetOrdered(localCount, localCount - 1) yes Evaluation Count:439 | yes Evaluation Count:6 |
| 6-439 |
120 | return true; executed: return true; Execution Count:439 | 439 |
121 | } executed: } Execution Count:6 | 6 |
122 | } | - |
123 | } | 0 |
124 | | - |
125 | ThreadEngineBase::ThreadEngineBase() | - |
126 | :futureInterface(0), threadPool(QThreadPool::globalInstance()) | - |
127 | { | - |
128 | setAutoDelete(false); executed (the execution status of this line is deduced): setAutoDelete(false); | - |
129 | } executed: } Execution Count:81049 | 81049 |
130 | | - |
131 | ThreadEngineBase::~ThreadEngineBase() {} | - |
132 | | - |
133 | void ThreadEngineBase::startSingleThreaded() | - |
134 | { | - |
135 | start(); executed (the execution status of this line is deduced): start(); | - |
136 | while (threadFunction() != ThreadFinished) partially evaluated: threadFunction() != ThreadFinished no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
137 | ; | 0 |
138 | finish(); executed (the execution status of this line is deduced): finish(); | - |
139 | } executed: } Execution Count:1 | 1 |
140 | | - |
141 | void ThreadEngineBase::startBlocking() | - |
142 | { | - |
143 | start(); executed (the execution status of this line is deduced): start(); | - |
144 | barrier.acquire(); executed (the execution status of this line is deduced): barrier.acquire(); | - |
145 | startThreads(); executed (the execution status of this line is deduced): startThreads(); | - |
146 | | - |
147 | bool throttled = false; executed (the execution status of this line is deduced): bool throttled = false; | - |
148 | #ifndef QT_NO_EXCEPTIONS | - |
149 | try { | - |
150 | #endif | - |
151 | while (threadFunction() == ThrottleThread) { evaluated: threadFunction() == ThrottleThread yes Evaluation Count:11 | yes Evaluation Count:20537 |
| 11-20537 |
152 | if (threadThrottleExit()) { partially evaluated: threadThrottleExit() yes Evaluation Count:11 | no Evaluation Count:0 |
| 0-11 |
153 | throttled = true; executed (the execution status of this line is deduced): throttled = true; | - |
154 | break; executed: break; Execution Count:11 | 11 |
155 | } | - |
156 | } | 0 |
157 | #ifndef QT_NO_EXCEPTIONS | - |
158 | } catch (QException &e) { executed: } Execution Count:20548 | 20548 |
159 | handleException(e); executed (the execution status of this line is deduced): handleException(e); | - |
160 | } catch (...) { executed: } Execution Count:2 | 2 |
161 | handleException(QUnhandledException()); executed (the execution status of this line is deduced): handleException(QUnhandledException()); | - |
162 | } executed: } Execution Count:2 | 2 |
163 | #endif | - |
164 | | - |
165 | if (throttled == false) { evaluated: throttled == false yes Evaluation Count:20541 | yes Evaluation Count:11 |
| 11-20541 |
166 | barrier.release(); executed (the execution status of this line is deduced): barrier.release(); | - |
167 | } executed: } Execution Count:20541 | 20541 |
168 | | - |
169 | barrier.wait(); executed (the execution status of this line is deduced): barrier.wait(); | - |
170 | finish(); executed (the execution status of this line is deduced): finish(); | - |
171 | exceptionStore.throwPossibleException(); executed (the execution status of this line is deduced): exceptionStore.throwPossibleException(); | - |
172 | } executed: } Execution Count:20548 | 20548 |
173 | | - |
174 | void ThreadEngineBase::startThread() | - |
175 | { | - |
176 | startThreadInternal(); executed (the execution status of this line is deduced): startThreadInternal(); | - |
177 | } executed: } Execution Count:79218 | 79218 |
178 | | - |
179 | void ThreadEngineBase::acquireBarrierSemaphore() | - |
180 | { | - |
181 | barrier.acquire(); executed (the execution status of this line is deduced): barrier.acquire(); | - |
182 | } executed: } Execution Count:60497 | 60497 |
183 | | - |
184 | bool ThreadEngineBase::isCanceled() | - |
185 | { | - |
186 | if (futureInterface) evaluated: futureInterface yes Evaluation Count:775546 | yes Evaluation Count:145390 |
| 145390-775546 |
187 | return futureInterface->isCanceled(); executed: return futureInterface->isCanceled(); Execution Count:775307 | 775307 |
188 | else | - |
189 | return false; executed: return false; Execution Count:144887 | 144887 |
190 | } | - |
191 | | - |
192 | void ThreadEngineBase::waitForResume() | - |
193 | { | - |
194 | if (futureInterface) evaluated: futureInterface yes Evaluation Count:24799 | yes Evaluation Count:57473 |
| 24799-57473 |
195 | futureInterface->waitForResume(); executed: futureInterface->waitForResume(); Execution Count:24663 | 24663 |
196 | } executed: } Execution Count:81718 | 81718 |
197 | | - |
198 | bool ThreadEngineBase::isProgressReportingEnabled() | - |
199 | { | - |
200 | // If we don't have a QFuture, there is no-one to report the progress to. | - |
201 | return (futureInterface != 0); executed: return (futureInterface != 0); Execution Count:20910 | 20910 |
202 | } | - |
203 | | - |
204 | void ThreadEngineBase::setProgressValue(int progress) | - |
205 | { | - |
206 | if (futureInterface) partially evaluated: futureInterface yes Evaluation Count:24394 | no Evaluation Count:0 |
| 0-24394 |
207 | futureInterface->setProgressValue(progress); executed: futureInterface->setProgressValue(progress); Execution Count:24346 | 24346 |
208 | } executed: } Execution Count:24428 | 24428 |
209 | | - |
210 | void ThreadEngineBase::setProgressRange(int minimum, int maximum) | - |
211 | { | - |
212 | if (futureInterface) partially evaluated: futureInterface yes Evaluation Count:255 | no Evaluation Count:0 |
| 0-255 |
213 | futureInterface->setProgressRange(minimum, maximum); executed: futureInterface->setProgressRange(minimum, maximum); Execution Count:255 | 255 |
214 | } executed: } Execution Count:255 | 255 |
215 | | - |
216 | bool ThreadEngineBase::startThreadInternal() | - |
217 | { | - |
218 | if (this->isCanceled()) partially evaluated: this->isCanceled() no Evaluation Count:0 | yes Evaluation Count:459381 |
| 0-459381 |
219 | return false; never executed: return false; | 0 |
220 | | - |
221 | barrier.acquire(); executed (the execution status of this line is deduced): barrier.acquire(); | - |
222 | if (!threadPool->tryStart(this)) { evaluated: !threadPool->tryStart(this) yes Evaluation Count:163145 | yes Evaluation Count:298530 |
| 163145-298530 |
223 | barrier.release(); executed (the execution status of this line is deduced): barrier.release(); | - |
224 | return false; executed: return false; Execution Count:163827 | 163827 |
225 | } | - |
226 | return true; executed: return true; Execution Count:298538 | 298538 |
227 | } | - |
228 | | - |
229 | void ThreadEngineBase::startThreads() | - |
230 | { | - |
231 | while (shouldStartThread() && startThreadInternal()) evaluated: shouldStartThread() yes Evaluation Count:382147 | yes Evaluation Count:294079 |
evaluated: startThreadInternal() yes Evaluation Count:298467 | yes Evaluation Count:84591 |
| 84591-382147 |
232 | ; executed: ; Execution Count:298468 | 298468 |
233 | } executed: } Execution Count:378412 | 378412 |
234 | | - |
235 | void ThreadEngineBase::threadExit() | - |
236 | { | - |
237 | const bool asynchronous = futureInterface != 0; executed (the execution status of this line is deduced): const bool asynchronous = futureInterface != 0; | - |
238 | const int lastThread = (barrier.release() == 0); executed (the execution status of this line is deduced): const int lastThread = (barrier.release() == 0); | - |
239 | | - |
240 | if (lastThread && asynchronous) evaluated: lastThread yes Evaluation Count:60977 | yes Evaluation Count:296913 |
evaluated: asynchronous yes Evaluation Count:60474 | yes Evaluation Count:503 |
| 503-296913 |
241 | this->asynchronousFinish(); executed: this->asynchronousFinish(); Execution Count:60467 | 60467 |
242 | } executed: } Execution Count:357891 | 357891 |
243 | | - |
244 | // Called by a worker thread that wants to be throttled. If the current number | - |
245 | // of running threads is larger than one the thread is allowed to exit and | - |
246 | // this function returns one. | - |
247 | bool ThreadEngineBase::threadThrottleExit() | - |
248 | { | - |
249 | return barrier.releaseUnlessLast(); executed: return barrier.releaseUnlessLast(); Execution Count:2106 | 2106 |
250 | } | - |
251 | | - |
252 | void ThreadEngineBase::run() // implements QRunnable. | - |
253 | { | - |
254 | if (this->isCanceled()) { evaluated: this->isCanceled() yes Evaluation Count:92 | yes Evaluation Count:358016 |
| 92-358016 |
255 | threadExit(); executed (the execution status of this line is deduced): threadExit(); | - |
256 | return; executed: return; Execution Count:94 | 94 |
257 | } | - |
258 | | - |
259 | startThreads(); executed (the execution status of this line is deduced): startThreads(); | - |
260 | | - |
261 | #ifndef QT_NO_EXCEPTIONS | - |
262 | try { | - |
263 | #endif | - |
264 | while (threadFunction() == ThrottleThread) { evaluated: threadFunction() == ThrottleThread yes Evaluation Count:2094 | yes Evaluation Count:356466 |
| 2094-356466 |
265 | // threadFunction returning ThrottleThread means it that the user | - |
266 | // struct wants to be throttled by making a worker thread exit. | - |
267 | // Respect that request unless this is the only worker thread left | - |
268 | // running, in which case it has to keep going. | - |
269 | if (threadThrottleExit()) evaluated: threadThrottleExit() yes Evaluation Count:794 | yes Evaluation Count:1302 |
| 794-1302 |
270 | return; executed: return; Execution Count:794 | 794 |
271 | } executed: } Execution Count:1302 | 1302 |
272 | | - |
273 | #ifndef QT_NO_EXCEPTIONS | - |
274 | } catch (QException &e) { executed: } Execution Count:356414 | 356414 |
275 | handleException(e); executed (the execution status of this line is deduced): handleException(e); | - |
276 | } catch (...) { executed: } Execution Count:20 | 20 |
277 | handleException(QUnhandledException()); executed (the execution status of this line is deduced): handleException(QUnhandledException()); | - |
278 | } executed: } Execution Count:24 | 24 |
279 | #endif | - |
280 | threadExit(); executed (the execution status of this line is deduced): threadExit(); | - |
281 | } executed: } Execution Count:357814 | 357814 |
282 | | - |
283 | #ifndef QT_NO_EXCEPTIONS | - |
284 | | - |
285 | void ThreadEngineBase::handleException(const QException &exception) | - |
286 | { | - |
287 | if (futureInterface) evaluated: futureInterface yes Evaluation Count:12 | yes Evaluation Count:36 |
| 12-36 |
288 | futureInterface->reportException(exception); executed: futureInterface->reportException(exception); Execution Count:12 | 12 |
289 | else | - |
290 | exceptionStore.setException(exception); executed: exceptionStore.setException(exception); Execution Count:36 | 36 |
291 | } | - |
292 | #endif | - |
293 | | - |
294 | | - |
295 | } // namepsace QtConcurrent | - |
296 | | - |
297 | QT_END_NAMESPACE | - |
298 | | - |
299 | #endif // QT_NO_CONCURRENT | - |
300 | | - |
| | |