Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 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 | | - |
54 | QT_BEGIN_NAMESPACE | - |
55 | | - |
56 | /* | - |
57 | QThreadData | - |
58 | */ | - |
59 | | - |
60 | QThreadData::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:1627245 | 1627245 |
66 | | - |
67 | QThreadData::~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 no Evaluation Count:0 | yes Evaluation Count:1627422 |
| 0-1627422 |
78 | QCoreApplicationPrivate::theMainThread = 0; never executed (the execution status of this line is deduced): QCoreApplicationPrivate::theMainThread = 0; | - |
79 | } | 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() yes Evaluation Count:106 | yes Evaluation Count:1627422 |
| 106-1627422 |
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 no Evaluation Count:0 | yes Evaluation Count:106 |
| 0-106 |
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 | } | 0 |
92 | } executed: } Execution Count:106 | 106 |
93 | | - |
94 | // fprintf(stderr, "QThreadData %p destroyed\n", this); | - |
95 | } executed: } Execution Count:1627422 | 1627422 |
96 | | - |
97 | void 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:5364903 | 5364903 |
104 | | - |
105 | void QThreadData::deref() | - |
106 | { | - |
107 | #ifndef QT_NO_THREAD | - |
108 | if (!_ref.deref()) evaluated: !_ref.deref() yes Evaluation Count:1627422 | yes Evaluation Count:5362763 |
| 1627422-5362763 |
109 | delete this; executed: delete this; Execution Count:1627422 | 1627422 |
110 | #endif | - |
111 | } executed: } Execution Count:6990175 | 6990175 |
112 | | - |
113 | /* | - |
114 | QAdoptedThread | - |
115 | */ | - |
116 | | - |
117 | QAdoptedThread::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 | | - |
131 | QAdoptedThread::~QAdoptedThread() | - |
132 | { | - |
133 | // fprintf(stderr, "~QAdoptedThread = %p\n", this); | - |
134 | } | - |
135 | | - |
136 | void 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 | } | 0 |
141 | #ifndef QT_NO_THREAD | - |
142 | /* | - |
143 | QThreadPrivate | - |
144 | */ | - |
145 | | - |
146 | QThreadPrivate::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 yes Evaluation Count:1627222 | yes Evaluation Count:18 |
| 18-1627222 |
164 | data = new QThreadData; executed: data = new QThreadData; Execution Count:1627222 | 1627222 |
165 | } executed: } Execution Count:1627240 | 1627240 |
166 | | - |
167 | QThreadPrivate::~QThreadPrivate() | - |
168 | { | - |
169 | data->deref(); executed (the execution status of this line is deduced): data->deref(); | - |
170 | } executed: } Execution Count:1627425 | 1627425 |
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. QThreads begin executing in run(). By default, run() starts the | - |
182 | event loop by calling exec() and runs a Qt event loop inside the thread. | - |
183 | | - |
184 | You can use worker objects by moving them to the thread using | - |
185 | QObject::moveToThread. | - |
186 | | - |
187 | \snippet code/src_corelib_thread_qthread.cpp worker | - |
188 | | - |
189 | The code inside the Worker's slot would then execute in a | - |
190 | separate thread. 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 | Another way to make code run in a separate thread, is to subclass QThread | - |
197 | and reimplement run(). For example: | - |
198 | | - |
199 | \snippet code/src_corelib_thread_qthread.cpp reimpl-run | - |
200 | | - |
201 | In that example, the thread will exit after the run function has returned. | - |
202 | There will not be any event loop running in the thread unless you call | - |
203 | exec(). | - |
204 | | - |
205 | It is important to remember that a QThread object usually lives | - |
206 | in the thread where it was created, not in the thread that it | - |
207 | manages. This oft-overlooked detail means that a QThread's slots | - |
208 | will be executed in the context of its home thread, not in the | - |
209 | context of the thread it is managing. For this reason, | - |
210 | implementing new slots in a QThread subclass is error-prone and | - |
211 | discouraged. | - |
212 | | - |
213 | \note If you interact with an object, using any technique other | - |
214 | than queued signal/slot connections (e.g. direct function calls), | - |
215 | then the usual multithreading precautions need to be taken. | - |
216 | | - |
217 | \note It is not possible to change the thread affinity of GUI | - |
218 | objects; they must remain in the main thread. | - |
219 | | - |
220 | \section1 Managing threads | - |
221 | | - |
222 | QThread will notifiy you via a signal when the thread is | - |
223 | started() and finished(), or you can use isFinished() and | - |
224 | isRunning() to query the state of the thread. | - |
225 | | - |
226 | You can stop the thread by calling exit() or quit(). In extreme | - |
227 | cases, you may want to forcibly terminate() an executing thread. | - |
228 | However, doing so is dangerous and discouraged. Please read the | - |
229 | documentation for terminate() and setTerminationEnabled() for | - |
230 | detailed information. | - |
231 | | - |
232 | From Qt 4.8 onwards, it is possible to deallocate objects that | - |
233 | live in a thread that has just ended, by connecting the | - |
234 | finished() signal to QObject::deleteLater(). | - |
235 | | - |
236 | Use wait() to block the calling thread, until the other thread | - |
237 | has finished execution (or until a specified time has passed). | - |
238 | | - |
239 | QThread also provides static, platform independent sleep | - |
240 | functions: sleep(), msleep(), and usleep() allow full second, | - |
241 | millisecond, and microsecond resolution respectively. These | - |
242 | functions were made public in Qt 5.0. | - |
243 | | - |
244 | \note wait() and the sleep() functions should be unnecessary in | - |
245 | general, since Qt is an event-driven framework. Instead of | - |
246 | wait(), consider listening for the finished() signal. Instead of | - |
247 | the sleep() functions, consider using QTimer. | - |
248 | | - |
249 | The static functions currentThreadId() and currentThread() return | - |
250 | identifiers for the currently executing thread. The former | - |
251 | returns a platform specific ID for the thread; the latter returns | - |
252 | a QThread pointer. | - |
253 | | - |
254 | To choose the name that your thread will be given (as identified | - |
255 | by the command \c{ps -L} on Linux, for example), you can call | - |
256 | \l{QObject::setObjectName()}{setObjectName()} before starting the thread. | - |
257 | If you don't call \l{QObject::setObjectName()}{setObjectName()}, | - |
258 | the name given to your thread will be the class name of the runtime | - |
259 | type of your thread object (for example, \c "RenderThread" in the case of the | - |
260 | \l{Mandelbrot Example}, as that is the name of the QThread subclass). | - |
261 | Note that this is currently not available with release builds on Windows. | - |
262 | | - |
263 | \sa {Thread Support in Qt}, QThreadStorage, QMutex, QSemaphore, QWaitCondition, | - |
264 | {Mandelbrot Example}, {Semaphores Example}, {Wait Conditions Example} | - |
265 | */ | - |
266 | | - |
267 | /*! | - |
268 | \fn Qt::HANDLE QThread::currentThreadId() | - |
269 | | - |
270 | Returns the thread handle of the currently executing thread. | - |
271 | | - |
272 | \warning The handle returned by this function is used for internal | - |
273 | purposes and should not be used in any application code. | - |
274 | | - |
275 | \warning On Windows, the returned value is a pseudo-handle for the | - |
276 | current thread. It can't be used for numerical comparison. i.e., | - |
277 | this function returns the DWORD (Windows-Thread ID) returned by | - |
278 | the Win32 function getCurrentThreadId(), not the HANDLE | - |
279 | (Windows-Thread HANDLE) returned by the Win32 function | - |
280 | getCurrentThread(). | - |
281 | */ | - |
282 | | - |
283 | /*! | - |
284 | \fn int QThread::idealThreadCount() | - |
285 | | - |
286 | Returns the ideal number of threads that can be run on the system. This is done querying | - |
287 | the number of processor cores, both real and logical, in the system. This function returns -1 | - |
288 | if the number of processor cores could not be detected. | - |
289 | */ | - |
290 | | - |
291 | /*! | - |
292 | \fn void QThread::yieldCurrentThread() | - |
293 | | - |
294 | Yields execution of the current thread to another runnable thread, | - |
295 | if any. Note that the operating system decides to which thread to | - |
296 | switch. | - |
297 | */ | - |
298 | | - |
299 | /*! | - |
300 | \fn void QThread::start(Priority priority) | - |
301 | | - |
302 | Begins execution of the thread by calling run(). The | - |
303 | operating system will schedule the thread according to the \a | - |
304 | priority parameter. If the thread is already running, this | - |
305 | function does nothing. | - |
306 | | - |
307 | The effect of the \a priority parameter is dependent on the | - |
308 | operating system's scheduling policy. In particular, the \a priority | - |
309 | will be ignored on systems that do not support thread priorities | - |
310 | (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler | - |
311 | for more details). | - |
312 | | - |
313 | \sa run(), terminate() | - |
314 | */ | - |
315 | | - |
316 | /*! | - |
317 | \fn void QThread::started() | - |
318 | | - |
319 | This signal is emitted from the associated thread when it starts executing, | - |
320 | before the run() function is called. | - |
321 | | - |
322 | \sa finished() | - |
323 | */ | - |
324 | | - |
325 | /*! | - |
326 | \fn void QThread::finished() | - |
327 | | - |
328 | This signal is emitted from the associated thread right before it finishes executing. | - |
329 | | - |
330 | When this signal is emitted, the event loop has already stopped running. | - |
331 | No more events will be processed in the thread, except for deferred deletion events. | - |
332 | This signal can be connected to QObject::deleteLater(), to free objects in that thread. | - |
333 | | - |
334 | \note If the associated thread was terminated using terminate(), it is undefined from | - |
335 | which thread this signal is emitted. | - |
336 | | - |
337 | \sa started() | - |
338 | */ | - |
339 | | - |
340 | /*! | - |
341 | \enum QThread::Priority | - |
342 | | - |
343 | This enum type indicates how the operating system should schedule | - |
344 | newly created threads. | - |
345 | | - |
346 | \value IdlePriority scheduled only when no other threads are | - |
347 | running. | - |
348 | | - |
349 | \value LowestPriority scheduled less often than LowPriority. | - |
350 | \value LowPriority scheduled less often than NormalPriority. | - |
351 | | - |
352 | \value NormalPriority the default priority of the operating | - |
353 | system. | - |
354 | | - |
355 | \value HighPriority scheduled more often than NormalPriority. | - |
356 | \value HighestPriority scheduled more often than HighPriority. | - |
357 | | - |
358 | \value TimeCriticalPriority scheduled as often as possible. | - |
359 | | - |
360 | \value InheritPriority use the same priority as the creating | - |
361 | thread. This is the default. | - |
362 | */ | - |
363 | | - |
364 | /*! | - |
365 | Returns a pointer to a QThread which manages the currently | - |
366 | executing thread. | - |
367 | */ | - |
368 | QThread *QThread::currentThread() | - |
369 | { | - |
370 | QThreadData *data = QThreadData::current(); executed (the execution status of this line is deduced): QThreadData *data = QThreadData::current(); | - |
371 | Q_ASSERT(data != 0); executed (the execution status of this line is deduced): qt_noop(); | - |
372 | return data->thread; executed: return data->thread; Execution Count:3732293 | 3732293 |
373 | } | - |
374 | | - |
375 | /*! | - |
376 | Constructs a new QThread to manage a new thread. The \a parent | - |
377 | takes ownership of the QThread. The thread does not begin | - |
378 | executing until start() is called. | - |
379 | | - |
380 | \sa start() | - |
381 | */ | - |
382 | QThread::QThread(QObject *parent) | - |
383 | : QObject(*(new QThreadPrivate), parent) | - |
384 | { | - |
385 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
386 | // fprintf(stderr, "QThreadData %p created for thread %p\n", d->data, this); | - |
387 | d->data->thread = this; executed (the execution status of this line is deduced): d->data->thread = this; | - |
388 | } executed: } Execution Count:1627222 | 1627222 |
389 | | - |
390 | /*! | - |
391 | \internal | - |
392 | */ | - |
393 | QThread::QThread(QThreadPrivate &dd, QObject *parent) | - |
394 | : QObject(dd, parent) | - |
395 | { | - |
396 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
397 | // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); | - |
398 | d->data->thread = this; executed (the execution status of this line is deduced): d->data->thread = this; | - |
399 | } executed: } Execution Count:18 | 18 |
400 | | - |
401 | /*! | - |
402 | Destroys the QThread. | - |
403 | | - |
404 | Note that deleting a QThread object will not stop the execution | - |
405 | of the thread it manages. Deleting a running QThread (i.e. | - |
406 | isFinished() returns false) will probably result in a program | - |
407 | crash. Wait for the finished() signal before deleting the | - |
408 | QThread. | - |
409 | */ | - |
410 | QThread::~QThread() | - |
411 | { | - |
412 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
413 | { | - |
414 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
415 | if (d->isInFinish) { evaluated: d->isInFinish yes Evaluation Count:17 | yes Evaluation Count:1627408 |
| 17-1627408 |
416 | locker.unlock(); executed (the execution status of this line is deduced): locker.unlock(); | - |
417 | wait(); executed (the execution status of this line is deduced): wait(); | - |
418 | locker.relock(); executed (the execution status of this line is deduced): locker.relock(); | - |
419 | } executed: } Execution Count:17 | 17 |
420 | if (d->running && !d->finished && !d->data->isAdopted) partially evaluated: d->running no Evaluation Count:0 | yes Evaluation Count:1627425 |
never evaluated: !d->finished never evaluated: !d->data->isAdopted | 0-1627425 |
421 | qWarning("QThread: Destroyed while thread is still running"); never executed: QMessageLogger("thread/qthread.cpp", 421, __PRETTY_FUNCTION__).warning("QThread: Destroyed while thread is still running"); | 0 |
422 | | - |
423 | d->data->thread = 0; executed (the execution status of this line is deduced): d->data->thread = 0; | - |
424 | } | - |
425 | } executed: } Execution Count:1627425 | 1627425 |
426 | | - |
427 | /*! | - |
428 | Returns true if the thread is finished; otherwise returns false. | - |
429 | | - |
430 | \sa isRunning() | - |
431 | */ | - |
432 | bool QThread::isFinished() const | - |
433 | { | - |
434 | Q_D(const QThread); executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func(); | - |
435 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
436 | return d->finished || d->isInFinish; executed: return d->finished || d->isInFinish; Execution Count:3029 | 3029 |
437 | } | - |
438 | | - |
439 | /*! | - |
440 | Returns true if the thread is running; otherwise returns false. | - |
441 | | - |
442 | \sa isFinished() | - |
443 | */ | - |
444 | bool QThread::isRunning() const | - |
445 | { | - |
446 | Q_D(const QThread); executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func(); | - |
447 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
448 | return d->running && !d->isInFinish; executed: return d->running && !d->isInFinish; Execution Count:460 | 460 |
449 | } | - |
450 | | - |
451 | /*! | - |
452 | Sets the maximum stack size for the thread to \a stackSize. If \a | - |
453 | stackSize is greater than zero, the maximum stack size is set to | - |
454 | \a stackSize bytes, otherwise the maximum stack size is | - |
455 | automatically determined by the operating system. | - |
456 | | - |
457 | \warning Most operating systems place minimum and maximum limits | - |
458 | on thread stack sizes. The thread will fail to start if the stack | - |
459 | size is outside these limits. | - |
460 | | - |
461 | \sa stackSize() | - |
462 | */ | - |
463 | void QThread::setStackSize(uint stackSize) | - |
464 | { | - |
465 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
466 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
467 | Q_ASSERT_X(!d->running, "QThread::setStackSize", executed (the execution status of this line is deduced): qt_noop(); | - |
468 | "cannot change stack size while the thread is running"); | - |
469 | d->stackSize = stackSize; executed (the execution status of this line is deduced): d->stackSize = stackSize; | - |
470 | } executed: } Execution Count:2 | 2 |
471 | | - |
472 | /*! | - |
473 | Returns the maximum stack size for the thread (if set with | - |
474 | setStackSize()); otherwise returns zero. | - |
475 | | - |
476 | \sa setStackSize() | - |
477 | */ | - |
478 | uint QThread::stackSize() const | - |
479 | { | - |
480 | Q_D(const QThread); executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func(); | - |
481 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
482 | return d->stackSize; executed: return d->stackSize; Execution Count:3 | 3 |
483 | } | - |
484 | | - |
485 | /*! | - |
486 | Enters the event loop and waits until exit() is called, returning the value | - |
487 | that was passed to exit(). The value returned is 0 if exit() is called via | - |
488 | quit(). | - |
489 | | - |
490 | It is necessary to call this function to start event handling. | - |
491 | | - |
492 | \sa quit(), exit() | - |
493 | */ | - |
494 | int QThread::exec() | - |
495 | { | - |
496 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
497 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
498 | d->data->quitNow = false; executed (the execution status of this line is deduced): d->data->quitNow = false; | - |
499 | if (d->exited) { evaluated: d->exited yes Evaluation Count:5 | yes Evaluation Count:626 |
| 5-626 |
500 | d->exited = false; executed (the execution status of this line is deduced): d->exited = false; | - |
501 | return d->returnCode; executed: return d->returnCode; Execution Count:5 | 5 |
502 | } | - |
503 | locker.unlock(); executed (the execution status of this line is deduced): locker.unlock(); | - |
504 | | - |
505 | QEventLoop eventLoop; executed (the execution status of this line is deduced): QEventLoop eventLoop; | - |
506 | int returnCode = eventLoop.exec(); executed (the execution status of this line is deduced): int returnCode = eventLoop.exec(); | - |
507 | | - |
508 | locker.relock(); executed (the execution status of this line is deduced): locker.relock(); | - |
509 | d->exited = false; executed (the execution status of this line is deduced): d->exited = false; | - |
510 | d->returnCode = -1; executed (the execution status of this line is deduced): d->returnCode = -1; | - |
511 | return returnCode; executed: return returnCode; Execution Count:627 | 627 |
512 | } | - |
513 | | - |
514 | /*! | - |
515 | Tells the thread's event loop to exit with a return code. | - |
516 | | - |
517 | After calling this function, the thread leaves the event loop and | - |
518 | returns from the call to QEventLoop::exec(). The | - |
519 | QEventLoop::exec() function returns \a returnCode. | - |
520 | | - |
521 | By convention, a \a returnCode of 0 means success, any non-zero value | - |
522 | indicates an error. | - |
523 | | - |
524 | Note that unlike the C library function of the same name, this | - |
525 | function \e does return to the caller -- it is event processing | - |
526 | that stops. | - |
527 | | - |
528 | No QEventLoops will be started anymore in this thread until | - |
529 | QThread::exec() has been called again. If the eventloop in QThread::exec() | - |
530 | is not running then the next call to QThread::exec() will also return | - |
531 | immediately. | - |
532 | | - |
533 | \sa quit(), QEventLoop | - |
534 | */ | - |
535 | void QThread::exit(int returnCode) | - |
536 | { | - |
537 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
538 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
539 | d->exited = true; executed (the execution status of this line is deduced): d->exited = true; | - |
540 | d->returnCode = returnCode; executed (the execution status of this line is deduced): d->returnCode = returnCode; | - |
541 | d->data->quitNow = true; executed (the execution status of this line is deduced): d->data->quitNow = true; | - |
542 | for (int i = 0; i < d->data->eventLoops.size(); ++i) { evaluated: i < d->data->eventLoops.size() yes Evaluation Count:628 | yes Evaluation Count:642 |
| 628-642 |
543 | QEventLoop *eventLoop = d->data->eventLoops.at(i); executed (the execution status of this line is deduced): QEventLoop *eventLoop = d->data->eventLoops.at(i); | - |
544 | eventLoop->exit(returnCode); executed (the execution status of this line is deduced): eventLoop->exit(returnCode); | - |
545 | } executed: } Execution Count:628 | 628 |
546 | } executed: } Execution Count:642 | 642 |
547 | | - |
548 | /*! | - |
549 | Tells the thread's event loop to exit with return code 0 (success). | - |
550 | Equivalent to calling QThread::exit(0). | - |
551 | | - |
552 | This function does nothing if the thread does not have an event | - |
553 | loop. | - |
554 | | - |
555 | \sa exit(), QEventLoop | - |
556 | */ | - |
557 | void QThread::quit() | - |
558 | { exit(); } executed: exit(); Execution Count:591 | 591 |
559 | | - |
560 | /*! | - |
561 | The starting point for the thread. After calling start(), the | - |
562 | newly created thread calls this function. The default | - |
563 | implementation simply calls exec(). | - |
564 | | - |
565 | You can reimplement this function to facilitate advanced thread | - |
566 | management. Returning from this method will end the execution of | - |
567 | the thread. | - |
568 | | - |
569 | \sa start(), wait() | - |
570 | */ | - |
571 | void QThread::run() | - |
572 | { | - |
573 | (void) exec(); executed (the execution status of this line is deduced): (void) exec(); | - |
574 | } executed: } Execution Count:568 | 568 |
575 | | - |
576 | /*! \fn void QThread::setPriority(Priority priority) | - |
577 | \since 4.1 | - |
578 | | - |
579 | This function sets the \a priority for a running thread. If the | - |
580 | thread is not running, this function does nothing and returns | - |
581 | immediately. Use start() to start a thread with a specific | - |
582 | priority. | - |
583 | | - |
584 | The \a priority argument can be any value in the \c | - |
585 | QThread::Priority enum except for \c InheritPriorty. | - |
586 | | - |
587 | The effect of the \a priority parameter is dependent on the | - |
588 | operating system's scheduling policy. In particular, the \a priority | - |
589 | will be ignored on systems that do not support thread priorities | - |
590 | (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler | - |
591 | for more details). | - |
592 | | - |
593 | \sa Priority, priority(), start() | - |
594 | */ | - |
595 | | - |
596 | /*! | - |
597 | \since 4.1 | - |
598 | | - |
599 | Returns the priority for a running thread. If the thread is not | - |
600 | running, this function returns \c InheritPriority. | - |
601 | | - |
602 | \sa Priority, setPriority(), start() | - |
603 | */ | - |
604 | QThread::Priority QThread::priority() const | - |
605 | { | - |
606 | Q_D(const QThread); executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func(); | - |
607 | QMutexLocker locker(&d->mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&d->mutex); | - |
608 | | - |
609 | // mask off the high bits that are used for flags | - |
610 | return Priority(d->priority & 0xffff); executed: return Priority(d->priority & 0xffff); Execution Count:33 | 33 |
611 | } | - |
612 | | - |
613 | /*! | - |
614 | \fn void QThread::sleep(unsigned long secs) | - |
615 | | - |
616 | Forces the current thread to sleep for \a secs seconds. | - |
617 | | - |
618 | \sa msleep(), usleep() | - |
619 | */ | - |
620 | | - |
621 | /*! | - |
622 | \fn void QThread::msleep(unsigned long msecs) | - |
623 | | - |
624 | Forces the current thread to sleep for \a msecs milliseconds. | - |
625 | | - |
626 | \sa sleep(), usleep() | - |
627 | */ | - |
628 | | - |
629 | /*! | - |
630 | \fn void QThread::usleep(unsigned long usecs) | - |
631 | | - |
632 | Forces the current thread to sleep for \a usecs microseconds. | - |
633 | | - |
634 | \sa sleep(), msleep() | - |
635 | */ | - |
636 | | - |
637 | /*! | - |
638 | \fn void QThread::terminate() | - |
639 | | - |
640 | Terminates the execution of the thread. The thread may or may not | - |
641 | be terminated immediately, depending on the operating system's | - |
642 | scheduling policies. Use QThread::wait() after terminate(), to be | - |
643 | sure. | - |
644 | | - |
645 | When the thread is terminated, all threads waiting for the thread | - |
646 | to finish will be woken up. | - |
647 | | - |
648 | \warning This function is dangerous and its use is discouraged. | - |
649 | The thread can be terminated at any point in its code path. | - |
650 | Threads can be terminated while modifying data. There is no | - |
651 | chance for the thread to clean up after itself, unlock any held | - |
652 | mutexes, etc. In short, use this function only if absolutely | - |
653 | necessary. | - |
654 | | - |
655 | Termination can be explicitly enabled or disabled by calling | - |
656 | QThread::setTerminationEnabled(). Calling this function while | - |
657 | termination is disabled results in the termination being | - |
658 | deferred, until termination is re-enabled. See the documentation | - |
659 | of QThread::setTerminationEnabled() for more information. | - |
660 | | - |
661 | \sa setTerminationEnabled() | - |
662 | */ | - |
663 | | - |
664 | /*! | - |
665 | \fn bool QThread::wait(unsigned long time) | - |
666 | | - |
667 | Blocks the thread until either of these conditions is met: | - |
668 | | - |
669 | \list | - |
670 | \li The thread associated with this QThread object has finished | - |
671 | execution (i.e. when it returns from \l{run()}). This function | - |
672 | will return true if the thread has finished. It also returns | - |
673 | true if the thread has not been started yet. | - |
674 | \li \a time milliseconds has elapsed. If \a time is ULONG_MAX (the | - |
675 | default), then the wait will never timeout (the thread must | - |
676 | return from \l{run()}). This function will return false if the | - |
677 | wait timed out. | - |
678 | \endlist | - |
679 | | - |
680 | This provides similar functionality to the POSIX \c | - |
681 | pthread_join() function. | - |
682 | | - |
683 | \sa sleep(), terminate() | - |
684 | */ | - |
685 | | - |
686 | /*! | - |
687 | \fn void QThread::setTerminationEnabled(bool enabled) | - |
688 | | - |
689 | Enables or disables termination of the current thread based on the | - |
690 | \a enabled parameter. The thread must have been started by | - |
691 | QThread. | - |
692 | | - |
693 | When \a enabled is false, termination is disabled. Future calls | - |
694 | to QThread::terminate() will return immediately without effect. | - |
695 | Instead, the termination is deferred until termination is enabled. | - |
696 | | - |
697 | When \a enabled is true, termination is enabled. Future calls to | - |
698 | QThread::terminate() will terminate the thread normally. If | - |
699 | termination has been deferred (i.e. QThread::terminate() was | - |
700 | called with termination disabled), this function will terminate | - |
701 | the calling thread \e immediately. Note that this function will | - |
702 | not return in this case. | - |
703 | | - |
704 | \sa terminate() | - |
705 | */ | - |
706 | | - |
707 | #else // QT_NO_THREAD | - |
708 | | - |
709 | QThread::QThread(QObject *parent) | - |
710 | : QObject(*(new QThreadPrivate), (QObject*)0){ | - |
711 | Q_D(QThread); | - |
712 | d->data->thread = this; | - |
713 | } | - |
714 | | - |
715 | QThread *QThread::currentThread() | - |
716 | { | - |
717 | return QThreadData::current()->thread; | - |
718 | } | - |
719 | | - |
720 | QThreadData* QThreadData::current() | - |
721 | { | - |
722 | static QThreadData *data = 0; // reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key)); | - |
723 | if (!data) { | - |
724 | QScopedPointer<QThreadData> newdata(new QThreadData); | - |
725 | newdata->thread = new QAdoptedThread(newdata.data()); | - |
726 | data = newdata.take(); | - |
727 | data->deref(); | - |
728 | } | - |
729 | return data; | - |
730 | } | - |
731 | | - |
732 | /*! | - |
733 | \internal | - |
734 | */ | - |
735 | QThread::QThread(QThreadPrivate &dd, QObject *parent) | - |
736 | : QObject(dd, parent) | - |
737 | { | - |
738 | Q_D(QThread); | - |
739 | // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); | - |
740 | d->data->thread = this; | - |
741 | } | - |
742 | | - |
743 | #endif // QT_NO_THREAD | - |
744 | | - |
745 | /*! | - |
746 | \since 5.0 | - |
747 | | - |
748 | Returns a pointer to the event dispatcher object for the thread. If no event | - |
749 | dispatcher exists for the thread, this function returns 0. | - |
750 | */ | - |
751 | QAbstractEventDispatcher *QThread::eventDispatcher() const | - |
752 | { | - |
753 | Q_D(const QThread); executed (the execution status of this line is deduced): const QThreadPrivate * const d = d_func(); | - |
754 | return d->data->eventDispatcher; executed: return d->data->eventDispatcher; Execution Count:6 | 6 |
755 | } | - |
756 | | - |
757 | /*! | - |
758 | \since 5.0 | - |
759 | | - |
760 | Sets the event dispatcher for the thread to \a eventDispatcher. This is | - |
761 | only possible as long as there is no event dispatcher installed for the | - |
762 | thread yet. That is, before the thread has been started with start() or, in | - |
763 | case of the main thread, before QCoreApplication has been instantiated. | - |
764 | This method takes ownership of the object. | - |
765 | */ | - |
766 | void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) | - |
767 | { | - |
768 | Q_D(QThread); executed (the execution status of this line is deduced): QThreadPrivate * const d = d_func(); | - |
769 | if (d->data->eventDispatcher != 0) { partially evaluated: d->data->eventDispatcher != 0 no Evaluation Count:0 | yes Evaluation Count:2 |
| 0-2 |
770 | 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", 770, __PRETTY_FUNCTION__).warning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread"); | - |
771 | } else { | 0 |
772 | eventDispatcher->moveToThread(this); executed (the execution status of this line is deduced): eventDispatcher->moveToThread(this); | - |
773 | if (eventDispatcher->thread() == this) // was the move successful? partially evaluated: eventDispatcher->thread() == this yes Evaluation Count:2 | no Evaluation Count:0 |
| 0-2 |
774 | d->data->eventDispatcher = eventDispatcher; executed: d->data->eventDispatcher = eventDispatcher; Execution Count:2 | 2 |
775 | else | - |
776 | qWarning("QThread::setEventDispatcher: Could not move event dispatcher to target thread"); never executed: QMessageLogger("thread/qthread.cpp", 776, __PRETTY_FUNCTION__).warning("QThread::setEventDispatcher: Could not move event dispatcher to target thread"); | 0 |
777 | } | - |
778 | } | - |
779 | | - |
780 | /*! | - |
781 | \reimp | - |
782 | */ | - |
783 | bool QThread::event(QEvent *event) | - |
784 | { | - |
785 | if (event->type() == QEvent::Quit) { evaluated: event->type() == QEvent::Quit yes Evaluation Count:1 | yes Evaluation Count:2102 |
| 1-2102 |
786 | quit(); executed (the execution status of this line is deduced): quit(); | - |
787 | return true; executed: return true; Execution Count:1 | 1 |
788 | } else { | - |
789 | return QObject::event(event); executed: return QObject::event(event); Execution Count:2102 | 2102 |
790 | } | - |
791 | } | - |
792 | | - |
793 | QT_END_NAMESPACE | - |
794 | | - |
| | |