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:3266734
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:343
yes
Evaluation Count:3266542
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:343
no
Evaluation Count:0
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:343
0-3266734
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:2373
2373
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:5294639
yes
Evaluation Count:5274917
5274917-5294639
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:5293120
5293120
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:2868387
yes
Evaluation Count:2426252
partially evaluated: !--r->ref
TRUEFALSE
yes
Evaluation Count:2868387
no
Evaluation Count:0
0-2868387
60 delete r;
executed: delete r;
Execution Count:2868387
2868387
61 }
executed: }
Execution Count:5294639
5294639
62 -
63 -
64 if (manager->tooManyThreadsActive())
evaluated: manager->tooManyThreadsActive()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:10569554
2-10569554
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:5292259
yes
Evaluation Count:5277295
5277295-5292259
68 } while (r != 0);
executed: }
Execution Count:10569554
evaluated: r != 0
TRUEFALSE
yes
Evaluation Count:5292259
yes
Evaluation Count:5277295
5277295-10569554
69 -
70 if (manager->isExiting) {
evaluated: manager->isExiting
TRUEFALSE
yes
Evaluation Count:2380
yes
Evaluation Count:5274917
2380-5274917
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:5274915
yes
Evaluation Count:2
2-5274915
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:5274917
6-5274917
85 --manager->waitingThreads;
executed: --manager->waitingThreads;
Execution Count:6
6
86 }
executed: }
Execution Count:5274923
5274923
87 if (expired) {
evaluated: expired
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:5274917
8-5274917
88 manager->expiredThreads.enqueue(this); -
89 registerTheadInactive(); -
90 break;
executed: break;
Execution Count:8
8
91 } -
92 }
executed: }
Execution Count:5274917
5274917
93}
executed: }
Execution Count:2388
2388
94 -
95void QThreadPoolThread::registerTheadInactive() -
96{ -
97 if (--manager->activeThreads == 0)
evaluated: --manager->activeThreads == 0
TRUEFALSE
yes
Evaluation Count:3647374
yes
Evaluation Count:1629929
1629929-3647374
98 manager->noActiveThreads.wakeAll();
executed: manager->noActiveThreads.wakeAll();
Execution Count:3647374
3647374
99}
executed: }
Execution Count:5277303
5277303
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:1008
1008
113 -
114bool QThreadPoolPrivate::tryStart(QRunnable *task) -
115{ -
116 if (allThreads.isEmpty()) {
evaluated: allThreads.isEmpty()
TRUEFALSE
yes
Evaluation Count:753
yes
Evaluation Count:5839291
753-5839291
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:565075
yes
Evaluation Count:5274216
565075-5274216
124 return false;
executed: return false;
Execution Count:565075
565075
125 -
126 if (waitingThreads > 0) {
evaluated: waitingThreads > 0
TRUEFALSE
yes
Evaluation Count:5272589
yes
Evaluation Count:1627
1627-5272589
127 -
128 --waitingThreads; -
129 enqueueTask(task); -
130 return true;
executed: return true;
Execution Count:5272589
5272589
131 } -
132 -
133 if (!expiredThreads.isEmpty()) {
evaluated: !expiredThreads.isEmpty()
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:1620
7-1620
134 -
135 QThreadPoolThread *thread = expiredThreads.dequeue(); -
136 qt_noop(); -
137 -
138 ++activeThreads; -
139 -
140 if (task->autoDelete())
evaluated: task->autoDelete()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:3
3-4
141 ++task->ref;
executed: ++task->ref;
Execution Count:4
4
142 thread->runnable = task; -
143 thread->start(); -
144 return true;
executed: return true;
Execution Count:7
7
145 } -
146 -
147 -
148 startThread(task); -
149 return true;
executed: return true;
Execution Count:1620
1620
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:3390889
yes
Evaluation Count:2426185
2426185-3390889
160 ++runnable->ref;
executed: ++runnable->ref;
Execution Count:3390889
3390889
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:1659207
yes
Evaluation Count:4157867
partially evaluated: priority < (*(it - 1)).second
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1659207
0-4157867
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:5817074
5817074
170 -
171int QThreadPoolPrivate::activeThreadCount() const -
172{ -
173 -
174 -
175 return (allThreads.count() 22177186
176 - expiredThreads.count() 22177186
177 - waitingThreads 22177186
178 + reservedThreads);
executed: return (allThreads.count() - expiredThreads.count() - waitingThreads + reservedThreads);
Execution Count:22177186
22177186
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:15844473
15844473
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:73
73-2300
205 ++runnable->ref;
executed: ++runnable->ref;
Execution Count:2300
2300
206 thread->runnable = runnable; -
207 thread.take()->start(); -
208}
executed: }
Execution Count:2373
2373
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:1644
754-1644
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:2381
2381
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:1644
1644
240 -
241bool QThreadPoolPrivate::waitForDone(int msecs) -
242{ -
243 QMutexLocker locker(&mutex); -
244 if (msecs < 0) {
evaluated: msecs < 0
TRUEFALSE
yes
Evaluation Count:1643
yes
Evaluation Count:2
2-1643
245 while (!(queue.isEmpty() && activeThreads == 0))
evaluated: queue.isEmpty()
TRUEFALSE
yes
Evaluation Count:2224
yes
Evaluation Count:29
evaluated: activeThreads == 0
TRUEFALSE
yes
Evaluation Count:1643
yes
Evaluation Count:581
29-2224
246 noActiveThreads.wait(locker.mutex());
executed: noActiveThreads.wait(locker.mutex());
Execution Count:610
610
247 } else {
executed: }
Execution Count:1643
1643
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:1645
1645
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:43752
yes
Evaluation Count:520796
43752-520796
267 return;
executed: return;
Execution Count:43752
43752
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:11673679
yes
Evaluation Count:561
561-11673679
275 if (it->first == runnable) {
evaluated: it->first == runnable
TRUEFALSE
yes
Evaluation Count:524806
yes
Evaluation Count:11148873
524806-11148873
276 found = true; -
277 queue.erase(it); -
278 break;
executed: break;
Execution Count:524806
524806
279 } -
280 ++it; -
281 }
executed: }
Execution Count:11148873
11148873
282 } -
283 -
284 if (!found)
evaluated: !found
TRUEFALSE
yes
Evaluation Count:561
yes
Evaluation Count:522772
561-522772
285 return;
executed: return;
Execution Count:561
561
286 -
287 const bool autoDelete = runnable->autoDelete(); -
288 bool del = autoDelete && !--runnable->ref;
partially evaluated: autoDelete
TRUEFALSE
yes
Evaluation Count:522682
no
Evaluation Count:0
partially evaluated: !--runnable->ref
TRUEFALSE
yes
Evaluation Count:522487
no
Evaluation Count:0
0-522682
289 -
290 runnable->run(); -
291 -
292 if (del) {
partially evaluated: del
TRUEFALSE
yes
Evaluation Count:521116
no
Evaluation Count:0
0-521116
293 delete runnable; -
294 }
executed: }
Execution Count:521208
521208
295}
executed: }
Execution Count:521194
521194
296QThreadPool::QThreadPool(QObject *parent) -
297 : QObject(*new QThreadPoolPrivate, parent) -
298{ }
executed: }
Execution Count:1008
1008
299 -
300 -
301 -
302 -
303 -
304QThreadPool::~QThreadPool() -
305{ -
306 waitForDone(); -
307}
executed: }
Execution Count:1010
1010
308 -
309 -
310 -
311 -
312QThreadPool *QThreadPool::globalInstance() -
313{ -
314 return theInstance();
executed: return theInstance();
Execution Count:3267503
3267503
315} -
316void QThreadPool::start(QRunnable *runnable, int priority) -
317{ -
318 if (!runnable)
partially evaluated: !runnable
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5526421
0-5526421
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:544485
yes
Evaluation Count:4986686
544485-4986686
324 d->enqueueTask(runnable, priority);
executed: d->enqueueTask(runnable, priority);
Execution Count:544485
544485
325}
executed: }
Execution Count:5531171
5531171
326bool QThreadPool::tryStart(QRunnable *runnable) -
327{ -
328 if (!runnable)
partially evaluated: !runnable
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:450576
0-450576
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:450384
yes
Evaluation Count:4
evaluated: d->activeThreadCount() >= d->maxThreadCount
TRUEFALSE
yes
Evaluation Count:141097
yes
Evaluation Count:308167
4-450384
336 return false;
executed: return false;
Execution Count:141400
141400
337 -
338 QMutexLocker locker(&d->mutex); -
339 return d->tryStart(runnable);
executed: return d->tryStart(runnable);
Execution Count:308862
308862
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:26671
26671
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:1644
yes
Evaluation Count:1
1-1644
401 d->reset();
executed: d->reset();
Execution Count:1644
1644
402 return rc;
executed: return rc;
Execution Count:1645
1645
403} -
404 -
405 -
406 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial