diff options
author | Sven Anderson <Sven.Anderson@snom.com> | 2011-09-08 17:40:55 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-15 10:16:12 +0100 |
commit | 2b7d98ef8fbd6cf49326fa0bbf154e9bacbb7b49 (patch) | |
tree | 250f2752acb679a237504ea70888b0ad5efa1251 /src/corelib/thread | |
parent | 51b7d3c8b621c2de6f98f465f478ec574bb14195 (diff) |
Allow to create a custom event dispatcher for specific QThreads.
QAbstractEventDispatcher() does no longer install itself into the current
thread. Instead the new methods QThread::setEventDispatcher() and
QCoreApplication::setEventDispatcher() allow to install a custom event
dispatcher into any QThread as long as there is no default event dispatcher
created yet. That is, before the thread has been started with
QThread::start() or, in case of the main thread, before QCoreApplication has
been instantiated.
Change-Id: I7367e13d8d8aebed5a5651260bb69b8818eb1b90
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qthread.cpp | 32 | ||||
-rw-r--r-- | src/corelib/thread/qthread.h | 4 | ||||
-rw-r--r-- | src/corelib/thread/qthread_unix.cpp | 6 | ||||
-rw-r--r-- | src/corelib/thread/qthread_win.cpp | 7 |
4 files changed, 45 insertions, 4 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index 6cc8a4875a..17b0f2cb94 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -757,4 +757,36 @@ QThread::QThread(QThreadPrivate &dd, QObject *parent) #endif // QT_NO_THREAD +/*! + Returns a pointer to the event dispatcher object for the thread. If no event + dispatcher exists for the thread, this function returns 0. +*/ +QAbstractEventDispatcher *QThread::eventDispatcher() const +{ + Q_D(const QThread); + return d->data->eventDispatcher; +} + +/*! + Sets the event dispatcher for the thread to \a eventDispatcher. This is + only possible as long as there is no event dispatcher installed for the + thread yet. That is, before the thread has been started with start() or, in + case of the main thread, before QCoreApplication has been instantiated. + This method takes ownership of the object. +*/ +void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) +{ + Q_D(QThread); + if (d->data->eventDispatcher != 0) { + qWarning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread"); + } else { + eventDispatcher->moveToThread(this); + if (eventDispatcher->thread() == this) // was the move successful? + d->data->eventDispatcher = eventDispatcher; + else + qWarning("QThread::setEventDispatcher: Could not move event dispatcher to target thread"); + } +} + + QT_END_NAMESPACE diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index 89a3ad8b1f..baad793969 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -54,6 +54,7 @@ QT_MODULE(Core) class QThreadData; class QThreadPrivate; +class QAbstractEventDispatcher; #ifndef QT_NO_THREAD class Q_CORE_EXPORT QThread : public QObject @@ -92,6 +93,9 @@ public: void exit(int retcode = 0); + QAbstractEventDispatcher *eventDispatcher() const; + void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher); + public Q_SLOTS: void start(Priority = InheritPriority); void terminate(); diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 46bc641d16..8c6fefcfd7 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -289,8 +289,10 @@ void *QThreadPrivate::start(void *arg) data->quitNow = thr->d_func()->exited; } - // ### TODO: allow the user to create a custom event dispatcher - createEventDispatcher(data); + if (data->eventDispatcher) // custom event dispatcher set? + data->eventDispatcher->startingUp(); + else + createEventDispatcher(data); emit thr->started(); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 80d7f3ff63..5b74bad7ef 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -315,8 +315,11 @@ unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(voi QMutexLocker locker(&thr->d_func()->mutex); data->quitNow = thr->d_func()->exited; } - // ### TODO: allow the user to create a custom event dispatcher - createEventDispatcher(data); + + if (data->eventDispatcher) // custom event dispatcher set? + data->eventDispatcher->startingUp(); + else + createEventDispatcher(data); #if !defined(QT_NO_DEBUG) && defined(Q_CC_MSVC) && !defined(Q_OS_WINCE) // sets the name of the current thread. |