[2] | 1 | /****************************************************************************
|
---|
| 2 | ** $Id: qthread.cpp 8 2005-11-16 19:36:46Z dmik $
|
---|
| 3 | **
|
---|
| 4 | ** Cross-platform QThread implementation.
|
---|
| 5 | **
|
---|
| 6 | ** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
|
---|
| 7 | **
|
---|
| 8 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
---|
| 9 | **
|
---|
| 10 | ** This file may be distributed under the terms of the Q Public License
|
---|
| 11 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
| 12 | ** LICENSE.QPL included in the packaging of this file.
|
---|
| 13 | **
|
---|
| 14 | ** This file may be distributed and/or modified under the terms of the
|
---|
| 15 | ** GNU General Public License version 2 as published by the Free Software
|
---|
| 16 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 17 | ** packaging of this file.
|
---|
| 18 | **
|
---|
| 19 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
| 20 | ** licenses for Unix/X11 or for Qt/Embedded may use this file in accordance
|
---|
| 21 | ** with the Qt Commercial License Agreement provided with the Software.
|
---|
| 22 | **
|
---|
| 23 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
| 24 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 25 | **
|
---|
| 26 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
| 27 | ** information about Qt Commercial License Agreements.
|
---|
| 28 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
| 29 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
| 30 | **
|
---|
| 31 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
| 32 | ** not clear to you.
|
---|
| 33 | **
|
---|
| 34 | **********************************************************************/
|
---|
| 35 |
|
---|
| 36 | #ifdef QT_THREAD_SUPPORT
|
---|
| 37 |
|
---|
| 38 | #include "qplatformdefs.h"
|
---|
| 39 |
|
---|
| 40 | #include "qthread.h"
|
---|
| 41 | #include <private/qthreadinstance_p.h>
|
---|
| 42 |
|
---|
[8] | 43 | #ifndef QT_NO_COMPAT
|
---|
[2] | 44 | #ifndef QT_H
|
---|
| 45 | # include "qapplication.h"
|
---|
| 46 | #endif // QT_H
|
---|
[8] | 47 | #endif // QT_NO_COMPAT
|
---|
[2] | 48 |
|
---|
| 49 | #if QT_VERSION >= 0x040000
|
---|
| 50 | # error "Remove QThread::QThread() and QThread::start()."
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | /*!
|
---|
| 55 | \class QThread qthread.h
|
---|
| 56 | \threadsafe
|
---|
| 57 | \brief The QThread class provides platform-independent threads.
|
---|
| 58 |
|
---|
| 59 | \ingroup thread
|
---|
| 60 | \ingroup environment
|
---|
| 61 |
|
---|
| 62 | A QThread represents a separate thread of control within the
|
---|
| 63 | program; it shares data with all the other threads within the
|
---|
| 64 | process but executes independently in the way that a separate
|
---|
| 65 | program does on a multitasking operating system. Instead of
|
---|
| 66 | starting in main(), QThreads begin executing in run(). You inherit
|
---|
| 67 | run() to include your code. For example:
|
---|
| 68 |
|
---|
| 69 | \code
|
---|
| 70 | class MyThread : public QThread {
|
---|
| 71 |
|
---|
| 72 | public:
|
---|
| 73 |
|
---|
| 74 | virtual void run();
|
---|
| 75 |
|
---|
| 76 | };
|
---|
| 77 |
|
---|
| 78 | void MyThread::run()
|
---|
| 79 | {
|
---|
| 80 | for( int count = 0; count < 20; count++ ) {
|
---|
| 81 | sleep( 1 );
|
---|
| 82 | qDebug( "Ping!" );
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | int main()
|
---|
| 87 | {
|
---|
| 88 | MyThread a;
|
---|
| 89 | MyThread b;
|
---|
| 90 | a.start();
|
---|
| 91 | b.start();
|
---|
| 92 | a.wait();
|
---|
| 93 | b.wait();
|
---|
| 94 | }
|
---|
| 95 | \endcode
|
---|
| 96 |
|
---|
| 97 | This will start two threads, each of which writes Ping! 20 times
|
---|
| 98 | to the screen and exits. The wait() calls at the end of main() are
|
---|
| 99 | necessary because exiting main() ends the program, unceremoniously
|
---|
| 100 | killing all other threads. Each MyThread stops executing when it
|
---|
| 101 | reaches the end of MyThread::run(), just as an application does
|
---|
| 102 | when it leaves main().
|
---|
| 103 |
|
---|
| 104 | \sa \link threads.html Thread Support in Qt\endlink.
|
---|
| 105 | */
|
---|
| 106 |
|
---|
| 107 | /*!
|
---|
| 108 | \enum QThread::Priority
|
---|
| 109 |
|
---|
| 110 | This enum type indicates how the operating system should schedule
|
---|
| 111 | newly created threads.
|
---|
| 112 |
|
---|
| 113 | \value IdlePriority scheduled only when no other threads are
|
---|
| 114 | running.
|
---|
| 115 |
|
---|
| 116 | \value LowestPriority scheduled less often than LowPriority.
|
---|
| 117 | \value LowPriority scheduled less often than NormalPriority.
|
---|
| 118 |
|
---|
| 119 | \value NormalPriority the default priority of the operating
|
---|
| 120 | system.
|
---|
| 121 |
|
---|
| 122 | \value HighPriority scheduled more often than NormalPriority.
|
---|
| 123 | \value HighestPriority scheduled more often then HighPriority.
|
---|
| 124 |
|
---|
| 125 | \value TimeCriticalPriority scheduled as often as possible.
|
---|
| 126 |
|
---|
| 127 | \value InheritPriority use the same priority as the creating
|
---|
| 128 | thread. This is the default.
|
---|
| 129 | */
|
---|
| 130 |
|
---|
| 131 | QThread::QThread()
|
---|
| 132 | {
|
---|
| 133 | d = new QThreadInstance;
|
---|
| 134 | d->init(0);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*!
|
---|
| 138 | Constructs a new thread. The thread does not begin executing until
|
---|
| 139 | start() is called.
|
---|
| 140 |
|
---|
| 141 | If \a stackSize is greater than zero, the maximum stack size is
|
---|
| 142 | set to \a stackSize bytes, otherwise the maximum stack size is
|
---|
| 143 | automatically determined by the operating system.
|
---|
| 144 |
|
---|
| 145 | \warning Most operating systems place minimum and maximum limits
|
---|
| 146 | on thread stack sizes. The thread will fail to start if the stack
|
---|
| 147 | size is outside these limits.
|
---|
| 148 | */
|
---|
| 149 | QThread::QThread( unsigned int stackSize )
|
---|
| 150 | {
|
---|
| 151 | d = new QThreadInstance;
|
---|
| 152 | d->init(stackSize);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /*!
|
---|
| 156 | QThread destructor.
|
---|
| 157 |
|
---|
| 158 | Note that deleting a QThread object will not stop the execution of
|
---|
| 159 | the thread it represents. Deleting a running QThread (i.e.
|
---|
| 160 | finished() returns FALSE) will probably result in a program crash.
|
---|
| 161 | You can wait() on a thread to make sure that it has finished.
|
---|
| 162 | */
|
---|
| 163 | QThread::~QThread()
|
---|
| 164 | {
|
---|
| 165 | QMutexLocker locker( d->mutex() );
|
---|
| 166 | if ( d->running && !d->finished ) {
|
---|
| 167 | #ifdef QT_CHECK_STATE
|
---|
| 168 | qWarning("QThread object destroyed while thread is still running.");
|
---|
| 169 | #endif
|
---|
| 170 |
|
---|
| 171 | d->orphan = TRUE;
|
---|
| 172 | return;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | d->deinit();
|
---|
| 176 | delete d;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /*!
|
---|
| 180 | This function terminates the execution of the thread. The thread
|
---|
| 181 | may or may not be terminated immediately, depending on the
|
---|
| 182 | operating systems scheduling policies. Use QThread::wait()
|
---|
| 183 | after terminate() for synchronous termination.
|
---|
| 184 |
|
---|
| 185 | When the thread is terminated, all threads waiting for the
|
---|
| 186 | the thread to finish will be woken up.
|
---|
| 187 |
|
---|
| 188 | \warning This function is dangerous, and its use is discouraged.
|
---|
| 189 | The thread can be terminate at any point in its code path. Threads
|
---|
| 190 | can be terminated while modifying data. There is no chance for
|
---|
| 191 | the thread to cleanup after itself, unlock any held mutexes, etc.
|
---|
| 192 | In short, use this function only if \e absolutely necessary.
|
---|
| 193 | */
|
---|
| 194 | void QThread::terminate()
|
---|
| 195 | {
|
---|
| 196 | QMutexLocker locker( d->mutex() );
|
---|
| 197 | if ( d->finished || !d->running )
|
---|
| 198 | return;
|
---|
| 199 | d->terminate();
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /*!
|
---|
| 203 | Returns TRUE if the thread is finished; otherwise returns FALSE.
|
---|
| 204 | */
|
---|
| 205 | bool QThread::finished() const
|
---|
| 206 | {
|
---|
| 207 | QMutexLocker locker( d->mutex() );
|
---|
| 208 | return d->finished;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /*!
|
---|
| 212 | Returns TRUE if the thread is running; otherwise returns FALSE.
|
---|
| 213 | */
|
---|
| 214 | bool QThread::running() const
|
---|
| 215 | {
|
---|
| 216 | QMutexLocker locker( d->mutex() );
|
---|
| 217 | return d->running;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /*!
|
---|
| 221 | \fn void QThread::run()
|
---|
| 222 |
|
---|
| 223 | This method is pure virtual, and must be implemented in derived
|
---|
| 224 | classes in order to do useful work. Returning from this method
|
---|
| 225 | will end the execution of the thread.
|
---|
| 226 |
|
---|
| 227 | \sa wait()
|
---|
| 228 | */
|
---|
| 229 |
|
---|
| 230 | #ifndef QT_NO_COMPAT
|
---|
| 231 | /*! \obsolete
|
---|
| 232 | Use QApplication::postEvent() instead.
|
---|
| 233 | */
|
---|
| 234 | void QThread::postEvent( QObject * receiver, QEvent * event )
|
---|
| 235 | {
|
---|
| 236 | QApplication::postEvent( receiver, event );
|
---|
| 237 | }
|
---|
| 238 | #endif
|
---|
| 239 |
|
---|
| 240 | #endif // QT_THREAD_SUPPORT
|
---|