thread/qthread_unix.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
10 -
11 -
12 -
13 -
14 -
15 -
16 -
17 -
18 -
19enum { ThreadPriorityResetFlag = 0x80000000 }; -
20static __thread QThreadData *currentThreadData = 0; -
21 -
22 -
23static pthread_once_t current_thread_data_once = 0; -
24static pthread_key_t current_thread_data_key; -
25 -
26static void destroy_current_thread_data(void *p) -
27{ -
28 pthread_setspecific(current_thread_data_key, p); -
29 QThreadData *data = static_cast<QThreadData *>(p); -
30 if (data->isAdopted) {
evaluated: data->isAdopted
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:1717279
18-1717279
31 QThread *thread = data->thread; -
32 qt_noop(); -
33 QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread)); -
34 qt_noop(); -
35 thread_p->finish(thread); -
36 }
executed: }
Execution Count:18
18
37 data->deref(); -
38 -
39 -
40 -
41 -
42 pthread_setspecific(current_thread_data_key, -
43 -
44 -
45 -
46 0); -
47 -
48}
executed: }
Execution Count:1717384
1717384
49 -
50static void create_current_thread_data_key() -
51{ -
52 pthread_key_create(&current_thread_data_key, destroy_current_thread_data); -
53}
never executed: }
0
54 -
55static void destroy_current_thread_data_key() -
56{ -
57 pthread_once(&current_thread_data_once, create_current_thread_data_key); -
58 pthread_key_delete(current_thread_data_key); -
59}
never executed: }
0
60namespace { static const struct destroy_current_thread_data_key_dtor_class_ { inline destroy_current_thread_data_key_dtor_class_() { } inline ~ destroy_current_thread_data_key_dtor_class_() { destroy_current_thread_data_key(); } } destroy_current_thread_data_key_dtor_instance_; }
never executed: }
0
61 -
62 -
63 -
64static QThreadData *get_thread_data() -
65{ -
66 -
67 return currentThreadData;
executed: return currentThreadData;
Execution Count:18568914
18568914
68 -
69 -
70 -
71 -
72} -
73 -
74static void set_thread_data(QThreadData *data) -
75{ -
76 -
77 currentThreadData = data; -
78 -
79 pthread_once(&current_thread_data_once, create_current_thread_data_key); -
80 pthread_setspecific(current_thread_data_key, data); -
81}
executed: }
Execution Count:1717404
1717404
82 -
83static void clear_thread_data() -
84{ -
85 -
86 currentThreadData = 0; -
87 -
88 pthread_setspecific(current_thread_data_key, 0); -
89}
never executed: }
0
90 -
91QThreadData *QThreadData::current() -
92{ -
93 QThreadData *data = get_thread_data(); -
94 if (!data) {
evaluated: !data
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:18568978
18-18568978
95 data = new QThreadData; -
96 try { -
97 set_thread_data(data); -
98 data->thread = new QAdoptedThread(data); -
99 } catch (...) {
executed: }
Execution Count:18
18
100 clear_thread_data(); -
101 data->deref(); -
102 data = 0; -
103 throw;
never executed: throw;
0
104 } -
105 data->deref(); -
106 data->isAdopted = true; -
107 data->threadId = (Qt::HANDLE)pthread_self(); -
108 if (!QCoreApplicationPrivate::theMainThread)
partially evaluated: !QCoreApplicationPrivate::theMainThread
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:18
0-18
109 QCoreApplicationPrivate::theMainThread = data->thread;
never executed: QCoreApplicationPrivate::theMainThread = data->thread;
0
110 }
executed: }
Execution Count:18
18
111 return data;
executed: return data;
Execution Count:18569006
18569006
112} -
113 -
114 -
115void QAdoptedThread::init() -
116{ -
117 QThreadPrivate * const d = d_func(); -
118 d->thread_id = pthread_self(); -
119}
executed: }
Execution Count:18
18
120 -
121 -
122 -
123 -
124 -
125 -
126extern "C" { -
127 -
128 -
129typedef void*(*QtThreadCallback)(void*); -
130 -
131 -
132} -
133 -
134 -
135 -
136 -
137void QThreadPrivate::createEventDispatcher(QThreadData *data) -
138{ -
139 -
140 -
141 -
142 -
143 if (qEnvironmentVariableIsEmpty("QT_NO_GLIB")
partially evaluated: qEnvironmentVariableIsEmpty("QT_NO_GLIB")
TRUEFALSE
yes
Evaluation Count:1717390
no
Evaluation Count:0
0-1717390
144 && qEnvironmentVariableIsEmpty("QT_NO_THREADED_GLIB")
partially evaluated: qEnvironmentVariableIsEmpty("QT_NO_THREADED_GLIB")
TRUEFALSE
yes
Evaluation Count:1717387
no
Evaluation Count:0
0-1717387
145 && QEventDispatcherGlib::versionSupported())
partially evaluated: QEventDispatcherGlib::versionSupported()
TRUEFALSE
yes
Evaluation Count:1717383
no
Evaluation Count:0
0-1717383
146 data->eventDispatcher = new QEventDispatcherGlib;
executed: data->eventDispatcher = new QEventDispatcherGlib;
Execution Count:1717383
1717383
147 else -
148 -
149 data->eventDispatcher = new QEventDispatcherUNIX;
never executed: data->eventDispatcher = new QEventDispatcherUNIX;
0
150 -
151 -
152 data->eventDispatcher->startingUp(); -
153}
executed: }
Execution Count:1717377
1717377
154 -
155 -
156 -
157 -
158static void setCurrentThreadName(pthread_t threadId, const char *name) -
159{ -
160 -
161 (void)threadId;; -
162 prctl(15, (unsigned long)name, 0, 0, 0); -
163 -
164 -
165 -
166 -
167 -
168 -
169}
executed: }
Execution Count:1717369
1717369
170 -
171 -
172void *QThreadPrivate::start(void *arg) -
173{ -
174 -
175 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, __null); -
176 -
177 do { __pthread_cleanup_class __clframe (QThreadPrivate::finish, arg); -
178 -
179 QThread *thr = reinterpret_cast<QThread *>(arg); -
180 QThreadData *data = QThreadData::get2(thr); -
181 -
182 -
183 if (int(thr->d_func()->priority) & ThreadPriorityResetFlag) {
evaluated: int(thr->d_func()->priority) & ThreadPriorityResetFlag
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:1717391
3-1717391
184 thr->setPriority(QThread::Priority(thr->d_func()->priority & ~ThreadPriorityResetFlag)); -
185 }
executed: }
Execution Count:3
3
186 -
187 data->threadId = (Qt::HANDLE)pthread_self(); -
188 set_thread_data(data); -
189 -
190 data->ref(); -
191 { -
192 QMutexLocker locker(&thr->d_func()->mutex); -
193 data->quitNow = thr->d_func()->exited; -
194 } -
195 -
196 if (data->eventDispatcher)
evaluated: data->eventDispatcher
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1717394
1-1717394
197 data->eventDispatcher->startingUp();
executed: data->eventDispatcher->startingUp();
Execution Count:1
1
198 else -
199 createEventDispatcher(data);
executed: createEventDispatcher(data);
Execution Count:1717394
1717394
200 -
201 -
202 -
203 QString objectName = thr->objectName(); -
204 -
205 if (__builtin_expect(!!(objectName.isEmpty()), true))
evaluated: __builtin_expect(!!(objectName.isEmpty()), true)
TRUEFALSE
yes
Evaluation Count:1714468
yes
Evaluation Count:2917
2917-1714468
206 setCurrentThreadName(thr->d_func()->thread_id, thr->metaObject()->className());
executed: setCurrentThreadName(thr->d_func()->thread_id, thr->metaObject()->className());
Execution Count:1714468
1714468
207 else -
208 setCurrentThreadName(thr->d_func()->thread_id, objectName.toLocal8Bit());
executed: setCurrentThreadName(thr->d_func()->thread_id, objectName.toLocal8Bit());
Execution Count:2917
2917
209 -
210 -
211 -
212 thr->started(QThread::QPrivateSignal()); -
213 -
214 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, __null); -
215 pthread_testcancel(); -
216 -
217 thr->run(); -
218 -
219 __clframe.__setdoit (1); } while (0);
executed: }
Execution Count:1717367
partially evaluated: 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1717330
0-1717367
220 -
221 return 0;
executed: return 0;
Execution Count:1717327
1717327
222} -
223 -
224void QThreadPrivate::finish(void *arg) -
225{ -
226 QThread *thr = reinterpret_cast<QThread *>(arg); -
227 QThreadPrivate *d = thr->d_func(); -
228 -
229 QMutexLocker locker(&d->mutex); -
230 -
231 d->isInFinish = true; -
232 d->priority = QThread::InheritPriority; -
233 void *data = &d->data->tls; -
234 locker.unlock(); -
235 thr->finished(QThread::QPrivateSignal()); -
236 QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); -
237 QThreadStorageData::finish((void **)data); -
238 locker.relock(); -
239 -
240 QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher; -
241 if (eventDispatcher) {
evaluated: eventDispatcher
TRUEFALSE
yes
Evaluation Count:1717370
yes
Evaluation Count:16
16-1717370
242 d->data->eventDispatcher = 0; -
243 locker.unlock(); -
244 eventDispatcher->closingDown(); -
245 delete eventDispatcher; -
246 locker.relock(); -
247 }
executed: }
Execution Count:1717357
1717357
248 -
249 d->thread_id = 0; -
250 d->running = false; -
251 d->finished = true; -
252 -
253 d->isInFinish = false; -
254 d->thread_done.wakeAll(); -
255}
executed: }
Execution Count:1717388
1717388
256Qt::HANDLE QThread::currentThreadId() -
257{ -
258 -
259 return (Qt::HANDLE)pthread_self();
executed: return (Qt::HANDLE)pthread_self();
Execution Count:10366935
10366935
260} -
261 -
262 -
263 -
264 -
265 -
266 -
267int QThread::idealThreadCount() -
268{ -
269 int cores = -1; -
270 cores = (int)sysconf(_SC_NPROCESSORS_ONLN); -
271 -
272 -
273 return cores;
executed: return cores;
Execution Count:1061
1061
274} -
275 -
276void QThread::yieldCurrentThread() -
277{ -
278 sched_yield(); -
279}
executed: }
Execution Count:768
768
280 -
281static timespec makeTimespec(time_t secs, long nsecs) -
282{ -
283 struct timespec ts; -
284 ts.tv_sec = secs; -
285 ts.tv_nsec = nsecs; -
286 return ts;
executed: return ts;
Execution Count:414348
414348
287} -
288 -
289void QThread::sleep(unsigned long secs) -
290{ -
291 qt_nanosleep(makeTimespec(secs, 0)); -
292}
executed: }
Execution Count:2
2
293 -
294void QThread::msleep(unsigned long msecs) -
295{ -
296 qt_nanosleep(makeTimespec(msecs / 1000, msecs % 1000 * 1000 * 1000)); -
297}
executed: }
Execution Count:341168
341168
298 -
299void QThread::usleep(unsigned long usecs) -
300{ -
301 qt_nanosleep(makeTimespec(usecs / 1000 / 1000, usecs % (1000*1000) * 1000)); -
302}
executed: }
Execution Count:73042
73042
303 -
304 -
305 -
306 -
307 -
308static bool calculateUnixPriority(int priority, int *sched_policy, int *__sched_priority) -
309{ -
310 -
311 if (priority == QThread::IdlePriority) {
evaluated: priority == QThread::IdlePriority
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:334
8-334
312 *sched_policy = 5; -
313 *__sched_priority = 0; -
314 return true;
executed: return true;
Execution Count:8
8
315 } -
316 const int lowestPriority = QThread::LowestPriority; -
317 -
318 -
319 -
320 const int highestPriority = QThread::TimeCriticalPriority; -
321 -
322 int prio_min = sched_get_priority_min(*sched_policy); -
323 int prio_max = sched_get_priority_max(*sched_policy); -
324 if (prio_min == -1 || prio_max == -1)
partially evaluated: prio_min == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:334
partially evaluated: prio_max == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:334
0-334
325 return false;
never executed: return false;
0
326 -
327 int prio; -
328 -
329 prio = ((priority - lowestPriority) * (prio_max - prio_min) / highestPriority) + prio_min; -
330 prio = qMax(prio_min, qMin(prio_max, prio)); -
331 -
332 *__sched_priority = prio; -
333 return true;
executed: return true;
Execution Count:334
334
334} -
335 -
336 -
337void QThread::start(Priority priority) -
338{ -
339 QThreadPrivate * const d = d_func(); -
340 QMutexLocker locker(&d->mutex); -
341 -
342 if (d->isInFinish)
evaluated: d->isInFinish
TRUEFALSE
yes
Evaluation Count:567
yes
Evaluation Count:1719322
567-1719322
343 d->thread_done.wait(locker.mutex());
executed: d->thread_done.wait(locker.mutex());
Execution Count:567
567
344 -
345 if (d->running)
evaluated: d->running
TRUEFALSE
yes
Evaluation Count:2493
yes
Evaluation Count:1717396
2493-1717396
346 return;
executed: return;
Execution Count:2493
2493
347 -
348 d->running = true; -
349 d->finished = false; -
350 d->returnCode = 0; -
351 d->exited = false; -
352 -
353 pthread_attr_t attr; -
354 pthread_attr_init(&attr); -
355 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); -
356 -
357 d->priority = priority; -
358 -
359 -
360 switch (priority) { -
361 case InheritPriority: -
362 { -
363 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); -
364 break;
executed: break;
Execution Count:1717071
1717071
365 } -
366 -
367 default: -
368 { -
369 int sched_policy; -
370 if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) {
partially evaluated: pthread_attr_getschedpolicy(&attr, &sched_policy) != 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:325
0-325
371 -
372 -
373 QMessageLogger("thread/qthread_unix.cpp", 542, __PRETTY_FUNCTION__).warning("QThread::start: Cannot determine default scheduler policy"); -
374 break;
never executed: break;
0
375 } -
376 -
377 int prio; -
378 if (!calculateUnixPriority(priority, &sched_policy, &prio)) {
partially evaluated: !calculateUnixPriority(priority, &sched_policy, &prio)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:325
0-325
379 -
380 -
381 QMessageLogger("thread/qthread_unix.cpp", 550, __PRETTY_FUNCTION__).warning("QThread::start: Cannot determine scheduler priority range"); -
382 break;
never executed: break;
0
383 } -
384 -
385 sched_param sp; -
386 sp.__sched_priority = prio; -
387 -
388 if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0
partially evaluated: pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:325
0-325
389 || pthread_attr_setschedpolicy(&attr, sched_policy) != 0
evaluated: pthread_attr_setschedpolicy(&attr, sched_policy) != 0
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:322
3-322
390 || pthread_attr_setschedparam(&attr, &sp) != 0) {
partially evaluated: pthread_attr_setschedparam(&attr, &sp) != 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:322
0-322
391 -
392 -
393 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); -
394 d->priority = Priority(priority | ThreadPriorityResetFlag); -
395 }
executed: }
Execution Count:3
3
396 break;
executed: break;
Execution Count:325
325
397 } -
398 } -
399 -
400 -
401 -
402 if (d->stackSize > 0) {
partially evaluated: d->stackSize > 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1717396
0-1717396
403 -
404 int code = pthread_attr_setstacksize(&attr, d->stackSize); -
405 -
406 -
407 -
408 -
409 if (code) {
never evaluated: code
0
410 QMessageLogger("thread/qthread_unix.cpp", 579, __PRETTY_FUNCTION__).warning("QThread::start: Thread stack size error: %s", -
411 QString(qt_error_string(code)).toLocal8Bit().constData()); -
412 -
413 -
414 -
415 d->running = false; -
416 d->finished = false; -
417 return;
never executed: return;
0
418 } -
419 }
never executed: }
0
420 -
421 int code = -
422 pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); -
423 if (code == 1) {
partially evaluated: code == 1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1717396
0-1717396
424 -
425 -
426 -
427 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); -
428 -
429 code = -
430 pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); -
431 }
never executed: }
0
432 -
433 pthread_attr_destroy(&attr); -
434 -
435 if (code) {
partially evaluated: code
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1717396
0-1717396
436 QMessageLogger("thread/qthread_unix.cpp", 605, __PRETTY_FUNCTION__).warning("QThread::start: Thread creation error: %s", QString(qt_error_string(code)).toLocal8Bit().constData()); -
437 -
438 d->running = false; -
439 d->finished = false; -
440 d->thread_id = 0; -
441 }
never executed: }
0
442}
executed: }
Execution Count:1717396
1717396
443 -
444void QThread::terminate() -
445{ -
446 -
447 QThreadPrivate * const d = d_func(); -
448 QMutexLocker locker(&d->mutex); -
449 -
450 if (!d->thread_id)
partially evaluated: !d->thread_id
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
451 return;
never executed: return;
0
452 -
453 int code = pthread_cancel(d->thread_id); -
454 if (code) {
partially evaluated: code
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
455 QMessageLogger("thread/qthread_unix.cpp", 624, __PRETTY_FUNCTION__).warning("QThread::start: Thread termination error: %s", -
456 QString(qt_error_string((code))).toLocal8Bit().constData()); -
457 }
never executed: }
0
458 -
459}
executed: }
Execution Count:2
2
460 -
461bool QThread::wait(unsigned long time) -
462{ -
463 QThreadPrivate * const d = d_func(); -
464 QMutexLocker locker(&d->mutex); -
465 -
466 if (d->thread_id == pthread_self()) {
partially evaluated: d->thread_id == pthread_self()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1716643
0-1716643
467 QMessageLogger("thread/qthread_unix.cpp", 636, __PRETTY_FUNCTION__).warning("QThread::wait: Thread tried to wait on itself"); -
468 return false;
never executed: return false;
0
469 } -
470 -
471 if (d->finished || !d->running)
evaluated: d->finished
TRUEFALSE
yes
Evaluation Count:4371
yes
Evaluation Count:1712272
partially evaluated: !d->running
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1712272
0-1712272
472 return true;
executed: return true;
Execution Count:4371
4371
473 -
474 while (d->running) {
evaluated: d->running
TRUEFALSE
yes
Evaluation Count:1712272
yes
Evaluation Count:1712200
1712200-1712272
475 if (!d->thread_done.wait(locker.mutex(), time))
evaluated: !d->thread_done.wait(locker.mutex(), time)
TRUEFALSE
yes
Evaluation Count:72
yes
Evaluation Count:1712200
72-1712200
476 return false;
executed: return false;
Execution Count:72
72
477 }
executed: }
Execution Count:1712200
1712200
478 return true;
executed: return true;
Execution Count:1712200
1712200
479} -
480 -
481void QThread::setTerminationEnabled(bool enabled) -
482{ -
483 QThread *thr = currentThread(); -
484 qt_noop(); -
485 -
486 -
487 (void)thr; -
488 -
489 -
490 -
491 pthread_setcancelstate(enabled ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, __null); -
492 if (enabled)
evaluated: enabled
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
493 pthread_testcancel();
executed: pthread_testcancel();
Execution Count:2
2
494 -
495}
executed: }
Execution Count:2
2
496 -
497void QThread::setPriority(Priority priority) -
498{ -
499 QThreadPrivate * const d = d_func(); -
500 QMutexLocker locker(&d->mutex); -
501 if (!d->running) {
evaluated: !d->running
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:17
14-17
502 QMessageLogger("thread/qthread_unix.cpp", 671, __PRETTY_FUNCTION__).warning("QThread::setPriority: Cannot set priority, thread is not running"); -
503 return;
executed: return;
Execution Count:14
14
504 } -
505 -
506 d->priority = priority; -
507 -
508 -
509 -
510 -
511 int sched_policy; -
512 sched_param param; -
513 -
514 if (pthread_getschedparam(d->thread_id, &sched_policy, &param) != 0) {
partially evaluated: pthread_getschedparam(d->thread_id, &sched_policy, &param) != 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:17
0-17
515 -
516 -
517 QMessageLogger("thread/qthread_unix.cpp", 686, __PRETTY_FUNCTION__).warning("QThread::setPriority: Cannot get scheduler parameters"); -
518 return;
never executed: return;
0
519 } -
520 -
521 int prio; -
522 if (!calculateUnixPriority(priority, &sched_policy, &prio)) {
partially evaluated: !calculateUnixPriority(priority, &sched_policy, &prio)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:17
0-17
523 -
524 -
525 QMessageLogger("thread/qthread_unix.cpp", 694, __PRETTY_FUNCTION__).warning("QThread::setPriority: Cannot determine scheduler priority range"); -
526 return;
never executed: return;
0
527 } -
528 -
529 param.__sched_priority = prio; -
530 int status = pthread_setschedparam(d->thread_id, sched_policy, &param); -
531 -
532 -
533 -
534 if (status == -1 && sched_policy == 5 && (*__errno_location ()) == 22) {
partially evaluated: status == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:17
never evaluated: sched_policy == 5
never evaluated: (*__errno_location ()) == 22
0-17
535 -
536 pthread_getschedparam(d->thread_id, &sched_policy, &param); -
537 param.__sched_priority = sched_get_priority_min(sched_policy); -
538 pthread_setschedparam(d->thread_id, sched_policy, &param); -
539 }
never executed: }
0
540 -
541 -
542 -
543 -
544}
executed: }
Execution Count:17
17
545 -
546 -
547 -
548 -
549 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial