Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qfutureinterface.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 | // qfutureinterface.h included from qfuture.h | - | ||||||||||||
35 | #include "qfuture.h" | - | ||||||||||||
36 | - | |||||||||||||
37 | #ifndef QT_NO_QFUTURE | - | ||||||||||||
38 | - | |||||||||||||
39 | #include "qfutureinterface_p.h" | - | ||||||||||||
40 | - | |||||||||||||
41 | #include <QtCore/qatomic.h> | - | ||||||||||||
42 | #include <QtCore/qthread.h> | - | ||||||||||||
43 | #include <private/qthreadpool_p.h> | - | ||||||||||||
44 | - | |||||||||||||
45 | QT_BEGIN_NAMESPACE | - | ||||||||||||
46 | - | |||||||||||||
47 | enum { | - | ||||||||||||
48 | MaxProgressEmitsPerSecond = 25 | - | ||||||||||||
49 | }; | - | ||||||||||||
50 | - | |||||||||||||
51 | namespace { | - | ||||||||||||
52 | class ThreadPoolThreadReleaser { | - | ||||||||||||
53 | QThreadPool *m_pool; | - | ||||||||||||
54 | public: | - | ||||||||||||
55 | explicit ThreadPoolThreadReleaser(QThreadPool *pool) | - | ||||||||||||
56 | : m_pool(pool) | - | ||||||||||||
57 | { if (pool) pool->releaseThread(); } never executed: end of block never executed: pool->releaseThread();
| 0 | ||||||||||||
58 | ~ThreadPoolThreadReleaser() | - | ||||||||||||
59 | { if (m_pool) m_pool->reserveThread(); } never executed: end of block never executed: m_pool->reserveThread();
| 0 | ||||||||||||
60 | }; | - | ||||||||||||
61 | } // unnamed namespace | - | ||||||||||||
62 | - | |||||||||||||
63 | - | |||||||||||||
64 | QFutureInterfaceBase::QFutureInterfaceBase(State initialState) | - | ||||||||||||
65 | : d(new QFutureInterfaceBasePrivate(initialState)) | - | ||||||||||||
66 | { executed 128673 times by 12 tests: }end of block Executed by:
executed 128673 times by 12 tests: end of block Executed by:
| 128673 | ||||||||||||
67 | - | |||||||||||||
68 | QFutureInterfaceBase::QFutureInterfaceBase(const QFutureInterfaceBase &other) | - | ||||||||||||
69 | : d(other.d) | - | ||||||||||||
70 | { | - | ||||||||||||
71 | d->refCount.ref(); | - | ||||||||||||
72 | } executed 160391 times by 12 tests: end of block Executed by:
| 160391 | ||||||||||||
73 | - | |||||||||||||
74 | QFutureInterfaceBase::~QFutureInterfaceBase() | - | ||||||||||||
75 | { | - | ||||||||||||
76 | if (!d->refCount.deref())
| 128599-160465 | ||||||||||||
77 | delete d; executed 128599 times by 13 tests: delete d; Executed by:
| 128599 | ||||||||||||
78 | } executed 289064 times by 13 tests: end of block Executed by:
| 289064 | ||||||||||||
79 | - | |||||||||||||
80 | static inline int switch_on(QAtomicInt &a, int which) | - | ||||||||||||
81 | { | - | ||||||||||||
82 | return a.fetchAndOrRelaxed(which) | which; executed 38 times by 5 tests: return a.fetchAndOrRelaxed(which) | which; Executed by:
| 38 | ||||||||||||
83 | } | - | ||||||||||||
84 | - | |||||||||||||
85 | static inline int switch_off(QAtomicInt &a, int which) | - | ||||||||||||
86 | { | - | ||||||||||||
87 | return a.fetchAndAndRelaxed(~which) & ~which; executed 26 times by 2 tests: return a.fetchAndAndRelaxed(~which) & ~which; Executed by:
| 26 | ||||||||||||
88 | } | - | ||||||||||||
89 | - | |||||||||||||
90 | static inline int switch_from_to(QAtomicInt &a, int from, int to) | - | ||||||||||||
91 | { | - | ||||||||||||
92 | int newValue; | - | ||||||||||||
93 | int expected = a.load(); | - | ||||||||||||
94 | do { | - | ||||||||||||
95 | newValue = (expected & ~from) | to; | - | ||||||||||||
96 | } while (!a.testAndSetRelaxed(expected, newValue, expected)); executed 129423 times by 11 tests: end of block Executed by:
| 0-129423 | ||||||||||||
97 | return newValue; executed 129423 times by 11 tests: return newValue; Executed by:
| 129423 | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | void QFutureInterfaceBase::cancel() | - | ||||||||||||
101 | { | - | ||||||||||||
102 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
103 | if (d->state.load() & Canceled)
| 5-1112 | ||||||||||||
104 | return; executed 5 times by 1 test: return; Executed by:
| 5 | ||||||||||||
105 | - | |||||||||||||
106 | switch_from_to(d->state, Paused, Canceled); | - | ||||||||||||
107 | d->waitCondition.wakeAll(); | - | ||||||||||||
108 | d->pausedWaitCondition.wakeAll(); | - | ||||||||||||
109 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Canceled)); | - | ||||||||||||
110 | } executed 1112 times by 4 tests: end of block Executed by:
| 1112 | ||||||||||||
111 | - | |||||||||||||
112 | void QFutureInterfaceBase::setPaused(bool paused) | - | ||||||||||||
113 | { | - | ||||||||||||
114 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
115 | if (paused) {
| 9 | ||||||||||||
116 | switch_on(d->state, Paused); | - | ||||||||||||
117 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Paused)); | - | ||||||||||||
118 | } else { executed 9 times by 2 tests: end of block Executed by:
| 9 | ||||||||||||
119 | switch_off(d->state, Paused); | - | ||||||||||||
120 | d->pausedWaitCondition.wakeAll(); | - | ||||||||||||
121 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Resumed)); | - | ||||||||||||
122 | } executed 9 times by 2 tests: end of block Executed by:
| 9 | ||||||||||||
123 | } | - | ||||||||||||
124 | - | |||||||||||||
125 | void QFutureInterfaceBase::togglePaused() | - | ||||||||||||
126 | { | - | ||||||||||||
127 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
128 | if (d->state.load() & Paused) {
| 1-7 | ||||||||||||
129 | switch_off(d->state, Paused); | - | ||||||||||||
130 | d->pausedWaitCondition.wakeAll(); | - | ||||||||||||
131 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Resumed)); | - | ||||||||||||
132 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
133 | switch_on(d->state, Paused); | - | ||||||||||||
134 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Paused)); | - | ||||||||||||
135 | } executed 7 times by 2 tests: end of block Executed by:
| 7 | ||||||||||||
136 | } | - | ||||||||||||
137 | - | |||||||||||||
138 | void QFutureInterfaceBase::setThrottled(bool enable) | - | ||||||||||||
139 | { | - | ||||||||||||
140 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
141 | if (enable) {
| 2-16 | ||||||||||||
142 | switch_on(d->state, Throttled); | - | ||||||||||||
143 | } else { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
144 | switch_off(d->state, Throttled); | - | ||||||||||||
145 | if (!(d->state.load() & Paused))
| 0-16 | ||||||||||||
146 | d->pausedWaitCondition.wakeAll(); executed 16 times by 2 tests: d->pausedWaitCondition.wakeAll(); Executed by:
| 16 | ||||||||||||
147 | } executed 16 times by 2 tests: end of block Executed by:
| 16 | ||||||||||||
148 | } | - | ||||||||||||
149 | - | |||||||||||||
150 | - | |||||||||||||
151 | bool QFutureInterfaceBase::isRunning() const | - | ||||||||||||
152 | { | - | ||||||||||||
153 | return queryState(Running); executed 323859 times by 12 tests: return queryState(Running); Executed by:
| 323859 | ||||||||||||
154 | } | - | ||||||||||||
155 | - | |||||||||||||
156 | bool QFutureInterfaceBase::isStarted() const | - | ||||||||||||
157 | { | - | ||||||||||||
158 | return queryState(Started); executed 28 times by 2 tests: return queryState(Started); Executed by:
| 28 | ||||||||||||
159 | } | - | ||||||||||||
160 | - | |||||||||||||
161 | bool QFutureInterfaceBase::isCanceled() const | - | ||||||||||||
162 | { | - | ||||||||||||
163 | return queryState(Canceled); executed 202230 times by 11 tests: return queryState(Canceled); Executed by:
| 202230 | ||||||||||||
164 | } | - | ||||||||||||
165 | - | |||||||||||||
166 | bool QFutureInterfaceBase::isFinished() const | - | ||||||||||||
167 | { | - | ||||||||||||
168 | return queryState(Finished); executed 128826 times by 12 tests: return queryState(Finished); Executed by:
| 128826 | ||||||||||||
169 | } | - | ||||||||||||
170 | - | |||||||||||||
171 | bool QFutureInterfaceBase::isPaused() const | - | ||||||||||||
172 | { | - | ||||||||||||
173 | return queryState(Paused); executed 13519 times by 6 tests: return queryState(Paused); Executed by:
| 13519 | ||||||||||||
174 | } | - | ||||||||||||
175 | - | |||||||||||||
176 | bool QFutureInterfaceBase::isThrottled() const | - | ||||||||||||
177 | { | - | ||||||||||||
178 | return queryState(Throttled); executed 7 times by 2 tests: return queryState(Throttled); Executed by:
| 7 | ||||||||||||
179 | } | - | ||||||||||||
180 | - | |||||||||||||
181 | bool QFutureInterfaceBase::isResultReadyAt(int index) const | - | ||||||||||||
182 | { | - | ||||||||||||
183 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
184 | return d->internal_isResultReadyAt(index); executed 15004 times by 2 tests: return d->internal_isResultReadyAt(index); Executed by:
| 15004 | ||||||||||||
185 | } | - | ||||||||||||
186 | - | |||||||||||||
187 | bool QFutureInterfaceBase::waitForNextResult() | - | ||||||||||||
188 | { | - | ||||||||||||
189 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
190 | return d->internal_waitForNextResult(); never executed: return d->internal_waitForNextResult(); | 0 | ||||||||||||
191 | } | - | ||||||||||||
192 | - | |||||||||||||
193 | void QFutureInterfaceBase::waitForResume() | - | ||||||||||||
194 | { | - | ||||||||||||
195 | // return early if possible to avoid taking the mutex lock. | - | ||||||||||||
196 | { | - | ||||||||||||
197 | const int state = d->state.load(); | - | ||||||||||||
198 | if (!(state & Paused) || (state & Canceled))
| 0-6309 | ||||||||||||
199 | return; executed 6309 times by 4 tests: return; Executed by:
| 6309 | ||||||||||||
200 | } | - | ||||||||||||
201 | - | |||||||||||||
202 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
203 | const int state = d->state.load(); | - | ||||||||||||
204 | if (!(state & Paused) || (state & Canceled))
| 0 | ||||||||||||
205 | return; never executed: return; | 0 | ||||||||||||
206 | - | |||||||||||||
207 | // decrease active thread count since this thread will wait. | - | ||||||||||||
208 | const ThreadPoolThreadReleaser releaser(d->pool()); | - | ||||||||||||
209 | - | |||||||||||||
210 | d->pausedWaitCondition.wait(&d->m_mutex); | - | ||||||||||||
211 | } never executed: end of block | 0 | ||||||||||||
212 | - | |||||||||||||
213 | int QFutureInterfaceBase::progressValue() const | - | ||||||||||||
214 | { | - | ||||||||||||
215 | const QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
216 | return d->m_progressValue; executed 17 times by 2 tests: return d->m_progressValue; Executed by:
| 17 | ||||||||||||
217 | } | - | ||||||||||||
218 | - | |||||||||||||
219 | int QFutureInterfaceBase::progressMinimum() const | - | ||||||||||||
220 | { | - | ||||||||||||
221 | const QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
222 | return d->m_progressMinimum; executed 12 times by 1 test: return d->m_progressMinimum; Executed by:
| 12 | ||||||||||||
223 | } | - | ||||||||||||
224 | - | |||||||||||||
225 | int QFutureInterfaceBase::progressMaximum() const | - | ||||||||||||
226 | { | - | ||||||||||||
227 | const QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
228 | return d->m_progressMaximum; executed 12 times by 1 test: return d->m_progressMaximum; Executed by:
| 12 | ||||||||||||
229 | } | - | ||||||||||||
230 | - | |||||||||||||
231 | int QFutureInterfaceBase::resultCount() const | - | ||||||||||||
232 | { | - | ||||||||||||
233 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
234 | return d->internal_resultCount(); executed 28909 times by 3 tests: return d->internal_resultCount(); Executed by:
| 28909 | ||||||||||||
235 | } | - | ||||||||||||
236 | - | |||||||||||||
237 | QString QFutureInterfaceBase::progressText() const | - | ||||||||||||
238 | { | - | ||||||||||||
239 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
240 | return d->m_progressText; executed 15 times by 2 tests: return d->m_progressText; Executed by:
| 15 | ||||||||||||
241 | } | - | ||||||||||||
242 | - | |||||||||||||
243 | bool QFutureInterfaceBase::isProgressUpdateNeeded() const | - | ||||||||||||
244 | { | - | ||||||||||||
245 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
246 | return !d->progressTime.isValid() || (d->progressTime.elapsed() > (1000 / MaxProgressEmitsPerSecond)); executed 104 times by 1 test: return !d->progressTime.isValid() || (d->progressTime.elapsed() > (1000 / MaxProgressEmitsPerSecond)); Executed by:
| 0-104 | ||||||||||||
247 | } | - | ||||||||||||
248 | - | |||||||||||||
249 | void QFutureInterfaceBase::reportStarted() | - | ||||||||||||
250 | { | - | ||||||||||||
251 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
252 | if (d->state.load() & (Started|Canceled|Finished))
| 0-128312 | ||||||||||||
253 | return; never executed: return; | 0 | ||||||||||||
254 | - | |||||||||||||
255 | d->setState(State(Started | Running)); | - | ||||||||||||
256 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Started)); | - | ||||||||||||
257 | } executed 128312 times by 11 tests: end of block Executed by:
| 128312 | ||||||||||||
258 | - | |||||||||||||
259 | void QFutureInterfaceBase::reportCanceled() | - | ||||||||||||
260 | { | - | ||||||||||||
261 | cancel(); | - | ||||||||||||
262 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
263 | - | |||||||||||||
264 | #ifndef QT_NO_EXCEPTIONS | - | ||||||||||||
265 | void QFutureInterfaceBase::reportException(const QException &exception) | - | ||||||||||||
266 | { | - | ||||||||||||
267 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
268 | if (d->state.load() & (Canceled|Finished))
| 0-17 | ||||||||||||
269 | return; never executed: return; | 0 | ||||||||||||
270 | - | |||||||||||||
271 | d->m_exceptionStore.setException(exception); | - | ||||||||||||
272 | switch_on(d->state, Canceled); | - | ||||||||||||
273 | d->waitCondition.wakeAll(); | - | ||||||||||||
274 | d->pausedWaitCondition.wakeAll(); | - | ||||||||||||
275 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Canceled)); | - | ||||||||||||
276 | } executed 17 times by 4 tests: end of block Executed by:
| 17 | ||||||||||||
277 | #endif | - | ||||||||||||
278 | - | |||||||||||||
279 | void QFutureInterfaceBase::reportFinished() | - | ||||||||||||
280 | { | - | ||||||||||||
281 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
282 | if (!isFinished()) {
| 0-128311 | ||||||||||||
283 | switch_from_to(d->state, Running, Finished); | - | ||||||||||||
284 | d->waitCondition.wakeAll(); | - | ||||||||||||
285 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Finished)); | - | ||||||||||||
286 | } executed 128311 times by 11 tests: end of block Executed by:
| 128311 | ||||||||||||
287 | } executed 128311 times by 11 tests: end of block Executed by:
| 128311 | ||||||||||||
288 | - | |||||||||||||
289 | void QFutureInterfaceBase::setExpectedResultCount(int resultCount) | - | ||||||||||||
290 | { | - | ||||||||||||
291 | if (d->manualProgress == false)
| 0 | ||||||||||||
292 | setProgressRange(0, resultCount); never executed: setProgressRange(0, resultCount); | 0 | ||||||||||||
293 | d->m_expectedResultCount = resultCount; | - | ||||||||||||
294 | } never executed: end of block | 0 | ||||||||||||
295 | - | |||||||||||||
296 | int QFutureInterfaceBase::expectedResultCount() | - | ||||||||||||
297 | { | - | ||||||||||||
298 | return d->m_expectedResultCount; never executed: return d->m_expectedResultCount; | 0 | ||||||||||||
299 | } | - | ||||||||||||
300 | - | |||||||||||||
301 | bool QFutureInterfaceBase::queryState(State state) const | - | ||||||||||||
302 | { | - | ||||||||||||
303 | return d->state.load() & state; executed 738262 times by 12 tests: return d->state.load() & state; Executed by:
| 738262 | ||||||||||||
304 | } | - | ||||||||||||
305 | - | |||||||||||||
306 | void QFutureInterfaceBase::waitForResult(int resultIndex) | - | ||||||||||||
307 | { | - | ||||||||||||
308 | d->m_exceptionStore.throwPossibleException(); | - | ||||||||||||
309 | - | |||||||||||||
310 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
311 | if (!isRunning())
| 15527-60999 | ||||||||||||
312 | return; executed 15527 times by 7 tests: return; Executed by:
| 15527 | ||||||||||||
313 | lock.unlock(); | - | ||||||||||||
314 | - | |||||||||||||
315 | // To avoid deadlocks and reduce the number of threads used, try to | - | ||||||||||||
316 | // run the runnable in the current thread. | - | ||||||||||||
317 | d->pool()->d_func()->stealAndRunRunnable(d->runnable); | - | ||||||||||||
318 | - | |||||||||||||
319 | lock.relock(); | - | ||||||||||||
320 | - | |||||||||||||
321 | const int waitIndex = (resultIndex == -1) ? INT_MAX : resultIndex;
| 86-60913 | ||||||||||||
322 | while (isRunning() && !d->internal_isResultReadyAt(waitIndex))
| 463-32891 | ||||||||||||
323 | d->waitCondition.wait(&d->m_mutex); executed 463 times by 6 tests: d->waitCondition.wait(&d->m_mutex); Executed by:
| 463 | ||||||||||||
324 | - | |||||||||||||
325 | d->m_exceptionStore.throwPossibleException(); | - | ||||||||||||
326 | } executed 60997 times by 7 tests: end of block Executed by:
| 60997 | ||||||||||||
327 | - | |||||||||||||
328 | void QFutureInterfaceBase::waitForFinished() | - | ||||||||||||
329 | { | - | ||||||||||||
330 | QMutexLocker lock(&d->m_mutex); | - | ||||||||||||
331 | const bool alreadyFinished = !isRunning(); | - | ||||||||||||
332 | lock.unlock(); | - | ||||||||||||
333 | - | |||||||||||||
334 | if (!alreadyFinished) {
| 3976-71671 | ||||||||||||
335 | d->pool()->d_func()->stealAndRunRunnable(d->runnable); | - | ||||||||||||
336 | - | |||||||||||||
337 | lock.relock(); | - | ||||||||||||
338 | - | |||||||||||||
339 | while (isRunning())
| 38495-71671 | ||||||||||||
340 | d->waitCondition.wait(&d->m_mutex); executed 38495 times by 9 tests: d->waitCondition.wait(&d->m_mutex); Executed by:
| 38495 | ||||||||||||
341 | } executed 71671 times by 9 tests: end of block Executed by:
| 71671 | ||||||||||||
342 | - | |||||||||||||
343 | d->m_exceptionStore.throwPossibleException(); | - | ||||||||||||
344 | } executed 75636 times by 11 tests: end of block Executed by:
| 75636 | ||||||||||||
345 | - | |||||||||||||
346 | void QFutureInterfaceBase::reportResultsReady(int beginIndex, int endIndex) | - | ||||||||||||
347 | { | - | ||||||||||||
348 | if (beginIndex == endIndex || (d->state.load() & (Canceled|Finished)))
| 0-34826 | ||||||||||||
349 | return; executed 53 times by 2 tests: return; Executed by:
| 53 | ||||||||||||
350 | - | |||||||||||||
351 | d->waitCondition.wakeAll(); | - | ||||||||||||
352 | - | |||||||||||||
353 | if (d->manualProgress == false) {
| 712-34114 | ||||||||||||
354 | if (d->internal_updateProgress(d->m_progressValue + endIndex - beginIndex) == false) {
| 1119-32995 | ||||||||||||
355 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::ResultsReady, | - | ||||||||||||
356 | beginIndex, | - | ||||||||||||
357 | endIndex)); | - | ||||||||||||
358 | return; executed 1119 times by 5 tests: return; Executed by:
| 1119 | ||||||||||||
359 | } | - | ||||||||||||
360 | - | |||||||||||||
361 | d->sendCallOuts(QFutureCallOutEvent(QFutureCallOutEvent::Progress, | - | ||||||||||||
362 | d->m_progressValue, | - | ||||||||||||
363 | d->m_progressText), | - | ||||||||||||
364 | QFutureCallOutEvent(QFutureCallOutEvent::ResultsReady, | - | ||||||||||||
365 | beginIndex, | - | ||||||||||||
366 | endIndex)); | - | ||||||||||||
367 | return; executed 32995 times by 8 tests: return; Executed by:
| 32995 | ||||||||||||
368 | } | - | ||||||||||||
369 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::ResultsReady, beginIndex, endIndex)); | - | ||||||||||||
370 | } executed 712 times by 4 tests: end of block Executed by:
| 712 | ||||||||||||
371 | - | |||||||||||||
372 | void QFutureInterfaceBase::setRunnable(QRunnable *runnable) | - | ||||||||||||
373 | { | - | ||||||||||||
374 | d->runnable = runnable; | - | ||||||||||||
375 | } executed 67761 times by 7 tests: end of block Executed by:
| 67761 | ||||||||||||
376 | - | |||||||||||||
377 | void QFutureInterfaceBase::setThreadPool(QThreadPool *pool) | - | ||||||||||||
378 | { | - | ||||||||||||
379 | d->m_pool = pool; | - | ||||||||||||
380 | } executed 67761 times by 7 tests: end of block Executed by:
| 67761 | ||||||||||||
381 | - | |||||||||||||
382 | void QFutureInterfaceBase::setFilterMode(bool enable) | - | ||||||||||||
383 | { | - | ||||||||||||
384 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
385 | resultStoreBase().setFilterMode(enable); | - | ||||||||||||
386 | } executed 34 times by 2 tests: end of block Executed by:
| 34 | ||||||||||||
387 | - | |||||||||||||
388 | void QFutureInterfaceBase::setProgressRange(int minimum, int maximum) | - | ||||||||||||
389 | { | - | ||||||||||||
390 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
391 | d->m_progressMinimum = minimum; | - | ||||||||||||
392 | d->m_progressMaximum = maximum; | - | ||||||||||||
393 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::ProgressRange, minimum, maximum)); | - | ||||||||||||
394 | } executed 260 times by 4 tests: end of block Executed by:
| 260 | ||||||||||||
395 | - | |||||||||||||
396 | void QFutureInterfaceBase::setProgressValue(int progressValue) | - | ||||||||||||
397 | { | - | ||||||||||||
398 | setProgressValueAndText(progressValue, QString()); | - | ||||||||||||
399 | } executed 105907 times by 5 tests: end of block Executed by:
| 105907 | ||||||||||||
400 | - | |||||||||||||
401 | void QFutureInterfaceBase::setProgressValueAndText(int progressValue, | - | ||||||||||||
402 | const QString &progressText) | - | ||||||||||||
403 | { | - | ||||||||||||
404 | QMutexLocker locker(&d->m_mutex); | - | ||||||||||||
405 | if (d->manualProgress == false)
| 261-105651 | ||||||||||||
406 | d->manualProgress = true; executed 261 times by 5 tests: d->manualProgress = true; Executed by:
| 261 | ||||||||||||
407 | if (d->m_progressValue >= progressValue)
| 48-105864 | ||||||||||||
408 | return; executed 48 times by 4 tests: return; Executed by:
| 48 | ||||||||||||
409 | - | |||||||||||||
410 | if (d->state.load() & (Canceled|Finished))
| 0-105864 | ||||||||||||
411 | return; never executed: return; | 0 | ||||||||||||
412 | - | |||||||||||||
413 | if (d->internal_updateProgress(progressValue, progressText)) {
| 524-105340 | ||||||||||||
414 | d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Progress, | - | ||||||||||||
415 | d->m_progressValue, | - | ||||||||||||
416 | d->m_progressText)); | - | ||||||||||||
417 | } executed 524 times by 5 tests: end of block Executed by:
| 524 | ||||||||||||
418 | } executed 105864 times by 5 tests: end of block Executed by:
| 105864 | ||||||||||||
419 | - | |||||||||||||
420 | QMutex *QFutureInterfaceBase::mutex() const | - | ||||||||||||
421 | { | - | ||||||||||||
422 | return &d->m_mutex; executed 111477 times by 8 tests: return &d->m_mutex; Executed by:
| 111477 | ||||||||||||
423 | } | - | ||||||||||||
424 | - | |||||||||||||
425 | QtPrivate::ExceptionStore &QFutureInterfaceBase::exceptionStore() | - | ||||||||||||
426 | { | - | ||||||||||||
427 | return d->m_exceptionStore; executed 3 times by 1 test: return d->m_exceptionStore; Executed by:
| 3 | ||||||||||||
428 | } | - | ||||||||||||
429 | - | |||||||||||||
430 | QtPrivate::ResultStoreBase &QFutureInterfaceBase::resultStoreBase() | - | ||||||||||||
431 | { | - | ||||||||||||
432 | return d->m_results; executed 68791 times by 8 tests: return d->m_results; Executed by:
| 68791 | ||||||||||||
433 | } | - | ||||||||||||
434 | - | |||||||||||||
435 | const QtPrivate::ResultStoreBase &QFutureInterfaceBase::resultStoreBase() const | - | ||||||||||||
436 | { | - | ||||||||||||
437 | return d->m_results; executed 76496 times by 8 tests: return d->m_results; Executed by:
| 76496 | ||||||||||||
438 | } | - | ||||||||||||
439 | - | |||||||||||||
440 | QFutureInterfaceBase &QFutureInterfaceBase::operator=(const QFutureInterfaceBase &other) | - | ||||||||||||
441 | { | - | ||||||||||||
442 | other.d->refCount.ref(); | - | ||||||||||||
443 | if (!d->refCount.deref())
| 26-74 | ||||||||||||
444 | delete d; executed 74 times by 4 tests: delete d; Executed by:
| 74 | ||||||||||||
445 | d = other.d; | - | ||||||||||||
446 | return *this; executed 100 times by 4 tests: return *this; Executed by:
| 100 | ||||||||||||
447 | } | - | ||||||||||||
448 | - | |||||||||||||
449 | bool QFutureInterfaceBase::refT() const | - | ||||||||||||
450 | { | - | ||||||||||||
451 | return d->refCount.refT(); executed 96297 times by 8 tests: return d->refCount.refT(); Executed by:
| 96297 | ||||||||||||
452 | } | - | ||||||||||||
453 | - | |||||||||||||
454 | bool QFutureInterfaceBase::derefT() const | - | ||||||||||||
455 | { | - | ||||||||||||
456 | return d->refCount.derefT(); executed 96297 times by 8 tests: return d->refCount.derefT(); Executed by:
| 96297 | ||||||||||||
457 | } | - | ||||||||||||
458 | - | |||||||||||||
459 | QFutureInterfaceBasePrivate::QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState) | - | ||||||||||||
460 | : refCount(1), m_progressValue(0), m_progressMinimum(0), m_progressMaximum(0), | - | ||||||||||||
461 | state(initialState), | - | ||||||||||||
462 | manualProgress(false), m_expectedResultCount(0), runnable(0), m_pool(0) | - | ||||||||||||
463 | { | - | ||||||||||||
464 | progressTime.invalidate(); | - | ||||||||||||
465 | } executed 128673 times by 12 tests: end of block Executed by:
| 128673 | ||||||||||||
466 | - | |||||||||||||
467 | int QFutureInterfaceBasePrivate::internal_resultCount() const | - | ||||||||||||
468 | { | - | ||||||||||||
469 | return m_results.count(); // ### subtract canceled results. executed 28909 times by 3 tests: return m_results.count(); Executed by:
| 28909 | ||||||||||||
470 | } | - | ||||||||||||
471 | - | |||||||||||||
472 | bool QFutureInterfaceBasePrivate::internal_isResultReadyAt(int index) const | - | ||||||||||||
473 | { | - | ||||||||||||
474 | return (m_results.contains(index)); executed 43575 times by 8 tests: return (m_results.contains(index)); Executed by:
| 43575 | ||||||||||||
475 | } | - | ||||||||||||
476 | - | |||||||||||||
477 | bool QFutureInterfaceBasePrivate::internal_waitForNextResult() | - | ||||||||||||
478 | { | - | ||||||||||||
479 | if (m_results.hasNextResult())
| 0 | ||||||||||||
480 | return true; never executed: return true; | 0 | ||||||||||||
481 | - | |||||||||||||
482 | while ((state.load() & QFutureInterfaceBase::Running) && m_results.hasNextResult() == false)
| 0 | ||||||||||||
483 | waitCondition.wait(&m_mutex); never executed: waitCondition.wait(&m_mutex); | 0 | ||||||||||||
484 | - | |||||||||||||
485 | return !(state.load() & QFutureInterfaceBase::Canceled) && m_results.hasNextResult(); never executed: return !(state.load() & QFutureInterfaceBase::Canceled) && m_results.hasNextResult();
| 0 | ||||||||||||
486 | } | - | ||||||||||||
487 | - | |||||||||||||
488 | bool QFutureInterfaceBasePrivate::internal_updateProgress(int progress, | - | ||||||||||||
489 | const QString &progressText) | - | ||||||||||||
490 | { | - | ||||||||||||
491 | if (m_progressValue >= progress)
| 0-139978 | ||||||||||||
492 | return false; never executed: return false; | 0 | ||||||||||||
493 | - | |||||||||||||
494 | m_progressValue = progress; | - | ||||||||||||
495 | m_progressText = progressText; | - | ||||||||||||
496 | - | |||||||||||||
497 | if (progressTime.isValid() && m_progressValue != m_progressMaximum) // make sure the first and last steps are emitted.
| 247-106769 | ||||||||||||
498 | if (progressTime.elapsed() < (1000 / MaxProgressEmitsPerSecond))
| 63-106459 | ||||||||||||
499 | return false; executed 106459 times by 6 tests: return false; Executed by:
| 106459 | ||||||||||||
500 | - | |||||||||||||
501 | progressTime.start(); | - | ||||||||||||
502 | return true; executed 33519 times by 8 tests: return true; Executed by:
| 33519 | ||||||||||||
503 | } | - | ||||||||||||
504 | - | |||||||||||||
505 | void QFutureInterfaceBasePrivate::internal_setThrottled(bool enable) | - | ||||||||||||
506 | { | - | ||||||||||||
507 | // bail out if we are not changing the state | - | ||||||||||||
508 | if ((enable && (state.load() & QFutureInterfaceBase::Throttled))
| 0-1164 | ||||||||||||
509 | || (!enable && !(state.load() & QFutureInterfaceBase::Throttled)))
| 0-3 | ||||||||||||
510 | return; executed 1161 times by 1 test: return; Executed by:
| 1161 | ||||||||||||
511 | - | |||||||||||||
512 | // change the state | - | ||||||||||||
513 | if (enable) {
| 0-3 | ||||||||||||
514 | switch_on(state, QFutureInterfaceBase::Throttled); | - | ||||||||||||
515 | } else { executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
516 | switch_off(state, QFutureInterfaceBase::Throttled); | - | ||||||||||||
517 | if (!(state.load() & QFutureInterfaceBase::Paused))
| 0 | ||||||||||||
518 | pausedWaitCondition.wakeAll(); never executed: pausedWaitCondition.wakeAll(); | 0 | ||||||||||||
519 | } never executed: end of block | 0 | ||||||||||||
520 | } | - | ||||||||||||
521 | - | |||||||||||||
522 | void QFutureInterfaceBasePrivate::sendCallOut(const QFutureCallOutEvent &callOutEvent) | - | ||||||||||||
523 | { | - | ||||||||||||
524 | if (outputConnections.isEmpty())
| 1055-259338 | ||||||||||||
525 | return; executed 259338 times by 11 tests: return; Executed by:
| 259338 | ||||||||||||
526 | - | |||||||||||||
527 | for (int i = 0; i < outputConnections.count(); ++i)
| 1055 | ||||||||||||
528 | outputConnections.at(i)->postCallOutEvent(callOutEvent); executed 1055 times by 1 test: outputConnections.at(i)->postCallOutEvent(callOutEvent); Executed by:
| 1055 | ||||||||||||
529 | } executed 1055 times by 1 test: end of block Executed by:
| 1055 | ||||||||||||
530 | - | |||||||||||||
531 | void QFutureInterfaceBasePrivate::sendCallOuts(const QFutureCallOutEvent &callOutEvent1, | - | ||||||||||||
532 | const QFutureCallOutEvent &callOutEvent2) | - | ||||||||||||
533 | { | - | ||||||||||||
534 | if (outputConnections.isEmpty())
| 6-32989 | ||||||||||||
535 | return; executed 32989 times by 8 tests: return; Executed by:
| 32989 | ||||||||||||
536 | - | |||||||||||||
537 | for (int i = 0; i < outputConnections.count(); ++i) {
| 6 | ||||||||||||
538 | QFutureCallOutInterface *interface = outputConnections.at(i); | - | ||||||||||||
539 | interface->postCallOutEvent(callOutEvent1); | - | ||||||||||||
540 | interface->postCallOutEvent(callOutEvent2); | - | ||||||||||||
541 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
542 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
543 | - | |||||||||||||
544 | // This function connects an output interface (for example a QFutureWatcher) | - | ||||||||||||
545 | // to this future. While holding the lock we check the state and ready results | - | ||||||||||||
546 | // and add the appropriate callouts to the queue. In order to avoid deadlocks, | - | ||||||||||||
547 | // the actual callouts are made at the end while not holding the lock. | - | ||||||||||||
548 | void QFutureInterfaceBasePrivate::connectOutputInterface(QFutureCallOutInterface *interface) | - | ||||||||||||
549 | { | - | ||||||||||||
550 | QMutexLocker locker(&m_mutex); | - | ||||||||||||
551 | - | |||||||||||||
552 | if (state.load() & QFutureInterfaceBase::Started) {
| 0-28 | ||||||||||||
553 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Started)); | - | ||||||||||||
554 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::ProgressRange, | - | ||||||||||||
555 | m_progressMinimum, | - | ||||||||||||
556 | m_progressMaximum)); | - | ||||||||||||
557 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Progress, | - | ||||||||||||
558 | m_progressValue, | - | ||||||||||||
559 | m_progressText)); | - | ||||||||||||
560 | } executed 28 times by 1 test: end of block Executed by:
| 28 | ||||||||||||
561 | - | |||||||||||||
562 | QtPrivate::ResultIteratorBase it = m_results.begin(); | - | ||||||||||||
563 | while (it != m_results.end()) {
| 28-177 | ||||||||||||
564 | const int begin = it.resultIndex(); | - | ||||||||||||
565 | const int end = begin + it.batchSize(); | - | ||||||||||||
566 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::ResultsReady, | - | ||||||||||||
567 | begin, | - | ||||||||||||
568 | end)); | - | ||||||||||||
569 | it.batchedAdvance(); | - | ||||||||||||
570 | } executed 177 times by 1 test: end of block Executed by:
| 177 | ||||||||||||
571 | - | |||||||||||||
572 | if (state.load() & QFutureInterfaceBase::Paused)
| 0-28 | ||||||||||||
573 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Paused)); never executed: interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Paused)); | 0 | ||||||||||||
574 | - | |||||||||||||
575 | if (state.load() & QFutureInterfaceBase::Canceled)
| 5-23 | ||||||||||||
576 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Canceled)); executed 5 times by 1 test: interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Canceled)); Executed by:
| 5 | ||||||||||||
577 | - | |||||||||||||
578 | if (state.load() & QFutureInterfaceBase::Finished)
| 14 | ||||||||||||
579 | interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Finished)); executed 14 times by 1 test: interface->postCallOutEvent(QFutureCallOutEvent(QFutureCallOutEvent::Finished)); Executed by:
| 14 | ||||||||||||
580 | - | |||||||||||||
581 | outputConnections.append(interface); | - | ||||||||||||
582 | } executed 28 times by 1 test: end of block Executed by:
| 28 | ||||||||||||
583 | - | |||||||||||||
584 | void QFutureInterfaceBasePrivate::disconnectOutputInterface(QFutureCallOutInterface *interface) | - | ||||||||||||
585 | { | - | ||||||||||||
586 | QMutexLocker lock(&m_mutex); | - | ||||||||||||
587 | const int index = outputConnections.indexOf(interface); | - | ||||||||||||
588 | if (index == -1)
| 23-28 | ||||||||||||
589 | return; executed 23 times by 1 test: return; Executed by:
| 23 | ||||||||||||
590 | outputConnections.removeAt(index); | - | ||||||||||||
591 | - | |||||||||||||
592 | interface->callOutInterfaceDisconnected(); | - | ||||||||||||
593 | } executed 28 times by 1 test: end of block Executed by:
| 28 | ||||||||||||
594 | - | |||||||||||||
595 | void QFutureInterfaceBasePrivate::setState(QFutureInterfaceBase::State newState) | - | ||||||||||||
596 | { | - | ||||||||||||
597 | state.store(newState); | - | ||||||||||||
598 | } executed 128312 times by 11 tests: end of block Executed by:
| 128312 | ||||||||||||
599 | - | |||||||||||||
600 | QT_END_NAMESPACE | - | ||||||||||||
601 | - | |||||||||||||
602 | #endif // QT_NO_QFUTURE | - | ||||||||||||
Source code | Switch to Preprocessed file |