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