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