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 "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:81048 | 81048 |
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:614 | yes Evaluation Count:543775 |
| 614-543775 |
58 | if (count.testAndSetOrdered(localCount, localCount -1)) evaluated: count.testAndSetOrdered(localCount, localCount -1) yes Evaluation Count:608 | yes Evaluation Count:15 |
| 15-608 |
59 | return; executed: return; Execution Count:608 | 608 |
60 | } else { executed: } Execution Count:15 | 15 |
61 | if (count.testAndSetOrdered(localCount, localCount + 1)) evaluated: count.testAndSetOrdered(localCount, localCount + 1) yes Evaluation Count:531495 | yes Evaluation Count:17668 |
| 17668-531495 |
62 | return; executed: return; Execution Count:531450 | 531450 |
63 | } executed: } Execution Count:17602 | 17602 |
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:504 | yes Evaluation Count:544363 |
| 504-544363 |
72 | if (count.testAndSetOrdered(-1, 0)) { partially evaluated: count.testAndSetOrdered(-1, 0) yes Evaluation Count:504 | no Evaluation Count:0 |
| 0-504 |
73 | semaphore.release(); executed (the execution status of this line is deduced): semaphore.release(); | - |
74 | return 0; executed: return 0; Execution Count:504 | 504 |
75 | } | - |
76 | } else if (localCount < 0) { never executed: } evaluated: localCount < 0 yes Evaluation Count:2151 | yes Evaluation Count:542550 |
| 0-542550 |
77 | if (count.testAndSetOrdered(localCount, localCount + 1)) evaluated: count.testAndSetOrdered(localCount, localCount + 1) yes Evaluation Count:2126 | yes Evaluation Count:46 |
| 46-2126 |
78 | return qAbs(localCount + 1); executed: return qAbs(localCount + 1); Execution Count:2126 | 2126 |
79 | } else { executed: } Execution Count:46 | 46 |
80 | if (count.testAndSetOrdered(localCount, localCount - 1)) evaluated: count.testAndSetOrdered(localCount, localCount - 1) yes Evaluation Count:528894 | yes Evaluation Count:20014 |
| 20014-528894 |
81 | return localCount - 1; executed: return localCount - 1; Execution Count:528399 | 528399 |
82 | } executed: } Execution Count:19893 | 19893 |
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:20047 | yes Evaluation Count:520 |
| 520-20047 |
92 | return; executed: return; Execution Count:20047 | 20047 |
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:504 | yes Evaluation Count:16 |
| 16-504 |
96 | semaphore.acquire(); executed (the execution status of this line is deduced): semaphore.acquire(); | - |
97 | return; executed: return; Execution Count:504 | 504 |
98 | } | - |
99 | } executed: } Execution Count:16 | 16 |
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:1532 | yes Evaluation Count:576 |
| 576-1532 |
114 | return false; executed: return false; Execution Count:1532 | 1532 |
115 | } else if (localCount < 0) { evaluated: localCount < 0 yes Evaluation Count:236 | yes Evaluation Count:340 |
| 236-340 |
116 | if (count.testAndSetOrdered(localCount, localCount + 1)) evaluated: count.testAndSetOrdered(localCount, localCount + 1) yes Evaluation Count:236 | yes Evaluation Count:1 |
| 1-236 |
117 | return true; executed: return true; Execution Count:236 | 236 |
118 | } else { executed: } Execution Count:1 | 1 |
119 | if (count.testAndSetOrdered(localCount, localCount - 1)) evaluated: count.testAndSetOrdered(localCount, localCount - 1) yes Evaluation Count:337 | yes Evaluation Count:4 |
| 4-337 |
120 | return true; executed: return true; Execution Count:337 | 337 |
121 | } executed: } Execution Count:4 | 4 |
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:81048 | 81048 |
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:20536 |
| 11-20536 |
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:20547 | 20547 |
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:20540 | yes Evaluation Count:11 |
| 11-20540 |
166 | barrier.release(); executed (the execution status of this line is deduced): barrier.release(); | - |
167 | } executed: } Execution Count:20540 | 20540 |
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:20547 | 20547 |
173 | | - |
174 | void ThreadEngineBase::startThread() | - |
175 | { | - |
176 | startThreadInternal(); executed (the execution status of this line is deduced): startThreadInternal(); | - |
177 | } executed: } Execution Count:77754 | 77754 |
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:754339 | yes Evaluation Count:143551 |
| 143551-754339 |
187 | return futureInterface->isCanceled(); executed: return futureInterface->isCanceled(); Execution Count:753904 | 753904 |
188 | else | - |
189 | return false; executed: return false; Execution Count:143040 | 143040 |
190 | } | - |
191 | | - |
192 | void ThreadEngineBase::waitForResume() | - |
193 | { | - |
194 | if (futureInterface) evaluated: futureInterface yes Evaluation Count:24235 | yes Evaluation Count:56416 |
| 24235-56416 |
195 | futureInterface->waitForResume(); executed: futureInterface->waitForResume(); Execution Count:24081 | 24081 |
196 | } executed: } Execution Count:80058 | 80058 |
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:23851 | no Evaluation Count:0 |
| 0-23851 |
207 | futureInterface->setProgressValue(progress); executed: futureInterface->setProgressValue(progress); Execution Count:23764 | 23764 |
208 | } executed: } Execution Count:23855 | 23855 |
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:448054 |
| 0-448054 |
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:161899 | yes Evaluation Count:288213 |
| 161899-288213 |
223 | barrier.release(); executed (the execution status of this line is deduced): barrier.release(); | - |
224 | return false; executed: return false; Execution Count:162843 | 162843 |
225 | } | - |
226 | return true; executed: return true; Execution Count:288214 | 288214 |
227 | } | - |
228 | | - |
229 | void ThreadEngineBase::startThreads() | - |
230 | { | - |
231 | while (shouldStartThread() && startThreadInternal()) evaluated: shouldStartThread() yes Evaluation Count:372276 | yes Evaluation Count:283307 |
evaluated: startThreadInternal() yes Evaluation Count:288139 | yes Evaluation Count:85040 |
| 85040-372276 |
232 | ; executed: ; Execution Count:288139 | 288139 |
233 | } executed: } Execution Count:368108 | 368108 |
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:60982 | yes Evaluation Count:286776 |
evaluated: asynchronous yes Evaluation Count:60479 | yes Evaluation Count:505 |
| 505-286776 |
241 | this->asynchronousFinish(); executed: this->asynchronousFinish(); Execution Count:60473 | 60473 |
242 | } executed: } Execution Count:347751 | 347751 |
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:2104 | 2104 |
250 | } | - |
251 | | - |
252 | void ThreadEngineBase::run() // implements QRunnable. | - |
253 | { | - |
254 | if (this->isCanceled()) { evaluated: this->isCanceled() yes Evaluation Count:96 | yes Evaluation Count:347773 |
| 96-347773 |
255 | threadExit(); executed (the execution status of this line is deduced): threadExit(); | - |
256 | return; executed: return; Execution Count:96 | 96 |
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:346399 |
| 2094-346399 |
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:561 | yes Evaluation Count:1532 |
| 561-1532 |
270 | return; executed: return; Execution Count:560 | 560 |
271 | } executed: } Execution Count:1532 | 1532 |
272 | | - |
273 | #ifndef QT_NO_EXCEPTIONS | - |
274 | } catch (QException &e) { executed: } Execution Count:346385 | 346385 |
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:347662 | 347662 |
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:35 |
| 12-35 |
288 | futureInterface->reportException(exception); executed: futureInterface->reportException(exception); Execution Count:12 | 12 |
289 | else | - |
290 | exceptionStore.setException(exception); executed: exceptionStore.setException(exception); Execution Count:35 | 35 |
291 | } | - |
292 | #endif | - |
293 | | - |
294 | | - |
295 | } // namepsace QtConcurrent | - |
296 | | - |
297 | QT_END_NAMESPACE | - |
298 | | - |
299 | #endif // QT_NO_CONCURRENT | - |
300 | | - |
| | |