thread/qthread.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qthread.h" -
43#include "qthreadstorage.h" -
44#include "qmutex.h" -
45#include "qreadwritelock.h" -
46#include "qabstracteventdispatcher.h" -
47 -
48#include <qeventloop.h> -
49#include <qhash.h> -
50 -
51#include "qthread_p.h" -
52#include "private/qcoreapplication_p.h" -
53 -
54QT_BEGIN_NAMESPACE -
55 -
56/* -
57 QThreadData -
58*/ -
59 -
60QThreadData::QThreadData(int initialRefCount) -
61 : _ref(initialRefCount), thread(0), threadId(0), -
62 quitNow(false), loopLevel(0), eventDispatcher(0), canWait(true), isAdopted(false) -
63{ -
64 // fprintf(stderr, "QThreadData %p created\n", this); -
65}
executed: }
Execution Count:1641606
1641606
66 -
67QThreadData::~QThreadData() -
68{ -
69 Q_ASSERT(_ref.load() == 0);
executed (the execution status of this line is deduced): qt_noop();
-
70 -
71 // In the odd case that Qt is running on a secondary thread, the main -
72 // thread instance will have been dereffed asunder because of the deref in -
73 // QThreadData::current() and the deref in the pthread_destroy. To avoid -
74 // crashing during QCoreApplicationData's global static cleanup we need to -
75 // safeguard the main thread here.. This fix is a bit crude, but it solves -
76 // the problem... -
77 if (this->thread == QCoreApplicationPrivate::theMainThread) {
partially evaluated: this->thread == QCoreApplicationPrivate::theMainThread
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1641764
0-1641764
78 QCoreApplicationPrivate::theMainThread = 0;
never executed (the execution status of this line is deduced): QCoreApplicationPrivate::theMainThread = 0;
-
79 }
never executed: }
0
80 -
81 QThread *t = thread;
executed (the execution status of this line is deduced): QThread *t = thread;
-
82 thread = 0;
executed (the execution status of this line is deduced): thread = 0;
-
83 delete t;
executed (the execution status of this line is deduced): delete t;
-
84 -
85 for (int i = 0; i < postEventList.size(); ++i) {
evaluated: i < postEventList.size()
TRUEFALSE
yes
Evaluation Count:89
yes
Evaluation Count:1641764
89-1641764
86 const QPostEvent &pe = postEventList.at(i);
executed (the execution status of this line is deduced): const QPostEvent &pe = postEventList.at(i);
-
87 if (pe.event) {
partially evaluated: pe.event
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:89
0-89
88 --pe.receiver->d_func()->postedEvents;
never executed (the execution status of this line is deduced): --pe.receiver->d_func()->postedEvents;
-
89 pe.event->posted = false;
never executed (the execution status of this line is deduced): pe.event->posted = false;
-
90 delete pe.event;
never executed (the execution status of this line is deduced): delete pe.event;
-
91 }
never executed: }
0
92 }
executed: }
Execution Count:89
89
93 -
94 // fprintf(stderr, "QThreadData %p destroyed\n", this); -
95}
executed: }
Execution Count:1641764
1641764
96 -
97void QThreadData::ref() -
98{ -
99#ifndef QT_NO_THREAD -
100 (void) _ref.ref();
executed (the execution status of this line is deduced): (void) _ref.ref();
-
101 Q_ASSERT(_ref.load() != 0);
executed (the execution status of this line is deduced): qt_noop();
-
102#endif -
103}
executed: }
Execution Count:5380922
5380922
104 -
105void QThreadData::deref() -
106{ -
107#ifndef QT_NO_THREAD -
108 if (!_ref.deref())
evaluated: !_ref.deref()
TRUEFALSE
yes
Evaluation Count:1641764
yes
Evaluation Count:5378903
1641764-5378903
109 delete this;
executed: delete this;
Execution Count:1641764
1641764
110#endif -
111}
executed: }
Execution Count:7020668
7020668
112 -
113/* -
114 QAdoptedThread -
115*/ -
116 -
117QAdoptedThread::QAdoptedThread(QThreadData *data) -
118 : QThread(*new QThreadPrivate(data)) -
119{ -
120 // thread should be running and not finished for the lifetime -
121 // of the application (even if QCoreApplication goes away) -
122#ifndef QT_NO_THREAD -
123 d_func()->running = true;
executed (the execution status of this line is deduced): d_func()->running = true;
-
124 d_func()->finished = false;
executed (the execution status of this line is deduced): d_func()->finished = false;
-
125 init();
executed (the execution status of this line is deduced): init();
-
126#endif -
127 -
128 // fprintf(stderr, "new QAdoptedThread = %p\n", this); -
129}
executed: }
Execution Count:18
18
130 -
131QAdoptedThread::~QAdoptedThread() -
132{ -
133 // fprintf(stderr, "~QAdoptedThread = %p\n", this); -
134} -
135 -
136void QAdoptedThread::run() -
137{ -
138 // this function should never be called -
139 qFatal("QAdoptedThread::run(): Internal error, this implementation should never be called.");
never executed (the execution status of this line is deduced): QMessageLogger("thread/qthread.cpp", 139, __PRETTY_FUNCTION__).fatal("QAdoptedThread::run(): Internal error, this implementation should never be called.");
-
140}
never executed: }
0
141#ifndef QT_NO_THREAD -
142/* -
143 QThreadPrivate -
144*/ -
145 -
146QThreadPrivate::QThreadPrivate(QThreadData *d) -
147 : QObjectPrivate(), running(false), finished(false), -
148 isInFinish(false), exited(false), returnCode(-1), -
149 stackSize(0), priority(QThread::InheritPriority), data(d) -
150{ -
151#if defined (Q_OS_UNIX) -
152 thread_id = 0;
executed (the execution status of this line is deduced): thread_id = 0;
-
153#elif defined (Q_OS_WIN) -
154 handle = 0; -
155 id = 0; -
156 waiters = 0; -
157#endif -
158#if defined (Q_OS_WIN) -
159 terminationEnabled = true; -
160 terminatePending = false; -
161#endif -
162 -
163 if (!data)
evaluated: !data
TRUEFALSE
yes
Evaluation Count:1641583
yes
Evaluation Count:18
18-1641583
164 data = new QThreadData;
executed: data = new QThreadData;
Execution Count:1641583
1641583
165}
executed: }
Execution Count:1641601
1641601
166 -
167QThreadPrivate::~QThreadPrivate() -
168{ -
169 data->deref();
executed (the execution status of this line is deduced): data->deref();
-
170}
executed: }
Execution Count:1641767
1641767
171 -
172/*! -
173 \class QThread -
174 \inmodule QtCore -
175 \brief The QThread class provides a platform-independent way to -
176 manage threads. -
177 -
178 \ingroup thread -
179 -
180 A QThread object manages one thread of control within the -
181 program. To make code run in a separate thread, simply create a -
182 QThread, change the thread affinity of the QObject(s) that -
183 contain the code, and start() the new event loop. For example: -
184 -
185 \snippet code/src_corelib_thread_qthread.cpp 0 -
186 -
187 The code inside the Worker's slot would then execute in a -
188 separate thread. In this example, the QThread triggers the -
189 Worker's doWork() slot upon starting, and frees the Worker's -
190 memory upon terminating. However, you are free to connect the -
191 Worker's slots to any signal, from any object, in any thread. It -
192 is safe to connect signals and slots across different threads, -
193 thanks to a mechanism called \l{Qt::QueuedConnection}{queued -
194 connections}. -
195 -
196 \note If you interact with an object, using any technique other -
197 than queued signal/slot connections (e.g. direct function calls), -
198 then the usual multithreading precautions need to be taken. -
199 -
200 \note It is not possible to change the thread affinity of GUI -
201 objects; they must remain in the main thread. -
202 -
203 -
204 \section1 Managing threads -
205 -
206 QThread will notifiy you via a signal when the thread is -
207 started() and finished(), or you can use isFinished() and -
208 isRunning() to query the state of the thread. -
209 -
210 You can stop the thread by calling exit() or quit(). In extreme -
211 cases, you may want to forcibly terminate() an executing thread. -
212 However, doing so is dangerous and discouraged. Please read the -
213 documentation for terminate() and setTerminationEnabled() for -
214 detailed information. -
215 -
216 From Qt 4.8 onwards, it is possible to deallocate objects that -
217 live in a thread that has just ended, by connecting the -
218 finished() signal to QObject::deleteLater(). -
219 -
220 Use wait() to block the calling thread, until the other thread -
221 has finished execution (or until a specified time has passed). -
222 -
223 QThread also provides static, platform independent sleep -
224 functions: sleep(), msleep(), and usleep() allow full second, -
225 millisecond, and microsecond resolution respectively. These -
226 functions were made public in Qt 5.0. -
227 -
228 \note wait() and the sleep() functions should be unnecessary in -
229 general, since Qt is an event-driven framework. Instead of -
230 wait(), consider listening for the finished() signal. Instead of -
231 the sleep() functions, consider using QTimer. -
232 -
233 The static functions currentThreadId() and currentThread() return -
234 identifiers for the currently executing thread. The former -
235 returns a platform specific ID for the thread; the latter returns -
236 a QThread pointer. -
237 -
238 To choose the name that your thread will be given (as identified -
239 by the command \c{ps -L} on Linux, for example), you can call -
240 \l{QObject::setObjectName()}{setObjectName()} before starting the thread. -
241 If you don't call \l{QObject::setObjectName()}{setObjectName()}, -
242 the name given to your thread will be the class name of the runtime -
243 type of your thread object (for example, \c "RenderThread" in the case of the -
244 \l{Mandelbrot Example}, as that is the name of the QThread subclass). -
245 Note that this is currently not available with release builds on Windows. -
246 -
247 \section1 Subclassing QThread -
248 -
249 Subclassing QThread is unnecessary for most purposes, since -
250 QThread provides fully-functional thread management capabilities. -
251 Nonetheless, QThread can be subclassed if you wish to implement -
252 advanced thread management. This is done by adding new member -
253 functions to the subclass, and/or by reimplementing run(). -
254 QThread's run() function is analogous to an application's main() -
255 function -- it is executed when the thread is started, and the -
256 thread will end when it returns. -
257 -
258 \note Prior to Qt 4.4, the only way to use QThread for parallel -
259 processing was to subclass it and implement the processing code -
260 inside run(). This approach is now considered \b {bad practice}; -
261 a QThread should only manage a thread, not process data. -
262 -
263 If you require event handling and signal/slot connections to -
264 work in your thread, and if you reimplement run(), you must -
265 explicitly call exec() at the end of your reimplementation: -
266 -
267 \snippet code/src_corelib_thread_qthread.cpp 1 -
268 -
269 It is important to remember that a QThread object usually lives -
270 in the thread where it was created, not in the thread that it -
271 manages. This oft-overlooked detail means that a QThread's slots -
272 will be executed in the context of its home thread, not in the -
273 context of the thread it is managing. For this reason, -
274 implementing new slots in a QThread subclass is error-prone and -
275 discouraged. -
276 -
277 \sa {Thread Support in Qt}, QThreadStorage, QMutex, QSemaphore, QWaitCondition, -
278 {Mandelbrot Example}, {Semaphores Example}, {Wait Conditions Example} -
279*/ -
280 -
281/*! -
282 \fn Qt::HANDLE QThread::currentThreadId() -
283 -
284 Returns the thread handle of the currently executing thread. -
285 -
286 \warning The handle returned by this function is used for internal -
287 purposes and should not be used in any application code. -
288 -
289 \warning On Windows, the returned value is a pseudo-handle for the -
290 current thread. It can't be used for numerical comparison. i.e., -
291 this function returns the DWORD (Windows-Thread ID) returned by -
292 the Win32 function getCurrentThreadId(), not the HANDLE -
293 (Windows-Thread HANDLE) returned by the Win32 function -
294 getCurrentThread(). -
295*/ -
296 -
297/*! -
298 \fn int QThread::idealThreadCount() -
299 -
300 Returns the ideal number of threads that can be run on the system. This is done querying -
301 the number of processor cores, both real and logical, in the system. This function returns -1 -
302 if the number of processor cores could not be detected. -
303*/ -
304 -
305/*! -
306 \fn void QThread::yieldCurrentThread() -
307 -
308 Yields execution of the current thread to another runnable thread, -
309 if any. Note that the operating system decides to which thread to -
310 switch. -
311*/ -
312 -
313/*! -
314 \fn void QThread::start(Priority priority) -
315 -
316 Begins execution of the thread by calling run(). The -
317 operating system will schedule the thread according to the \a -
318 priority parameter. If the thread is already running, this -
319 function does nothing. -
320 -
321 The effect of the \a priority parameter is dependent on the -
322 operating system's scheduling policy. In particular, the \a priority -
323 will be ignored on systems that do not support thread priorities -
324 (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler -
325 for more details). -
326 -
327 \sa run(), terminate() -
328*/ -
329 -
330/*! -
331 \fn void QThread::started() -
332 -
333 This signal is emitted from the associated thread when it starts executing, -
334 before the run() function is called. -
335 -
336 \sa finished() -
337*/ -
338 -
339/*! -
340 \fn void QThread::finished() -
341 -
342 This signal is emitted from the associated thread right before it finishes executing. -
343 -
344 When this signal is emitted, the event loop has already stopped running. -
345 No more events will be processed in the thread, except for deferred deletion events. -
346 This signal can be connected to QObject::deleteLater(), to free objects in that thread. -
347 -
348 \note If the associated thread was terminated using terminate(), it is undefined from -
349 which thread this signal is emitted. -
350 -
351 \sa started() -
352*/ -
353 -
354/*! -
355 \enum QThread::Priority -
356 -
357 This enum type indicates how the operating system should schedule -
358 newly created threads. -
359 -
360 \value IdlePriority scheduled only when no other threads are -
361 running. -
362 -
363 \value LowestPriority scheduled less often than LowPriority. -
364 \value LowPriority scheduled less often than NormalPriority. -
365 -
366 \value NormalPriority the default priority of the operating -
367 system. -
368 -
369 \value HighPriority scheduled more often than NormalPriority. -
370 \value HighestPriority scheduled more often than HighPriority. -
371 -
372 \value TimeCriticalPriority scheduled as often as possible. -
373 -
374 \value InheritPriority use the same priority as the creating -
375 thread. This is the default. -
376*/ -
377 -
378/*! -
379 Returns a pointer to a QThread which manages the currently -
380 executing thread. -
381*/ -
382QThread *QThread::currentThread() -
383{ -
384 QThreadData *data = QThreadData::current();
executed (the execution status of this line is deduced): QThreadData *data = QThreadData::current();
-
385 Q_ASSERT(data != 0);
executed (the execution status of this line is deduced): qt_noop();
-
386 return data->thread;
executed: return data->thread;
Execution Count:3695783
3695783
387} -
388 -
389/*! -
390 Constructs a new QThread to manage a new thread. The \a parent -
391 takes ownership of the QThread. The thread does not begin -
392 executing until start() is called. -
393 -
394 \sa start() -
395*/ -
396QThread::QThread(QObject *parent) -
397 : QObject(*(new QThreadPrivate), parent) -
398{ -
399 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
400 // fprintf(stderr, "QThreadData %p created for thread %p\n", d->data, this); -
401 d->data->thread = this;
executed (the execution status of this line is deduced): d->data->thread = this;
-
402}
executed: }
Execution Count:1641583
1641583
403 -
404/*! -
405 \internal -
406 */ -
407QThread::QThread(QThreadPrivate &dd, QObject *parent) -
408 : QObject(dd, parent) -
409{ -
410 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
411 // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); -
412 d->data->thread = this;
executed (the execution status of this line is deduced): d->data->thread = this;
-
413}
executed: }
Execution Count:18
18
414 -
415/*! -
416 Destroys the QThread. -
417 -
418 Note that deleting a QThread object will not stop the execution -
419 of the thread it manages. Deleting a running QThread (i.e. -
420 isFinished() returns false) will probably result in a program -
421 crash. Wait for the finished() signal before deleting the -
422 QThread. -
423*/ -
424QThread::~QThread() -
425{ -
426 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
427 { -
428 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
429 if (d->isInFinish) {
evaluated: d->isInFinish
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:1641753
14-1641753
430 locker.unlock();
executed (the execution status of this line is deduced): locker.unlock();
-
431 wait();
executed (the execution status of this line is deduced): wait();
-
432 locker.relock();
executed (the execution status of this line is deduced): locker.relock();
-
433 }
executed: }
Execution Count:14
14
434 if (d->running && !d->finished && !d->data->isAdopted)
partially evaluated: d->running
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1641767
never evaluated: !d->finished
never evaluated: !d->data->isAdopted
0-1641767
435 qWarning("QThread: Destroyed while thread is still running");
never executed: QMessageLogger("thread/qthread.cpp", 435, __PRETTY_FUNCTION__).warning("QThread: Destroyed while thread is still running");
0
436 -
437 d->data->thread = 0;
executed (the execution status of this line is deduced): d->data->thread = 0;
-
438 } -
439}
executed: }
Execution Count:1641767
1641767
440 -
441/*! -
442 Returns true if the thread is finished; otherwise returns false. -
443 -
444 \sa isRunning() -
445*/ -
446bool QThread::isFinished() const -
447{ -
448 Q_D(const QThread);
executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func();
-
449 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
450 return d->finished || d->isInFinish;
executed: return d->finished || d->isInFinish;
Execution Count:2825
2825
451} -
452 -
453/*! -
454 Returns true if the thread is running; otherwise returns false. -
455 -
456 \sa isFinished() -
457*/ -
458bool QThread::isRunning() const -
459{ -
460 Q_D(const QThread);
executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func();
-
461 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
462 return d->running && !d->isInFinish;
executed: return d->running && !d->isInFinish;
Execution Count:431
431
463} -
464 -
465/*! -
466 Sets the maximum stack size for the thread to \a stackSize. If \a -
467 stackSize is greater than zero, the maximum stack size is set to -
468 \a stackSize bytes, otherwise the maximum stack size is -
469 automatically determined by the operating system. -
470 -
471 \warning Most operating systems place minimum and maximum limits -
472 on thread stack sizes. The thread will fail to start if the stack -
473 size is outside these limits. -
474 -
475 \sa stackSize() -
476*/ -
477void QThread::setStackSize(uint stackSize) -
478{ -
479 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
480 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
481 Q_ASSERT_X(!d->running, "QThread::setStackSize",
executed (the execution status of this line is deduced): qt_noop();
-
482 "cannot change stack size while the thread is running"); -
483 d->stackSize = stackSize;
executed (the execution status of this line is deduced): d->stackSize = stackSize;
-
484}
executed: }
Execution Count:2
2
485 -
486/*! -
487 Returns the maximum stack size for the thread (if set with -
488 setStackSize()); otherwise returns zero. -
489 -
490 \sa setStackSize() -
491*/ -
492uint QThread::stackSize() const -
493{ -
494 Q_D(const QThread);
executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func();
-
495 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
496 return d->stackSize;
executed: return d->stackSize;
Execution Count:3
3
497} -
498 -
499/*! -
500 Enters the event loop and waits until exit() is called, returning the value -
501 that was passed to exit(). The value returned is 0 if exit() is called via -
502 quit(). -
503 -
504 It is necessary to call this function to start event handling. -
505 -
506 \sa quit(), exit() -
507*/ -
508int QThread::exec() -
509{ -
510 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
511 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
512 d->data->quitNow = false;
executed (the execution status of this line is deduced): d->data->quitNow = false;
-
513 if (d->exited) {
evaluated: d->exited
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:583
9-583
514 d->exited = false;
executed (the execution status of this line is deduced): d->exited = false;
-
515 return d->returnCode;
executed: return d->returnCode;
Execution Count:9
9
516 } -
517 locker.unlock();
executed (the execution status of this line is deduced): locker.unlock();
-
518 -
519 QEventLoop eventLoop;
executed (the execution status of this line is deduced): QEventLoop eventLoop;
-
520 int returnCode = eventLoop.exec();
executed (the execution status of this line is deduced): int returnCode = eventLoop.exec();
-
521 -
522 locker.relock();
executed (the execution status of this line is deduced): locker.relock();
-
523 d->exited = false;
executed (the execution status of this line is deduced): d->exited = false;
-
524 d->returnCode = -1;
executed (the execution status of this line is deduced): d->returnCode = -1;
-
525 return returnCode;
executed: return returnCode;
Execution Count:584
584
526} -
527 -
528/*! -
529 Tells the thread's event loop to exit with a return code. -
530 -
531 After calling this function, the thread leaves the event loop and -
532 returns from the call to QEventLoop::exec(). The -
533 QEventLoop::exec() function returns \a returnCode. -
534 -
535 By convention, a \a returnCode of 0 means success, any non-zero value -
536 indicates an error. -
537 -
538 Note that unlike the C library function of the same name, this -
539 function \e does return to the caller -- it is event processing -
540 that stops. -
541 -
542 No QEventLoops will be started anymore in this thread until -
543 QThread::exec() has been called again. If the eventloop in QThread::exec() -
544 is not running then the next call to QThread::exec() will also return -
545 immediately. -
546 -
547 \sa quit(), QEventLoop -
548*/ -
549void QThread::exit(int returnCode) -
550{ -
551 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
552 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
553 d->exited = true;
executed (the execution status of this line is deduced): d->exited = true;
-
554 d->returnCode = returnCode;
executed (the execution status of this line is deduced): d->returnCode = returnCode;
-
555 d->data->quitNow = true;
executed (the execution status of this line is deduced): d->data->quitNow = true;
-
556 for (int i = 0; i < d->data->eventLoops.size(); ++i) {
evaluated: i < d->data->eventLoops.size()
TRUEFALSE
yes
Evaluation Count:587
yes
Evaluation Count:603
587-603
557 QEventLoop *eventLoop = d->data->eventLoops.at(i);
executed (the execution status of this line is deduced): QEventLoop *eventLoop = d->data->eventLoops.at(i);
-
558 eventLoop->exit(returnCode);
executed (the execution status of this line is deduced): eventLoop->exit(returnCode);
-
559 }
executed: }
Execution Count:587
587
560}
executed: }
Execution Count:603
603
561 -
562/*! -
563 Tells the thread's event loop to exit with return code 0 (success). -
564 Equivalent to calling QThread::exit(0). -
565 -
566 This function does nothing if the thread does not have an event -
567 loop. -
568 -
569 \sa exit(), QEventLoop -
570*/ -
571void QThread::quit() -
572{ exit(); }
executed: exit();
Execution Count:552
552
573 -
574/*! -
575 The starting point for the thread. After calling start(), the -
576 newly created thread calls this function. The default -
577 implementation simply calls exec(). -
578 -
579 You can reimplement this function to facilitate advanced thread -
580 management. Returning from this method will end the execution of -
581 the thread. -
582 -
583 \sa start(), wait() -
584*/ -
585void QThread::run() -
586{ -
587 (void) exec();
executed (the execution status of this line is deduced): (void) exec();
-
588}
executed: }
Execution Count:529
529
589 -
590/*! \fn void QThread::setPriority(Priority priority) -
591 \since 4.1 -
592 -
593 This function sets the \a priority for a running thread. If the -
594 thread is not running, this function does nothing and returns -
595 immediately. Use start() to start a thread with a specific -
596 priority. -
597 -
598 The \a priority argument can be any value in the \c -
599 QThread::Priority enum except for \c InheritPriorty. -
600 -
601 The effect of the \a priority parameter is dependent on the -
602 operating system's scheduling policy. In particular, the \a priority -
603 will be ignored on systems that do not support thread priorities -
604 (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler -
605 for more details). -
606 -
607 \sa Priority, priority(), start() -
608*/ -
609 -
610/*! -
611 \since 4.1 -
612 -
613 Returns the priority for a running thread. If the thread is not -
614 running, this function returns \c InheritPriority. -
615 -
616 \sa Priority, setPriority(), start() -
617*/ -
618QThread::Priority QThread::priority() const -
619{ -
620 Q_D(const QThread);
executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func();
-
621 QMutexLocker locker(&d->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex);
-
622 -
623 // mask off the high bits that are used for flags -
624 return Priority(d->priority & 0xffff);
executed: return Priority(d->priority & 0xffff);
Execution Count:33
33
625} -
626 -
627/*! -
628 \fn void QThread::sleep(unsigned long secs) -
629 -
630 Forces the current thread to sleep for \a secs seconds. -
631 -
632 \sa msleep(), usleep() -
633*/ -
634 -
635/*! -
636 \fn void QThread::msleep(unsigned long msecs) -
637 -
638 Forces the current thread to sleep for \a msecs milliseconds. -
639 -
640 \sa sleep(), usleep() -
641*/ -
642 -
643/*! -
644 \fn void QThread::usleep(unsigned long usecs) -
645 -
646 Forces the current thread to sleep for \a usecs microseconds. -
647 -
648 \sa sleep(), msleep() -
649*/ -
650 -
651/*! -
652 \fn void QThread::terminate() -
653 -
654 Terminates the execution of the thread. The thread may or may not -
655 be terminated immediately, depending on the operating system's -
656 scheduling policies. Use QThread::wait() after terminate(), to be -
657 sure. -
658 -
659 When the thread is terminated, all threads waiting for the thread -
660 to finish will be woken up. -
661 -
662 \warning This function is dangerous and its use is discouraged. -
663 The thread can be terminated at any point in its code path. -
664 Threads can be terminated while modifying data. There is no -
665 chance for the thread to clean up after itself, unlock any held -
666 mutexes, etc. In short, use this function only if absolutely -
667 necessary. -
668 -
669 Termination can be explicitly enabled or disabled by calling -
670 QThread::setTerminationEnabled(). Calling this function while -
671 termination is disabled results in the termination being -
672 deferred, until termination is re-enabled. See the documentation -
673 of QThread::setTerminationEnabled() for more information. -
674 -
675 \sa setTerminationEnabled() -
676*/ -
677 -
678/*! -
679 \fn bool QThread::wait(unsigned long time) -
680 -
681 Blocks the thread until either of these conditions is met: -
682 -
683 \list -
684 \li The thread associated with this QThread object has finished -
685 execution (i.e. when it returns from \l{run()}). This function -
686 will return true if the thread has finished. It also returns -
687 true if the thread has not been started yet. -
688 \li \a time milliseconds has elapsed. If \a time is ULONG_MAX (the -
689 default), then the wait will never timeout (the thread must -
690 return from \l{run()}). This function will return false if the -
691 wait timed out. -
692 \endlist -
693 -
694 This provides similar functionality to the POSIX \c -
695 pthread_join() function. -
696 -
697 \sa sleep(), terminate() -
698*/ -
699 -
700/*! -
701 \fn void QThread::setTerminationEnabled(bool enabled) -
702 -
703 Enables or disables termination of the current thread based on the -
704 \a enabled parameter. The thread must have been started by -
705 QThread. -
706 -
707 When \a enabled is false, termination is disabled. Future calls -
708 to QThread::terminate() will return immediately without effect. -
709 Instead, the termination is deferred until termination is enabled. -
710 -
711 When \a enabled is true, termination is enabled. Future calls to -
712 QThread::terminate() will terminate the thread normally. If -
713 termination has been deferred (i.e. QThread::terminate() was -
714 called with termination disabled), this function will terminate -
715 the calling thread \e immediately. Note that this function will -
716 not return in this case. -
717 -
718 \sa terminate() -
719*/ -
720 -
721#else // QT_NO_THREAD -
722 -
723QThread::QThread(QObject *parent) -
724 : QObject(*(new QThreadPrivate), (QObject*)0){ -
725 Q_D(QThread); -
726 d->data->thread = this; -
727} -
728 -
729QThread *QThread::currentThread() -
730{ -
731 return QThreadData::current()->thread; -
732} -
733 -
734QThreadData* QThreadData::current() -
735{ -
736 static QThreadData *data = 0; // reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key)); -
737 if (!data) { -
738 QScopedPointer<QThreadData> newdata(new QThreadData); -
739 newdata->thread = new QAdoptedThread(newdata.data()); -
740 data = newdata.take(); -
741 data->deref(); -
742 } -
743 return data; -
744} -
745 -
746/*! -
747 \internal -
748 */ -
749QThread::QThread(QThreadPrivate &dd, QObject *parent) -
750 : QObject(dd, parent) -
751{ -
752 Q_D(QThread); -
753 // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); -
754 d->data->thread = this; -
755} -
756 -
757#endif // QT_NO_THREAD -
758 -
759/*! -
760 \since 5.0 -
761 -
762 Returns a pointer to the event dispatcher object for the thread. If no event -
763 dispatcher exists for the thread, this function returns 0. -
764*/ -
765QAbstractEventDispatcher *QThread::eventDispatcher() const -
766{ -
767 Q_D(const QThread);
executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func();
-
768 return d->data->eventDispatcher;
executed: return d->data->eventDispatcher;
Execution Count:6
6
769} -
770 -
771/*! -
772 \since 5.0 -
773 -
774 Sets the event dispatcher for the thread to \a eventDispatcher. This is -
775 only possible as long as there is no event dispatcher installed for the -
776 thread yet. That is, before the thread has been started with start() or, in -
777 case of the main thread, before QCoreApplication has been instantiated. -
778 This method takes ownership of the object. -
779*/ -
780void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) -
781{ -
782 Q_D(QThread);
executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func();
-
783 if (d->data->eventDispatcher != 0) {
partially evaluated: d->data->eventDispatcher != 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
784 qWarning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread");
never executed (the execution status of this line is deduced): QMessageLogger("thread/qthread.cpp", 784, __PRETTY_FUNCTION__).warning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread");
-
785 } else {
never executed: }
0
786 eventDispatcher->moveToThread(this);
executed (the execution status of this line is deduced): eventDispatcher->moveToThread(this);
-
787 if (eventDispatcher->thread() == this) // was the move successful?
partially evaluated: eventDispatcher->thread() == this
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
788 d->data->eventDispatcher = eventDispatcher;
executed: d->data->eventDispatcher = eventDispatcher;
Execution Count:2
2
789 else -
790 qWarning("QThread::setEventDispatcher: Could not move event dispatcher to target thread");
never executed: QMessageLogger("thread/qthread.cpp", 790, __PRETTY_FUNCTION__).warning("QThread::setEventDispatcher: Could not move event dispatcher to target thread");
0
791 } -
792} -
793 -
794/*! -
795 \reimp -
796*/ -
797bool QThread::event(QEvent *event) -
798{ -
799 if (event->type() == QEvent::Quit) {
evaluated: event->type() == QEvent::Quit
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:2078
1-2078
800 quit();
executed (the execution status of this line is deduced): quit();
-
801 return true;
executed: return true;
Execution Count:1
1
802 } else { -
803 return QObject::event(event);
executed: return QObject::event(event);
Execution Count:2078
2078
804 } -
805} -
806 -
807QT_END_NAMESPACE -
808 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial