thread/qthreadpool.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8static QThreadPool *theInstance() { static QGlobalStatic<QThreadPool > thisGlobalStatic = { { (0) }, false }; if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { QThreadPool *x = new QThreadPool; if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) delete x; else static QGlobalStaticDeleter<QThreadPool > cleanup(thisGlobalStatic); } return thisGlobalStatic.pointer.load(); }
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:3316997
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:325
yes
Evaluation Count:3316696
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:325
no
Evaluation Count:0
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:325
0-3316997
9 -
10 -
11 -
12 -
13class QThreadPoolThread : public QThread -
14{ -
15public: -
16 QThreadPoolThread(QThreadPoolPrivate *manager); -
17 void run(); -
18 void registerTheadInactive(); -
19 -
20 QThreadPoolPrivate *manager; -
21 QRunnable *runnable; -
22}; -
23QThreadPoolThread::QThreadPoolThread(QThreadPoolPrivate *manager) -
24 :manager(manager), runnable(0) -
25{ }
executed: }
Execution Count:2374
2374
26 -
27 -
28 -
29 -
30void QThreadPoolThread::run() -
31{ -
32 QMutexLocker locker(&manager->mutex); -
33 for(;;) { -
34 QRunnable *r = runnable; -
35 runnable = 0; -
36 -
37 do { -
38 if (r) {
evaluated: r
TRUEFALSE
yes
Evaluation Count:5334901
yes
Evaluation Count:5318384
5318384-5334901
39 const bool autoDelete = r->autoDelete(); -
40 -
41 -
42 -
43 locker.unlock(); -
44 -
45 try { -
46 -
47 r->run(); -
48 -
49 } catch (...) {
executed: }
Execution Count:5333225
5333225
50 QMessageLogger("thread/qthreadpool.cpp", 103, __PRETTY_FUNCTION__).warning("Qt Concurrent has caught an exception thrown from a worker thread.\n" -
51 "This is not supported, exceptions thrown in worker threads must be\n" -
52 "caught before control returns to Qt Concurrent."); -
53 registerTheadInactive(); -
54 throw;
never executed: throw;
0
55 } -
56 -
57 locker.relock(); -
58 -
59 if (autoDelete && !--r->ref)
evaluated: autoDelete
TRUEFALSE
yes
Evaluation Count:2847738
yes
Evaluation Count:2487163
partially evaluated: !--r->ref
TRUEFALSE
yes
Evaluation Count:2847738
no
Evaluation Count:0
0-2847738
60 delete r;
executed: delete r;
Execution Count:2847738
2847738
61 }
executed: }
Execution Count:5334901
5334901
62 -
63 -
64 if (manager->tooManyThreadsActive())
evaluated: manager->tooManyThreadsActive()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:10653283
2-10653283
65 break;
executed: break;
Execution Count:2
2
66 -
67 r = !manager->queue.isEmpty() ? manager->queue.takeFirst().first : 0;
evaluated: !manager->queue.isEmpty()
TRUEFALSE
yes
Evaluation Count:5332521
yes
Evaluation Count:5320762
5320762-5332521
68 } while (r != 0);
executed: }
Execution Count:10653283
evaluated: r != 0
TRUEFALSE
yes
Evaluation Count:5332521
yes
Evaluation Count:5320762
5320762-10653283
69 -
70 if (manager->isExiting) {
evaluated: manager->isExiting
TRUEFALSE
yes
Evaluation Count:2380
yes
Evaluation Count:5318384
2380-5318384
71 registerTheadInactive(); -
72 break;
executed: break;
Execution Count:2380
2380
73 } -
74 -
75 -
76 bool expired = manager->tooManyThreadsActive(); -
77 if (!expired) {
evaluated: !expired
TRUEFALSE
yes
Evaluation Count:5318382
yes
Evaluation Count:2
2-5318382
78 ++manager->waitingThreads; -
79 registerTheadInactive(); -
80 -
81 expired = !manager->runnableReady.wait(locker.mutex(), manager->expiryTimeout); -
82 ++manager->activeThreads; -
83 -
84 if (expired)
evaluated: expired
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:5318384
6-5318384
85 --manager->waitingThreads;
executed: --manager->waitingThreads;
Execution Count:6
6
86 }
executed: }
Execution Count:5318390
5318390
87 if (expired) {
evaluated: expired
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:5318384
8-5318384
88 manager->expiredThreads.enqueue(this); -
89 registerTheadInactive(); -
90 break;
executed: break;
Execution Count:8
8
91 } -
92 }
executed: }
Execution Count:5318384
5318384
93}
executed: }
Execution Count:2388
2388
94 -
95void QThreadPoolThread::registerTheadInactive() -
96{ -
97 if (--manager->activeThreads == 0)
evaluated: --manager->activeThreads == 0
TRUEFALSE
yes
Evaluation Count:3658540
yes
Evaluation Count:1662230
1662230-3658540
98 manager->noActiveThreads.wakeAll();
executed: manager->noActiveThreads.wakeAll();
Execution Count:3658540
3658540
99}
executed: }
Execution Count:5320770
5320770
100 -
101 -
102 -
103 -
104 -
105QThreadPoolPrivate:: QThreadPoolPrivate() -
106 : isExiting(false), -
107 expiryTimeout(30000), -
108 maxThreadCount(qAbs(QThread::idealThreadCount())), -
109 reservedThreads(0), -
110 waitingThreads(0), -
111 activeThreads(0) -
112{ }
executed: }
Execution Count:990
990
113 -
114bool QThreadPoolPrivate::tryStart(QRunnable *task) -
115{ -
116 if (allThreads.isEmpty()) {
evaluated: allThreads.isEmpty()
TRUEFALSE
yes
Evaluation Count:753
yes
Evaluation Count:5878991
753-5878991
117 -
118 startThread(task); -
119 return true;
executed: return true;
Execution Count:753
753
120 } -
121 -
122 -
123 if (activeThreadCount() >= maxThreadCount)
evaluated: activeThreadCount() >= maxThreadCount
TRUEFALSE
yes
Evaluation Count:561307
yes
Evaluation Count:5317684
561307-5317684
124 return false;
executed: return false;
Execution Count:561307
561307
125 -
126 if (waitingThreads > 0) {
evaluated: waitingThreads > 0
TRUEFALSE
yes
Evaluation Count:5316057
yes
Evaluation Count:1627
1627-5316057
127 -
128 --waitingThreads; -
129 enqueueTask(task); -
130 return true;
executed: return true;
Execution Count:5316057
5316057
131 } -
132 -
133 if (!expiredThreads.isEmpty()) {
evaluated: !expiredThreads.isEmpty()
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:1621
6-1621
134 -
135 QThreadPoolThread *thread = expiredThreads.dequeue(); -
136 qt_noop(); -
137 -
138 ++activeThreads; -
139 -
140 if (task->autoDelete())
evaluated: task->autoDelete()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:3
3
141 ++task->ref;
executed: ++task->ref;
Execution Count:3
3
142 thread->runnable = task; -
143 thread->start(); -
144 return true;
executed: return true;
Execution Count:6
6
145 } -
146 -
147 -
148 startThread(task); -
149 return true;
executed: return true;
Execution Count:1621
1621
150} -
151 -
152inline bool operator<(int priority, const QPair<QRunnable *, int> &p) -
153{ return p.second < priority; }
never executed: return p.second < priority;
0
154inline bool operator<(const QPair<QRunnable *, int> &p, int priority) -
155{ return priority < p.second; }
never executed: return priority < p.second;
0
156 -
157void QThreadPoolPrivate::enqueueTask(QRunnable *runnable, int priority) -
158{ -
159 if (runnable->autoDelete())
evaluated: runnable->autoDelete()
TRUEFALSE
yes
Evaluation Count:3370282
yes
Evaluation Count:2487095
2487095-3370282
160 ++runnable->ref;
executed: ++runnable->ref;
Execution Count:3370282
3370282
161 -
162 -
163 QList<QPair<QRunnable *, int> >::const_iterator begin = queue.constBegin(); -
164 QList<QPair<QRunnable *, int> >::const_iterator it = queue.constEnd(); -
165 if (it != begin && priority < (*(it - 1)).second)
evaluated: it != begin
TRUEFALSE
yes
Evaluation Count:1696581
yes
Evaluation Count:4160796
partially evaluated: priority < (*(it - 1)).second
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1696581
0-4160796
166 it = std::upper_bound(begin, --it, priority);
never executed: it = std::upper_bound(begin, --it, priority);
0
167 queue.insert(it - begin, qMakePair(runnable, priority)); -
168 runnableReady.wakeOne(); -
169}
executed: }
Execution Count:5857377
5857377
170 -
171int QThreadPoolPrivate::activeThreadCount() const -
172{ -
173 -
174 -
175 return (allThreads.count() 22355314
176 - expiredThreads.count() 22355314
177 - waitingThreads 22355314
178 + reservedThreads);
executed: return (allThreads.count() - expiredThreads.count() - waitingThreads + reservedThreads);
Execution Count:22355314
22355314
179} -
180 -
181void QThreadPoolPrivate::tryToStartMoreThreads() -
182{ -
183 -
184 while (!queue.isEmpty() && tryStart(queue.first().first))
evaluated: !queue.isEmpty()
TRUEFALSE
yes
Evaluation Count:11
yes
Evaluation Count:49811
evaluated: tryStart(queue.first().first)
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:2
2-49811
185 queue.removeFirst();
executed: queue.removeFirst();
Execution Count:9
9
186}
executed: }
Execution Count:49813
49813
187 -
188bool QThreadPoolPrivate::tooManyThreadsActive() const -
189{ -
190 const int activeThreadCount = this->activeThreadCount(); -
191 return activeThreadCount > maxThreadCount && (activeThreadCount - reservedThreads) > 1;
executed: return activeThreadCount > maxThreadCount && (activeThreadCount - reservedThreads) > 1;
Execution Count:15971669
15971669
192} -
193 -
194 -
195 -
196 -
197void QThreadPoolPrivate::startThread(QRunnable *runnable) -
198{ -
199 QScopedPointer <QThreadPoolThread> thread(new QThreadPoolThread(this)); -
200 thread->setObjectName(QLatin1String("Thread (pooled)")); -
201 allThreads.insert(thread.data()); -
202 ++activeThreads; -
203 -
204 if (runnable->autoDelete())
evaluated: runnable->autoDelete()
TRUEFALSE
yes
Evaluation Count:2300
yes
Evaluation Count:74
74-2300
205 ++runnable->ref;
executed: ++runnable->ref;
Execution Count:2300
2300
206 thread->runnable = runnable; -
207 thread.take()->start(); -
208}
executed: }
Execution Count:2374
2374
209 -
210 -
211 -
212 -
213 -
214void QThreadPoolPrivate::reset() -
215{ -
216 QMutexLocker locker(&mutex); -
217 isExiting = true; -
218 runnableReady.wakeAll(); -
219 -
220 while (!allThreads.empty()) {
evaluated: !allThreads.empty()
TRUEFALSE
yes
Evaluation Count:754
yes
Evaluation Count:1594
754-1594
221 -
222 QSet<QThreadPoolThread *> allThreadsCopy; -
223 allThreadsCopy.swap(allThreads); -
224 locker.unlock(); -
225 -
226 for (QForeachContainer<__typeof__(allThreadsCopy)> _container_(allThreadsCopy); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (QThreadPoolThread *thread = *_container_.i;; __extension__ ({--_container_.brk; break;})) { -
227 thread->wait(); -
228 delete thread; -
229 }
executed: }
Execution Count:2382
2382
230 -
231 locker.relock(); -
232 -
233 }
executed: }
Execution Count:754
754
234 -
235 waitingThreads = 0; -
236 expiredThreads.clear(); -
237 -
238 isExiting = false; -
239}
executed: }
Execution Count:1594
1594
240 -
241bool QThreadPoolPrivate::waitForDone(int msecs) -
242{ -
243 QMutexLocker locker(&mutex); -
244 if (msecs < 0) {
evaluated: msecs < 0
TRUEFALSE
yes
Evaluation Count:1593
yes
Evaluation Count:2
2-1593
245 while (!(queue.isEmpty() && activeThreads == 0))
evaluated: queue.isEmpty()
TRUEFALSE
yes
Evaluation Count:2193
yes
Evaluation Count:35
evaluated: activeThreads == 0
TRUEFALSE
yes
Evaluation Count:1593
yes
Evaluation Count:600
35-2193
246 noActiveThreads.wait(locker.mutex());
executed: noActiveThreads.wait(locker.mutex());
Execution Count:635
635
247 } else {
executed: }
Execution Count:1593
1593
248 QElapsedTimer timer; -
249 timer.start(); -
250 int t; -
251 while (!(queue.isEmpty() && activeThreads == 0) &&
partially evaluated: queue.isEmpty()
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
evaluated: activeThreads == 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:3
0-4
252 ((t = msecs - timer.elapsed()) > 0))
evaluated: ((t = msecs - timer.elapsed()) > 0)
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:1
1-2
253 noActiveThreads.wait(locker.mutex(), t);
executed: noActiveThreads.wait(locker.mutex(), t);
Execution Count:2
2
254 }
executed: }
Execution Count:2
2
255 return queue.isEmpty() && activeThreads == 0;
executed: return queue.isEmpty() && activeThreads == 0;
Execution Count:1595
1595
256} -
257 -
258 -
259 -
260 -
261 -
262 -
263 -
264void QThreadPoolPrivate::stealRunnable(QRunnable *runnable) -
265{ -
266 if (runnable == 0)
evaluated: runnable == 0
TRUEFALSE
yes
Evaluation Count:43869
yes
Evaluation Count:520633
43869-520633
267 return;
executed: return;
Execution Count:43869
43869
268 bool found = false; -
269 { -
270 QMutexLocker locker(&mutex); -
271 QList<QPair<QRunnable *, int> >::iterator it = queue.begin(); -
272 QList<QPair<QRunnable *, int> >::iterator end = queue.end(); -
273 -
274 while (it != end) {
evaluated: it != end
TRUEFALSE
yes
Evaluation Count:11851155
yes
Evaluation Count:526
526-11851155
275 if (it->first == runnable) {
evaluated: it->first == runnable
TRUEFALSE
yes
Evaluation Count:524847
yes
Evaluation Count:11326308
524847-11326308
276 found = true; -
277 queue.erase(it); -
278 break;
executed: break;
Execution Count:524847
524847
279 } -
280 ++it; -
281 }
executed: }
Execution Count:11326308
11326308
282 } -
283 -
284 if (!found)
evaluated: !found
TRUEFALSE
yes
Evaluation Count:526
yes
Evaluation Count:522942
526-522942
285 return;
executed: return;
Execution Count:526
526
286 -
287 const bool autoDelete = runnable->autoDelete(); -
288 bool del = autoDelete && !--runnable->ref;
partially evaluated: autoDelete
TRUEFALSE
yes
Evaluation Count:522909
no
Evaluation Count:0
partially evaluated: !--runnable->ref
TRUEFALSE
yes
Evaluation Count:522741
no
Evaluation Count:0
0-522909
289 -
290 runnable->run(); -
291 -
292 if (del) {
partially evaluated: del
TRUEFALSE
yes
Evaluation Count:521012
no
Evaluation Count:0
0-521012
293 delete runnable; -
294 }
executed: }
Execution Count:520747
520747
295}
executed: }
Execution Count:520573
520573
296QThreadPool::QThreadPool(QObject *parent) -
297 : QObject(*new QThreadPoolPrivate, parent) -
298{ }
executed: }
Execution Count:990
990
299 -
300 -
301 -
302 -
303 -
304QThreadPool::~QThreadPool() -
305{ -
306 waitForDone(); -
307}
executed: }
Execution Count:992
992
308 -
309 -
310 -
311 -
312QThreadPool *QThreadPool::globalInstance() -
313{ -
314 return theInstance();
executed: return theInstance();
Execution Count:3317391
3317391
315} -
316void QThreadPool::start(QRunnable *runnable, int priority) -
317{ -
318 if (!runnable)
partially evaluated: !runnable
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5556497
0-5556497
319 return;
never executed: return;
0
320 -
321 QThreadPoolPrivate * const d = d_func(); -
322 QMutexLocker locker(&d->mutex); -
323 if (!d->tryStart(runnable))
evaluated: !d->tryStart(runnable)
TRUEFALSE
yes
Evaluation Count:541320
yes
Evaluation Count:5019830
541320-5019830
324 d->enqueueTask(runnable, priority);
executed: d->enqueueTask(runnable, priority);
Execution Count:541320
541320
325}
executed: }
Execution Count:5561150
5561150
326bool QThreadPool::tryStart(QRunnable *runnable) -
327{ -
328 if (!runnable)
partially evaluated: !runnable
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:461856
0-461856
329 return false;
never executed: return false;
0
330 -
331 QThreadPoolPrivate * const d = d_func(); -
332 -
333 -
334 -
335 if (d->allThreads.isEmpty() == false && d->activeThreadCount() >= d->maxThreadCount)
evaluated: d->allThreads.isEmpty() == false
TRUEFALSE
yes
Evaluation Count:461683
yes
Evaluation Count:4
evaluated: d->activeThreadCount() >= d->maxThreadCount
TRUEFALSE
yes
Evaluation Count:143566
yes
Evaluation Count:317856
4-461683
336 return false;
executed: return false;
Execution Count:143452
143452
337 -
338 QMutexLocker locker(&d->mutex); -
339 return d->tryStart(runnable);
executed: return d->tryStart(runnable);
Execution Count:318583
318583
340} -
341int QThreadPool::expiryTimeout() const -
342{ -
343 const QThreadPoolPrivate * const d = d_func(); -
344 return d->expiryTimeout;
executed: return d->expiryTimeout;
Execution Count:3
3
345} -
346 -
347void QThreadPool::setExpiryTimeout(int expiryTimeout) -
348{ -
349 QThreadPoolPrivate * const d = d_func(); -
350 if (d->expiryTimeout == expiryTimeout)
partially evaluated: d->expiryTimeout == expiryTimeout
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
351 return;
never executed: return;
0
352 d->expiryTimeout = expiryTimeout; -
353}
executed: }
Execution Count:2
2
354int QThreadPool::maxThreadCount() const -
355{ -
356 const QThreadPoolPrivate * const d = d_func(); -
357 return d->maxThreadCount;
executed: return d->maxThreadCount;
Execution Count:26645
26645
358} -
359 -
360void QThreadPool::setMaxThreadCount(int maxThreadCount) -
361{ -
362 QThreadPoolPrivate * const d = d_func(); -
363 QMutexLocker locker(&d->mutex); -
364 -
365 if (maxThreadCount == d->maxThreadCount)
partially evaluated: maxThreadCount == d->maxThreadCount
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:149
0-149
366 return;
never executed: return;
0
367 -
368 d->maxThreadCount = maxThreadCount; -
369 d->tryToStartMoreThreads(); -
370}
executed: }
Execution Count:149
149
371int QThreadPool::activeThreadCount() const -
372{ -
373 const QThreadPoolPrivate * const d = d_func(); -
374 return d->activeThreadCount();
executed: return d->activeThreadCount();
Execution Count:49776
49776
375} -
376void QThreadPool::reserveThread() -
377{ -
378 QThreadPoolPrivate * const d = d_func(); -
379 QMutexLocker locker(&d->mutex); -
380 ++d->reservedThreads; -
381}
executed: }
Execution Count:49664
49664
382void QThreadPool::releaseThread() -
383{ -
384 QThreadPoolPrivate * const d = d_func(); -
385 QMutexLocker locker(&d->mutex); -
386 --d->reservedThreads; -
387 d->tryToStartMoreThreads(); -
388}
executed: }
Execution Count:49664
49664
389 -
390 -
391 -
392 -
393 -
394 -
395 -
396bool QThreadPool::waitForDone(int msecs) -
397{ -
398 QThreadPoolPrivate * const d = d_func(); -
399 bool rc = d->waitForDone(msecs); -
400 if (rc)
evaluated: rc
TRUEFALSE
yes
Evaluation Count:1594
yes
Evaluation Count:1
1-1594
401 d->reset();
executed: d->reset();
Execution Count:1594
1594
402 return rc;
executed: return rc;
Execution Count:1595
1595
403} -
404 -
405 -
406 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial