| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qthread.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count |
|---|---|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - |
| 4 | ** Copyright (C) 2016 Intel Corporation. | - |
| 5 | ** Contact: https://www.qt.io/licensing/ | - |
| 6 | ** | - |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
| 8 | ** | - |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 10 | ** Commercial License Usage | - |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 12 | ** accordance with the commercial license agreement provided with the | - |
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - |
| 17 | ** | - |
| 18 | ** GNU Lesser General Public License Usage | - |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 20 | ** General Public License version 3 as published by the Free Software | - |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - |
| 22 | ** packaging of this file. Please review the following information to | - |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - |
| 25 | ** | - |
| 26 | ** GNU General Public License Usage | - |
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - |
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - |
| 32 | ** included in the packaging of this file. Please review the following | - |
| 33 | ** information to ensure the GNU General Public License requirements will | - |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - |
| 36 | ** | - |
| 37 | ** $QT_END_LICENSE$ | - |
| 38 | ** | - |
| 39 | ****************************************************************************/ | - |
| 40 | - | |
| 41 | #include "qthread.h" | - |
| 42 | #include "qthreadstorage.h" | - |
| 43 | #include "qmutex.h" | - |
| 44 | #include "qreadwritelock.h" | - |
| 45 | #include "qabstracteventdispatcher.h" | - |
| 46 | - | |
| 47 | #include <qeventloop.h> | - |
| 48 | - | |
| 49 | #include "qthread_p.h" | - |
| 50 | #include "private/qcoreapplication_p.h" | - |
| 51 | - | |
| 52 | QT_BEGIN_NAMESPACE | - |
| 53 | - | |
| 54 | /* | - |
| 55 | QThreadData | - |
| 56 | */ | - |
| 57 | - | |
| 58 | QThreadData::QThreadData(int initialRefCount) | - |
| 59 | : _ref(initialRefCount), loopLevel(0), scopeLevel(0), thread(0), threadId(0), | - |
| 60 | eventDispatcher(0), | - |
| 61 | quitNow(false), canWait(true), isAdopted(false), requiresCoreApplication(true) | - |
| 62 | { | - |
| 63 | // fprintf(stderr, "QThreadData %p created\n", this); | - |
| 64 | } executed 582058 times by 680 tests: end of blockExecuted by:
| 582058 |
| 65 | - | |
| 66 | QThreadData::~QThreadData() | - |
| 67 | { | - |
| 68 | Q_ASSERT(_ref.load() == 0); | - |
| 69 | - | |
| 70 | // In the odd case that Qt is running on a secondary thread, the main | - |
| 71 | // thread instance will have been dereffed asunder because of the deref in | - |
| 72 | // QThreadData::current() and the deref in the pthread_destroy. To avoid | - |
| 73 | // crashing during QCoreApplicationData's global static cleanup we need to | - |
| 74 | // safeguard the main thread here.. This fix is a bit crude, but it solves | - |
| 75 | // the problem... | - |
| 76 | if (this->thread == QCoreApplicationPrivate::theMainThread) { | - |
| 77 | QCoreApplicationPrivate::theMainThread = 0; | - |
| 78 | QThreadData::clearCurrentThreadData(); | - |
| 79 | } | - |
| 80 | - | |
| 81 | QThread *t = thread; | - |
| 82 | thread = 0; | - |
| 83 | delete t; | - |
| 84 | - | |
| 85 | for (int i = 0; i < postEventList.size(); ++i) { | - |
| 86 | const QPostEvent &pe = postEventList.at(i); | - |
| 87 | if (pe.event) { | - |
| 88 | --pe.receiver->d_func()->postedEvents; | - |
| 89 | pe.event->posted = false; | - |
| 90 | delete pe.event; | - |
| 91 | } | - |
| 92 | } | - |
| 93 | - | |
| 94 | // fprintf(stderr, "QThreadData %p destroyed\n", this); | - |
| 95 | } | - |
| 96 | - | |
| 97 | void QThreadData::ref() | - |
| 98 | { | - |
| 99 | #ifndef QT_NO_THREAD | - |
| 100 | (void) _ref.ref(); | - |
| 101 | Q_ASSERT(_ref.load() != 0); | - |
| 102 | #endif | - |
| 103 | } | - |
| 104 | - | |
| 105 | void QThreadData::deref() | - |
| 106 | { | - |
| 107 | #ifndef QT_NO_THREAD | - |
| 108 | if (!_ref.deref()) | - |
| 109 | delete this; | - |
| 110 | #endif | - |
| 111 | } | - |
| 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; | - |
| 124 | d_func()->finished = false; | - |
| 125 | init(); | - |
| 126 | #endif | - |
| 127 | - | |
| 128 | // fprintf(stderr, "new QAdoptedThread = %p\n", this); | - |
| 129 | } | - |
| 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."); | - |
| 140 | } | - |
| 141 | #ifndef QT_NO_THREAD | - |
| 142 | /* | - |
| 143 | QThreadPrivate | - |
| 144 | */ | - |
| 145 | - | |
| 146 | QThreadPrivate::QThreadPrivate(QThreadData *d) | - |
| 147 | : QObjectPrivate(), running(false), finished(false), | - |
| 148 | isInFinish(false), interruptionRequested(false), | - |
| 149 | exited(false), returnCode(-1), | - |
| 150 | stackSize(0), priority(QThread::InheritPriority), data(d) | - |
| 151 | { | - |
| 152 | #if defined (Q_OS_WIN) | - |
| 153 | handle = 0; | - |
| 154 | # ifndef Q_OS_WINRT | - |
| 155 | id = 0; | - |
| 156 | # endif | - |
| 157 | waiters = 0; | - |
| 158 | terminationEnabled = true; | - |
| 159 | terminatePending = false; | - |
| 160 | #endif | - |
| 161 | - | |
| 162 | if (!data) | - |
| 163 | data = new QThreadData; | - |
| 164 | } | - |
| 165 | - | |
| 166 | QThreadPrivate::~QThreadPrivate() | - |
| 167 | { | - |
| 168 | data->deref(); | - |
| 169 | } | - |
| 170 | - | |
| 171 | /*! | - |
| 172 | \class QThread | - |
| 173 | \inmodule QtCore | - |
| 174 | \brief The QThread class provides a platform-independent way to | - |
| 175 | manage threads. | - |
| 176 | - | |
| 177 | \ingroup thread | - |
| 178 | - | |
| 179 | A QThread object manages one thread of control within the | - |
| 180 | program. QThreads begin executing in run(). By default, run() starts the | - |
| 181 | event loop by calling exec() and runs a Qt event loop inside the thread. | - |
| 182 | - | |
| 183 | You can use worker objects by moving them to the thread using | - |
| 184 | QObject::moveToThread(). | - |
| 185 | - | |
| 186 | \snippet code/src_corelib_thread_qthread.cpp worker | - |
| 187 | - | |
| 188 | The code inside the Worker's slot would then execute in a | - |
| 189 | separate thread. However, you are free to connect the | - |
| 190 | Worker's slots to any signal, from any object, in any thread. It | - |
| 191 | is safe to connect signals and slots across different threads, | - |
| 192 | thanks to a mechanism called \l{Qt::QueuedConnection}{queued | - |
| 193 | connections}. | - |
| 194 | - | |
| 195 | Another way to make code run in a separate thread, is to subclass QThread | - |
| 196 | and reimplement run(). For example: | - |
| 197 | - | |
| 198 | \snippet code/src_corelib_thread_qthread.cpp reimpl-run | - |
| 199 | - | |
| 200 | In that example, the thread will exit after the run function has returned. | - |
| 201 | There will not be any event loop running in the thread unless you call | - |
| 202 | exec(). | - |
| 203 | - | |
| 204 | It is important to remember that a QThread instance \l{QObject#Thread | - |
| 205 | Affinity}{lives in} the old thread that instantiated it, not in the | - |
| 206 | new thread that calls run(). This means that all of QThread's queued | - |
| 207 | slots will execute in the old thread. Thus, a developer who wishes to | - |
| 208 | invoke slots in the new thread must use the worker-object approach; new | - |
| 209 | slots should not be implemented directly into a subclassed QThread. | - |
| 210 | - | |
| 211 | When subclassing QThread, keep in mind that the constructor executes in | - |
| 212 | the old thread while run() executes in the new thread. If a member | - |
| 213 | variable is accessed from both functions, then the variable is accessed | - |
| 214 | from two different threads. Check that it is safe to do so. | - |
| 215 | - | |
| 216 | \note Care must be taken when interacting with objects across different | - |
| 217 | threads. See \l{Synchronizing Threads} for details. | - |
| 218 | - | |
| 219 | \section1 Managing Threads | - |
| 220 | - | |
| 221 | QThread will notifiy you via a signal when the thread is | - |
| 222 | started() and finished(), or you can use isFinished() and | - |
| 223 | isRunning() to query the state of the thread. | - |
| 224 | - | |
| 225 | You can stop the thread by calling exit() or quit(). In extreme | - |
| 226 | cases, you may want to forcibly terminate() an executing thread. | - |
| 227 | However, doing so is dangerous and discouraged. Please read the | - |
| 228 | documentation for terminate() and setTerminationEnabled() for | - |
| 229 | detailed information. | - |
| 230 | - | |
| 231 | From Qt 4.8 onwards, it is possible to deallocate objects that | - |
| 232 | live in a thread that has just ended, by connecting the | - |
| 233 | finished() signal to QObject::deleteLater(). | - |
| 234 | - | |
| 235 | Use wait() to block the calling thread, until the other thread | - |
| 236 | has finished execution (or until a specified time has passed). | - |
| 237 | - | |
| 238 | QThread also provides static, platform independent sleep | - |
| 239 | functions: sleep(), msleep(), and usleep() allow full second, | - |
| 240 | millisecond, and microsecond resolution respectively. These | - |
| 241 | functions were made public in Qt 5.0. | - |
| 242 | - | |
| 243 | \note wait() and the sleep() functions should be unnecessary in | - |
| 244 | general, since Qt is an event-driven framework. Instead of | - |
| 245 | wait(), consider listening for the finished() signal. Instead of | - |
| 246 | the sleep() functions, consider using QTimer. | - |
| 247 | - | |
| 248 | The static functions currentThreadId() and currentThread() return | - |
| 249 | identifiers for the currently executing thread. The former | - |
| 250 | returns a platform specific ID for the thread; the latter returns | - |
| 251 | a QThread pointer. | - |
| 252 | - | |
| 253 | To choose the name that your thread will be given (as identified | - |
| 254 | by the command \c{ps -L} on Linux, for example), you can call | - |
| 255 | \l{QObject::setObjectName()}{setObjectName()} before starting the thread. | - |
| 256 | If you don't call \l{QObject::setObjectName()}{setObjectName()}, | - |
| 257 | the name given to your thread will be the class name of the runtime | - |
| 258 | type of your thread object (for example, \c "RenderThread" in the case of the | - |
| 259 | \l{Mandelbrot Example}, as that is the name of the QThread subclass). | - |
| 260 | Note that this is currently not available with release builds on Windows. | - |
| 261 | - | |
| 262 | \sa {Thread Support in Qt}, QThreadStorage, {Synchronizing Threads}, | - |
| 263 | {Mandelbrot Example}, {Semaphores Example}, {Wait Conditions Example} | - |
| 264 | */ | - |
| 265 | - | |
| 266 | /*! | - |
| 267 | \fn Qt::HANDLE QThread::currentThreadId() | - |
| 268 | - | |
| 269 | Returns the thread handle of the currently executing thread. | - |
| 270 | - | |
| 271 | \warning The handle returned by this function is used for internal | - |
| 272 | purposes and should not be used in any application code. | - |
| 273 | - | |
| 274 | \warning On Windows, the returned value is a pseudo-handle for the | - |
| 275 | current thread. It can't be used for numerical comparison. i.e., | - |
| 276 | this function returns the DWORD (Windows-Thread ID) returned by | - |
| 277 | the Win32 function getCurrentThreadId(), not the HANDLE | - |
| 278 | (Windows-Thread HANDLE) returned by the Win32 function | - |
| 279 | getCurrentThread(). | - |
| 280 | */ | - |
| 281 | - | |
| 282 | /*! | - |
| 283 | \fn int QThread::idealThreadCount() | - |
| 284 | - | |
| 285 | Returns the ideal number of threads that can be run on the system. This is done querying | - |
| 286 | the number of processor cores, both real and logical, in the system. This function returns -1 | - |
| 287 | if the number of processor cores could not be detected. | - |
| 288 | */ | - |
| 289 | - | |
| 290 | /*! | - |
| 291 | \fn void QThread::yieldCurrentThread() | - |
| 292 | - | |
| 293 | Yields execution of the current thread to another runnable thread, | - |
| 294 | if any. Note that the operating system decides to which thread to | - |
| 295 | switch. | - |
| 296 | */ | - |
| 297 | - | |
| 298 | /*! | - |
| 299 | \fn void QThread::start(Priority priority) | - |
| 300 | - | |
| 301 | Begins execution of the thread by calling run(). The | - |
| 302 | operating system will schedule the thread according to the \a | - |
| 303 | priority parameter. If the thread is already running, this | - |
| 304 | function does nothing. | - |
| 305 | - | |
| 306 | The effect of the \a priority parameter is dependent on the | - |
| 307 | operating system's scheduling policy. In particular, the \a priority | - |
| 308 | will be ignored on systems that do not support thread priorities | - |
| 309 | (such as on Linux, see the | - |
| 310 | \l {http://linux.die.net/man/2/sched_setscheduler}{sched_setscheduler} | - |
| 311 | documentation 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(); | - |
| 371 | Q_ASSERT(data != 0); | - |
| 372 | return data->thread; | - |
| 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); | - |
| 386 | // fprintf(stderr, "QThreadData %p created for thread %p\n", d->data, this); | - |
| 387 | d->data->thread = this; | - |
| 388 | } | - |
| 389 | - | |
| 390 | /*! | - |
| 391 | \internal | - |
| 392 | */ | - |
| 393 | QThread::QThread(QThreadPrivate &dd, QObject *parent) | - |
| 394 | : QObject(dd, parent) | - |
| 395 | { | - |
| 396 | Q_D(QThread); | - |
| 397 | // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); | - |
| 398 | d->data->thread = this; | - |
| 399 | } | - |
| 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 \c 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); | - |
| 413 | { | - |
| 414 | QMutexLocker locker(&d->mutex); | - |
| 415 | if (d->isInFinish) { | - |
| 416 | locker.unlock(); | - |
| 417 | wait(); | - |
| 418 | locker.relock(); | - |
| 419 | } | - |
| 420 | if (d->running && !d->finished && !d->data->isAdopted) | - |
| 421 | qWarning("QThread: Destroyed while thread is still running"); | - |
| 422 | - | |
| 423 | d->data->thread = 0; | - |
| 424 | } | - |
| 425 | } | - |
| 426 | - | |
| 427 | /*! | - |
| 428 | Returns \c true if the thread is finished; otherwise returns \c false. | - |
| 429 | - | |
| 430 | \sa isRunning() | - |
| 431 | */ | - |
| 432 | bool QThread::isFinished() const | - |
| 433 | { | - |
| 434 | Q_D(const QThread); | - |
| 435 | QMutexLocker locker(&d->mutex); | - |
| 436 | return d->finished || d->isInFinish; | - |
| 437 | } | - |
| 438 | - | |
| 439 | /*! | - |
| 440 | Returns \c true if the thread is running; otherwise returns \c false. | - |
| 441 | - | |
| 442 | \sa isFinished() | - |
| 443 | */ | - |
| 444 | bool QThread::isRunning() const | - |
| 445 | { | - |
| 446 | Q_D(const QThread); | - |
| 447 | QMutexLocker locker(&d->mutex); | - |
| 448 | return d->running && !d->isInFinish; | - |
| 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); | - |
| 466 | QMutexLocker locker(&d->mutex); | - |
| 467 | Q_ASSERT_X(!d->running, "QThread::setStackSize", | - |
| 468 | "cannot change stack size while the thread is running"); | - |
| 469 | d->stackSize = stackSize; | - |
| 470 | } | - |
| 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); | - |
| 481 | QMutexLocker locker(&d->mutex); | - |
| 482 | return d->stackSize; | - |
| 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 | This function is meant to be called from within run(). It is necessary to | - |
| 491 | call this function to start event handling. | - |
| 492 | - | |
| 493 | \sa quit(), exit() | - |
| 494 | */ | - |
| 495 | int QThread::exec() | - |
| 496 | { | - |
| 497 | Q_D(QThread); | - |
| 498 | QMutexLocker locker(&d->mutex); | - |
| 499 | d->data->quitNow = false; | - |
| 500 | if (d->exited) { | - |
| 501 | d->exited = false; | - |
| 502 | return d->returnCode; | - |
| 503 | } | - |
| 504 | locker.unlock(); | - |
| 505 | - | |
| 506 | QEventLoop eventLoop; | - |
| 507 | int returnCode = eventLoop.exec(); | - |
| 508 | - | |
| 509 | locker.relock(); | - |
| 510 | d->exited = false; | - |
| 511 | d->returnCode = -1; | - |
| 512 | return returnCode; | - |
| 513 | } | - |
| 514 | - | |
| 515 | /*! | - |
| 516 | Tells the thread's event loop to exit with a return code. | - |
| 517 | - | |
| 518 | After calling this function, the thread leaves the event loop and | - |
| 519 | returns from the call to QEventLoop::exec(). The | - |
| 520 | QEventLoop::exec() function returns \a returnCode. | - |
| 521 | - | |
| 522 | By convention, a \a returnCode of 0 means success, any non-zero value | - |
| 523 | indicates an error. | - |
| 524 | - | |
| 525 | Note that unlike the C library function of the same name, this | - |
| 526 | function \e does return to the caller -- it is event processing | - |
| 527 | that stops. | - |
| 528 | - | |
| 529 | No QEventLoops will be started anymore in this thread until | - |
| 530 | QThread::exec() has been called again. If the eventloop in QThread::exec() | - |
| 531 | is not running then the next call to QThread::exec() will also return | - |
| 532 | immediately. | - |
| 533 | - | |
| 534 | \sa quit(), QEventLoop | - |
| 535 | */ | - |
| 536 | void QThread::exit(int returnCode) | - |
| 537 | { | - |
| 538 | Q_D(QThread); | - |
| 539 | QMutexLocker locker(&d->mutex); | - |
| 540 | d->exited = true; | - |
| 541 | d->returnCode = returnCode; | - |
| 542 | d->data->quitNow = true; | - |
| 543 | for (int i = 0; i < d->data->eventLoops.size(); ++i) { | - |
| 544 | QEventLoop *eventLoop = d->data->eventLoops.at(i); | - |
| 545 | eventLoop->exit(returnCode); | - |
| 546 | } | - |
| 547 | } | - |
| 548 | - | |
| 549 | /*! | - |
| 550 | Tells the thread's event loop to exit with return code 0 (success). | - |
| 551 | Equivalent to calling QThread::exit(0). | - |
| 552 | - | |
| 553 | This function does nothing if the thread does not have an event | - |
| 554 | loop. | - |
| 555 | - | |
| 556 | \sa exit(), QEventLoop | - |
| 557 | */ | - |
| 558 | void QThread::quit() | - |
| 559 | { exit(); } | - |
| 560 | - | |
| 561 | /*! | - |
| 562 | The starting point for the thread. After calling start(), the | - |
| 563 | newly created thread calls this function. The default | - |
| 564 | implementation simply calls exec(). | - |
| 565 | - | |
| 566 | You can reimplement this function to facilitate advanced thread | - |
| 567 | management. Returning from this method will end the execution of | - |
| 568 | the thread. | - |
| 569 | - | |
| 570 | \sa start(), wait() | - |
| 571 | */ | - |
| 572 | void QThread::run() | - |
| 573 | { | - |
| 574 | (void) exec(); | - |
| 575 | } | - |
| 576 | - | |
| 577 | /*! \fn void QThread::setPriority(Priority priority) | - |
| 578 | \since 4.1 | - |
| 579 | - | |
| 580 | This function sets the \a priority for a running thread. If the | - |
| 581 | thread is not running, this function does nothing and returns | - |
| 582 | immediately. Use start() to start a thread with a specific | - |
| 583 | priority. | - |
| 584 | - | |
| 585 | The \a priority argument can be any value in the \c | - |
| 586 | QThread::Priority enum except for \c InheritPriorty. | - |
| 587 | - | |
| 588 | The effect of the \a priority parameter is dependent on the | - |
| 589 | operating system's scheduling policy. In particular, the \a priority | - |
| 590 | will be ignored on systems that do not support thread priorities | - |
| 591 | (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler | - |
| 592 | for more details). | - |
| 593 | - | |
| 594 | \sa Priority, priority(), start() | - |
| 595 | */ | - |
| 596 | void QThread::setPriority(Priority priority) | - |
| 597 | { | - |
| 598 | Q_D(QThread); | - |
| 599 | QMutexLocker locker(&d->mutex); | - |
| 600 | if (!d->running) { | - |
| 601 | qWarning("QThread::setPriority: Cannot set priority, thread is not running"); | - |
| 602 | return; | - |
| 603 | } | - |
| 604 | d->setPriority(priority); | - |
| 605 | } | - |
| 606 | - | |
| 607 | /*! | - |
| 608 | \since 4.1 | - |
| 609 | - | |
| 610 | Returns the priority for a running thread. If the thread is not | - |
| 611 | running, this function returns \c InheritPriority. | - |
| 612 | - | |
| 613 | \sa Priority, setPriority(), start() | - |
| 614 | */ | - |
| 615 | QThread::Priority QThread::priority() const | - |
| 616 | { | - |
| 617 | Q_D(const QThread); | - |
| 618 | QMutexLocker locker(&d->mutex); | - |
| 619 | - | |
| 620 | // mask off the high bits that are used for flags | - |
| 621 | return Priority(d->priority & 0xffff); | - |
| 622 | } | - |
| 623 | - | |
| 624 | /*! | - |
| 625 | \fn void QThread::sleep(unsigned long secs) | - |
| 626 | - | |
| 627 | Forces the current thread to sleep for \a secs seconds. | - |
| 628 | - | |
| 629 | \sa msleep(), usleep() | - |
| 630 | */ | - |
| 631 | - | |
| 632 | /*! | - |
| 633 | \fn void QThread::msleep(unsigned long msecs) | - |
| 634 | - | |
| 635 | Forces the current thread to sleep for \a msecs milliseconds. | - |
| 636 | - | |
| 637 | \sa sleep(), usleep() | - |
| 638 | */ | - |
| 639 | - | |
| 640 | /*! | - |
| 641 | \fn void QThread::usleep(unsigned long usecs) | - |
| 642 | - | |
| 643 | Forces the current thread to sleep for \a usecs microseconds. | - |
| 644 | - | |
| 645 | \sa sleep(), msleep() | - |
| 646 | */ | - |
| 647 | - | |
| 648 | /*! | - |
| 649 | \fn void QThread::terminate() | - |
| 650 | - | |
| 651 | Terminates the execution of the thread. The thread may or may not | - |
| 652 | be terminated immediately, depending on the operating system's | - |
| 653 | scheduling policies. Use QThread::wait() after terminate(), to be | - |
| 654 | sure. | - |
| 655 | - | |
| 656 | When the thread is terminated, all threads waiting for the thread | - |
| 657 | to finish will be woken up. | - |
| 658 | - | |
| 659 | \warning This function is dangerous and its use is discouraged. | - |
| 660 | The thread can be terminated at any point in its code path. | - |
| 661 | Threads can be terminated while modifying data. There is no | - |
| 662 | chance for the thread to clean up after itself, unlock any held | - |
| 663 | mutexes, etc. In short, use this function only if absolutely | - |
| 664 | necessary. | - |
| 665 | - | |
| 666 | Termination can be explicitly enabled or disabled by calling | - |
| 667 | QThread::setTerminationEnabled(). Calling this function while | - |
| 668 | termination is disabled results in the termination being | - |
| 669 | deferred, until termination is re-enabled. See the documentation | - |
| 670 | of QThread::setTerminationEnabled() for more information. | - |
| 671 | - | |
| 672 | \sa setTerminationEnabled() | - |
| 673 | */ | - |
| 674 | - | |
| 675 | /*! | - |
| 676 | \fn bool QThread::wait(unsigned long time) | - |
| 677 | - | |
| 678 | Blocks the thread until either of these conditions is met: | - |
| 679 | - | |
| 680 | \list | - |
| 681 | \li The thread associated with this QThread object has finished | - |
| 682 | execution (i.e. when it returns from \l{run()}). This function | - |
| 683 | will return true if the thread has finished. It also returns | - |
| 684 | true if the thread has not been started yet. | - |
| 685 | \li \a time milliseconds has elapsed. If \a time is ULONG_MAX (the | - |
| 686 | default), then the wait will never timeout (the thread must | - |
| 687 | return from \l{run()}). This function will return false if the | - |
| 688 | wait timed out. | - |
| 689 | \endlist | - |
| 690 | - | |
| 691 | This provides similar functionality to the POSIX \c | - |
| 692 | pthread_join() function. | - |
| 693 | - | |
| 694 | \sa sleep(), terminate() | - |
| 695 | */ | - |
| 696 | - | |
| 697 | /*! | - |
| 698 | \fn void QThread::setTerminationEnabled(bool enabled) | - |
| 699 | - | |
| 700 | Enables or disables termination of the current thread based on the | - |
| 701 | \a enabled parameter. The thread must have been started by | - |
| 702 | QThread. | - |
| 703 | - | |
| 704 | When \a enabled is false, termination is disabled. Future calls | - |
| 705 | to QThread::terminate() will return immediately without effect. | - |
| 706 | Instead, the termination is deferred until termination is enabled. | - |
| 707 | - | |
| 708 | When \a enabled is true, termination is enabled. Future calls to | - |
| 709 | QThread::terminate() will terminate the thread normally. If | - |
| 710 | termination has been deferred (i.e. QThread::terminate() was | - |
| 711 | called with termination disabled), this function will terminate | - |
| 712 | the calling thread \e immediately. Note that this function will | - |
| 713 | not return in this case. | - |
| 714 | - | |
| 715 | \sa terminate() | - |
| 716 | */ | - |
| 717 | - | |
| 718 | /*! | - |
| 719 | \since 5.5 | - |
| 720 | Returns the current event loop level for the thread. | - |
| 721 | - | |
| 722 | \note This can only be called within the thread itself, i.e. when | - |
| 723 | it is the current thread. | - |
| 724 | */ | - |
| 725 | - | |
| 726 | int QThread::loopLevel() const | - |
| 727 | { | - |
| 728 | Q_D(const QThread); | - |
| 729 | return d->data->eventLoops.size(); | - |
| 730 | } | - |
| 731 | - | |
| 732 | #else // QT_NO_THREAD | - |
| 733 | - | |
| 734 | QThread::QThread(QObject *parent) | - |
| 735 | : QObject(*(new QThreadPrivate), (QObject*)0){ | - |
| 736 | Q_D(QThread); | - |
| 737 | d->data->thread = this; | - |
| 738 | } | - |
| 739 | - | |
| 740 | QThread *QThread::currentThread() | - |
| 741 | { | - |
| 742 | return QThreadData::current()->thread; | - |
| 743 | } | - |
| 744 | - | |
| 745 | QThreadData* QThreadData::current() | - |
| 746 | { | - |
| 747 | static QThreadData *data = 0; // reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key)); | - |
| 748 | if (!data) { | - |
| 749 | QScopedPointer<QThreadData> newdata(new QThreadData); | - |
| 750 | newdata->thread = new QAdoptedThread(newdata.data()); | - |
| 751 | data = newdata.take(); | - |
| 752 | data->deref(); | - |
| 753 | } | - |
| 754 | return data; | - |
| 755 | } | - |
| 756 | - | |
| 757 | /*! | - |
| 758 | \internal | - |
| 759 | */ | - |
| 760 | QThread::QThread(QThreadPrivate &dd, QObject *parent) | - |
| 761 | : QObject(dd, parent) | - |
| 762 | { | - |
| 763 | Q_D(QThread); | - |
| 764 | // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); | - |
| 765 | d->data->thread = this; | - |
| 766 | } | - |
| 767 | - | |
| 768 | #endif // QT_NO_THREAD | - |
| 769 | - | |
| 770 | /*! | - |
| 771 | \since 5.0 | - |
| 772 | - | |
| 773 | Returns a pointer to the event dispatcher object for the thread. If no event | - |
| 774 | dispatcher exists for the thread, this function returns 0. | - |
| 775 | */ | - |
| 776 | QAbstractEventDispatcher *QThread::eventDispatcher() const | - |
| 777 | { | - |
| 778 | Q_D(const QThread); | - |
| 779 | return d->data->eventDispatcher.load(); | - |
| 780 | } | - |
| 781 | - | |
| 782 | /*! | - |
| 783 | \since 5.0 | - |
| 784 | - | |
| 785 | Sets the event dispatcher for the thread to \a eventDispatcher. This is | - |
| 786 | only possible as long as there is no event dispatcher installed for the | - |
| 787 | thread yet. That is, before the thread has been started with start() or, in | - |
| 788 | case of the main thread, before QCoreApplication has been instantiated. | - |
| 789 | This method takes ownership of the object. | - |
| 790 | */ | - |
| 791 | void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) | - |
| 792 | { | - |
| 793 | Q_D(QThread); | - |
| 794 | if (d->data->hasEventDispatcher()) { | - |
| 795 | qWarning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread"); | - |
| 796 | } else { | - |
| 797 | eventDispatcher->moveToThread(this); | - |
| 798 | if (eventDispatcher->thread() == this) // was the move successful? | - |
| 799 | d->data->eventDispatcher = eventDispatcher; | - |
| 800 | else | - |
| 801 | qWarning("QThread::setEventDispatcher: Could not move event dispatcher to target thread"); | - |
| 802 | } | - |
| 803 | } | - |
| 804 | - | |
| 805 | /*! | - |
| 806 | \reimp | - |
| 807 | */ | - |
| 808 | bool QThread::event(QEvent *event) | - |
| 809 | { | - |
| 810 | if (event->type() == QEvent::Quit) { | - |
| 811 | quit(); | - |
| 812 | return true; | - |
| 813 | } else { | - |
| 814 | return QObject::event(event); | - |
| 815 | } | - |
| 816 | } | - |
| 817 | - | |
| 818 | /*! | - |
| 819 | \since 5.2 | - |
| 820 | - | |
| 821 | Request the interruption of the thread. | - |
| 822 | That request is advisory and it is up to code running on the thread to decide | - |
| 823 | if and how it should act upon such request. | - |
| 824 | This function does not stop any event loop running on the thread and | - |
| 825 | does not terminate it in any way. | - |
| 826 | - | |
| 827 | \sa isInterruptionRequested() | - |
| 828 | */ | - |
| 829 | - | |
| 830 | void QThread::requestInterruption() | - |
| 831 | { | - |
| 832 | Q_D(QThread); | - |
| 833 | QMutexLocker locker(&d->mutex); | - |
| 834 | if (!d->running || d->finished || d->isInFinish) | - |
| 835 | return; | - |
| 836 | if (this == QCoreApplicationPrivate::theMainThread) { | - |
| 837 | qWarning("QThread::requestInterruption has no effect on the main thread"); | - |
| 838 | return; | - |
| 839 | } | - |
| 840 | d->interruptionRequested = true; | - |
| 841 | } | - |
| 842 | - | |
| 843 | /*! | - |
| 844 | \since 5.2 | - |
| 845 | - | |
| 846 | Return true if the task running on this thread should be stopped. | - |
| 847 | An interruption can be requested by requestInterruption(). | - |
| 848 | - | |
| 849 | This function can be used to make long running tasks cleanly interruptible. | - |
| 850 | Never checking or acting on the value returned by this function is safe, | - |
| 851 | however it is advisable do so regularly in long running functions. | - |
| 852 | Take care not to call it too often, to keep the overhead low. | - |
| 853 | - | |
| 854 | \code | - |
| 855 | void long_task() { | - |
| 856 | forever { | - |
| 857 | if ( QThread::currentThread()->isInterruptionRequested() ) { | - |
| 858 | return; | - |
| 859 | } | - |
| 860 | } | - |
| 861 | } | - |
| 862 | \endcode | - |
| 863 | - | |
| 864 | \sa currentThread() requestInterruption() | - |
| 865 | */ | - |
| 866 | bool QThread::isInterruptionRequested() const | - |
| 867 | { | - |
| 868 | Q_D(const QThread); | - |
| 869 | QMutexLocker locker(&d->mutex); | - |
| 870 | if (!d->running || d->finished || d->isInFinish) | - |
| 871 | return false; | - |
| 872 | return d->interruptionRequested; | - |
| 873 | } | - |
| 874 | - | |
| 875 | /*! | - |
| 876 | \class QDaemonThread | - |
| 877 | \since 5.5 | - |
| 878 | \brief The QDaemonThread provides a class to manage threads that outlive QCoreApplication | - |
| 879 | \internal | - |
| 880 | - | |
| 881 | Note: don't try to deliver events from the started() signal. | - |
| 882 | */ | - |
| static void setThreadDoesNotRequireCoreApplication() | ||
| { | ||
| QThreadData::current()->requiresCoreApplication = false; | ||
| }QDaemonThread::QDaemonThread(QObject *parent) | ||
| 884 | : QThread(parent) | - |
| 885 | { | - |
| 886 | // QThread::started() is emitted from the thread we start | - |
| 887 | connect(this, &QThread::started, | - |
| 888 | setThreadDoesNotRequireCoreApplication);[](){ QThreadData::current()->requiresCoreApplication = false; }); executed 189 times by 161 tests: end of blockExecuted by:
| 189 |
| 889 | } executed 189 times by 161 tests: end of blockExecuted by:
| 189 |
| 890 | - | |
| 891 | QDaemonThread::~QDaemonThread() | - |
| 892 | { | - |
| 893 | } | - |
| 894 | - | |
| 895 | QT_END_NAMESPACE | - |
| Source code | Switch to Preprocessed file |