From 78125ea2f051107b84fdc0354acdedb7885308ee Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 6 Dec 2012 16:47:27 -0600 Subject: Add real threading support, including per-thread event loops, to QThread --- src/kernel/qapplication.cpp | 124 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 110 insertions(+), 14 deletions(-) (limited to 'src/kernel/qapplication.cpp') diff --git a/src/kernel/qapplication.cpp b/src/kernel/qapplication.cpp index 51aa247..a9c9fb8 100644 --- a/src/kernel/qapplication.cpp +++ b/src/kernel/qapplication.cpp @@ -68,6 +68,7 @@ #if defined(QT_THREAD_SUPPORT) # include "qmutex.h" # include "qthread.h" +# include #endif // QT_THREAD_SUPPORT #include @@ -383,7 +384,25 @@ Q_EXPORT Qt::HANDLE qt_get_application_thread_id() } #endif // QT_THREAD_SUPPORT +#ifndef QT_THREAD_SUPPORT QEventLoop *QApplication::eventloop = 0; // application event loop +#endif + +#ifdef QT_THREAD_SUPPORT +QEventLoop* QApplication::currentEventLoop() { + QThread* thread = QThread::currentThreadObject(); + if (thread) { + if (thread->d) { + return thread->d->eventLoop; + } + } + return NULL; +} +#else +QEventLoop* QApplication::currentEventLoop() { + return QApplication::eventloop; +} +#endif #ifndef QT_NO_ACCEL extern bool qt_dispatchAccelEvent( QWidget*, QKeyEvent* ); // def in qaccel.cpp @@ -516,6 +535,41 @@ QClipboard *qt_clipboard = 0; // global clipboard object #endif QWidgetList * qt_modal_stack=0; // stack of modal widgets +#ifdef QT_THREAD_SUPPORT +// thread wrapper for the main() thread +class QCoreApplicationThread : public QThread +{ +public: + inline QCoreApplicationThread() + { + QThreadInstance::setCurrentThread(this); + + // thread should be running and not finished for the lifetime + // of the application (even if QCoreApplication goes away) + d->running = true; + d->finished = false; + d->eventLoop = NULL; + } + inline ~QCoreApplicationThread() + { + // avoid warning from QThread + d->running = false; + } +private: + inline void run() + { + // this function should never be called, it is implemented + // only so that we can instantiate the object + qFatal("QCoreApplicationThread: internal error"); + } +}; + +static QCoreApplicationThread qt_main_thread; +static QThread *mainThread() { return &qt_main_thread; } +#else +static QThread* mainThread() { return QThread::currentThread(); } +#endif + // Definitions for posted events struct QPostEvent { QPostEvent( QObject *r, QEvent *e ): receiver( r ), event( e ) {} @@ -818,8 +872,8 @@ void QApplication::construct( int &argc, char **argv, Type type ) initialize( argc, argv ); if ( qt_is_gui_used ) qt_maxWindowRect = desktop()->rect(); - if ( eventloop ) - eventloop->appStartingUp(); + if ( currentEventLoop() ) + currentEventLoop()->appStartingUp(); } /*! @@ -874,8 +928,8 @@ QApplication::QApplication( Display* dpy, HANDLE visual, HANDLE colormap ) if ( qt_is_gui_used ) qt_maxWindowRect = desktop()->rect(); - if ( eventloop ) - eventloop->appStartingUp(); + if ( currentEventLoop() ) + currentEventLoop()->appStartingUp(); } /*! @@ -916,13 +970,26 @@ QApplication::QApplication(Display *dpy, int argc, char **argv, if ( qt_is_gui_used ) qt_maxWindowRect = desktop()->rect(); - if ( eventloop ) - eventloop->appStartingUp(); + if ( currentEventLoop() ) + currentEventLoop()->appStartingUp(); } #endif // Q_WS_X11 +#ifdef QT_THREAD_SUPPORT +QThread* QApplication::guiThread() { + return mainThread(); +} + +bool QApplication::isGuiThread() { + return (QThread::currentThreadObject() == guiThread()); +} +#else +bool QApplication::isGuiThread() { + return true; +} +#endif void QApplication::init_precmdline() { @@ -1030,8 +1097,8 @@ QApplication::~QApplication() } #endif - if ( eventloop ) - eventloop->appClosingDown(); + if ( currentEventLoop() ) + currentEventLoop()->appClosingDown(); if ( postRList ) { QVFuncList::Iterator it = postRList->begin(); while ( it != postRList->end() ) { // call post routines @@ -2698,8 +2765,20 @@ bool QApplication::internalNotify( QObject *receiver, QEvent * e) } - if (!handled) + if (!handled) { +#if defined(QT_THREAD_SUPPORT) + bool locked = QApplication::qt_mutex->locked(); + if (locked) { + QApplication::qt_mutex->unlock(); + } +#endif consumed = receiver->event( e ); +#if defined(QT_THREAD_SUPPORT) + if (locked) { + QApplication::qt_mutex->lock(); + } +#endif + } e->spont = FALSE; return consumed; } @@ -2793,9 +2872,10 @@ void QApplication::processOneEvent() */ QEventLoop *QApplication::eventLoop() { - if ( !eventloop && !is_app_closing ) + if ( !currentEventLoop() && !is_app_closing ) { (void) new QEventLoop( qApp, "default event loop" ); - return eventloop; + } + return currentEventLoop(); } @@ -3263,8 +3343,23 @@ void QApplication::postEvent( QObject *receiver, QEvent *event ) l->append( pe ); globalPostedEvents->append( pe ); - if (eventloop) - eventloop->wakeUp(); +#ifdef QT_THREAD_SUPPORT + if ( event->type() == QEvent::MetaCall ) { + // Wake up the receiver thread event loop + QThread* thread = receiver->contextThreadObject(); + if (thread) { + if (thread->d) { + if (thread->d->eventLoop) { + thread->d->eventLoop->wakeUp(); + } + } + } + return; + } +#endif + + if (currentEventLoop()) + currentEventLoop()->wakeUp(); } @@ -3326,7 +3421,8 @@ void QApplication::sendPostedEvents( QObject *receiver, int event_type ) && ( receiver == 0 // we send to all receivers || receiver == pe->receiver ) // we send to THAT receiver && ( event_type == 0 // we send all types - || event_type == pe->event->type() ) ) { // we send THAT type + || event_type == pe->event->type() ) // we send THAT type + && ( (!pe->receiver) || (pe->receiver->contextThreadObject() == QThread::currentThreadObject()) ) ) { // only send if active thread is receiver object owning thread // first, we diddle the event so that we can deliver // it, and that noone will try to touch it later. pe->event->posted = FALSE; -- cgit v1.2.3 From caf80d88243aaa00e8f1baeaa6b7e4c3aca75f63 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 6 Dec 2012 18:29:37 -0600 Subject: Add threading tutorial and fix a couple rare crashes --- src/kernel/qapplication.cpp | 9 ++- src/kernel/qeventloop_unix.cpp | 2 + src/kernel/qeventloop_x11.cpp | 6 +- tutorial/t15/main.cpp | 173 +++++++++++++++++++++++++++++++++++++++++ tutorial/t15/main.h | 45 +++++++++++ tutorial/t15/t15.pro | 5 ++ tutorial/tutorial.pro | 2 +- 7 files changed, 237 insertions(+), 5 deletions(-) create mode 100644 tutorial/t15/main.cpp create mode 100644 tutorial/t15/main.h create mode 100644 tutorial/t15/t15.pro (limited to 'src/kernel/qapplication.cpp') diff --git a/src/kernel/qapplication.cpp b/src/kernel/qapplication.cpp index a9c9fb8..f3b0119 100644 --- a/src/kernel/qapplication.cpp +++ b/src/kernel/qapplication.cpp @@ -2767,7 +2767,10 @@ bool QApplication::internalNotify( QObject *receiver, QEvent * e) if (!handled) { #if defined(QT_THREAD_SUPPORT) - bool locked = QApplication::qt_mutex->locked(); + bool locked = false; + if (QApplication::qt_mutex) { + locked = QApplication::qt_mutex->locked(); + } if (locked) { QApplication::qt_mutex->unlock(); } @@ -2775,7 +2778,9 @@ bool QApplication::internalNotify( QObject *receiver, QEvent * e) consumed = receiver->event( e ); #if defined(QT_THREAD_SUPPORT) if (locked) { - QApplication::qt_mutex->lock(); + if (QApplication::qt_mutex) { + QApplication::qt_mutex->lock(); + } } #endif } diff --git a/src/kernel/qeventloop_unix.cpp b/src/kernel/qeventloop_unix.cpp index 202ef12..80c8f29 100644 --- a/src/kernel/qeventloop_unix.cpp +++ b/src/kernel/qeventloop_unix.cpp @@ -562,6 +562,8 @@ int QEventLoop::activateTimers() n_act++; QTimerEvent e( t->id ); QApplication::sendEvent( t->obj, &e ); // send event + if ( !timerList ) // sendEvent allows other threads to execute, therefore we must check for list existence when it returns! + return 0; if ( timerList->findRef( begin ) == -1 ) begin = 0; } diff --git a/src/kernel/qeventloop_x11.cpp b/src/kernel/qeventloop_x11.cpp index cd3e865..5ee41ca 100644 --- a/src/kernel/qeventloop_x11.cpp +++ b/src/kernel/qeventloop_x11.cpp @@ -284,7 +284,8 @@ bool QEventLoop::processEvents( ProcessEventsFlags flags ) // unlock the GUI mutex and select. when we return from this function, there is // something for us to do #if defined(QT_THREAD_SUPPORT) - locker.mutex()->unlock(); + if ( locker.mutex() ) locker.mutex()->unlock(); + else return false; #endif int nsel; @@ -298,7 +299,8 @@ bool QEventLoop::processEvents( ProcessEventsFlags flags ) // relock the GUI mutex before processing any pending events #if defined(QT_THREAD_SUPPORT) - locker.mutex()->lock(); + if ( locker.mutex() ) locker.mutex()->lock(); + else return false; #endif // we are awake, broadcast it diff --git a/tutorial/t15/main.cpp b/tutorial/t15/main.cpp new file mode 100644 index 0000000..4c96083 --- /dev/null +++ b/tutorial/t15/main.cpp @@ -0,0 +1,173 @@ +/**************************************************************** +** +** Qt threading tutorial +** (c) 2012 Timothy Pearson +** +** This tutorial is released into the Public Domain and +** can therefore be modified and/or used for any purpose +** +****************************************************************/ + +#include "main.h" + +#include + +#include +#include + +void WorkerObject::run() +{ + qDebug( "[%s] thread: %p event loop: %p", threadFriendlyName.ascii(), QThread::currentThreadObject(), QApplication::eventLoop() ); + + QEventLoop* eventLoop = QApplication::eventLoop(); + if (!eventLoop) return; + + QTimer *t = new QTimer(this); + connect( t, SIGNAL(timeout()), SLOT(timerHandler()) ); + t->start( 1000, FALSE ); + + for( int count = 0; count < 5; count++ ) { + sleep( 1 ); + qDebug( "[%s] Ping!", threadFriendlyName.ascii() ); + displayMessage("Hi", "There!"); + eventLoop->processEvents(QEventLoop::AllEvents); + } + + eventLoop->exit(0); +} + +void WorkerObject::timerHandler() +{ + qDebug( "[%s] Timer fired!", threadFriendlyName.ascii() ); +} + +void MainObject::emitMessage(QString str1, QString str2) +{ + qDebug( "%s", ("[MainObject] emitMessage: " + str1 + " " + str2).ascii() ); +} + +void MainObject::buttonClicked() +{ + qDebug( "[MainObject] Button clicked!" ); + + QEventLoop* eventLoop = QApplication::eventLoop(); + if (!eventLoop) return; + eventLoop->exit(0); +} + +#define SET_UP_WORKER(x, y, z) \ + WorkerObject x; \ + x.threadFriendlyName = y; \ + x.moveToThread(&z); \ + QObject::connect(&x, SIGNAL(displayMessage(QString,QString)), &mainobject, SLOT(emitMessage(QString,QString))); \ + QTimer::singleShot(0, &x, SLOT(run())); + +int main( int argc, char **argv ) +{ + QApplication a( argc, argv ); + + qDebug( "[MainObject] thread: %p event loop: %p", QThread::currentThreadObject(), QApplication::eventLoop() ); + + QPushButton hello( "Exit", 0 ); + hello.resize( 100, 30 ); + + MainObject mainobject; + + QEventLoopThread workerthread0; + QEventLoopThread workerthread1; + QEventLoopThread workerthread2; + QEventLoopThread workerthread3; + QEventLoopThread workerthread4; + QEventLoopThread workerthread5; + QEventLoopThread workerthread6; + QEventLoopThread workerthread7; + QEventLoopThread workerthread8; + QEventLoopThread workerthread9; + + QEventLoopThread workerthread10; + QEventLoopThread workerthread11; + QEventLoopThread workerthread12; + QEventLoopThread workerthread13; + QEventLoopThread workerthread14; + QEventLoopThread workerthread15; + QEventLoopThread workerthread16; + QEventLoopThread workerthread17; + QEventLoopThread workerthread18; + QEventLoopThread workerthread19; + + SET_UP_WORKER(workerobject0, "WorkerObject0", workerthread0) + SET_UP_WORKER(workerobject1, "WorkerObject1", workerthread1) + SET_UP_WORKER(workerobject2, "WorkerObject2", workerthread2) + SET_UP_WORKER(workerobject3, "WorkerObject3", workerthread3) + SET_UP_WORKER(workerobject4, "WorkerObject4", workerthread4) + SET_UP_WORKER(workerobject5, "WorkerObject5", workerthread5) + SET_UP_WORKER(workerobject6, "WorkerObject6", workerthread6) + SET_UP_WORKER(workerobject7, "WorkerObject7", workerthread7) + SET_UP_WORKER(workerobject8, "WorkerObject8", workerthread8) + SET_UP_WORKER(workerobject9, "WorkerObject9", workerthread9) + + SET_UP_WORKER(workerobject10, "WorkerObjec10", workerthread10) + SET_UP_WORKER(workerobject11, "WorkerObjec11", workerthread11) + SET_UP_WORKER(workerobject12, "WorkerObjec12", workerthread12) + SET_UP_WORKER(workerobject13, "WorkerObjec13", workerthread13) + SET_UP_WORKER(workerobject14, "WorkerObjec14", workerthread14) + SET_UP_WORKER(workerobject15, "WorkerObjec15", workerthread15) + SET_UP_WORKER(workerobject16, "WorkerObjec16", workerthread16) + SET_UP_WORKER(workerobject17, "WorkerObjec17", workerthread17) + SET_UP_WORKER(workerobject18, "WorkerObjec18", workerthread18) + SET_UP_WORKER(workerobject19, "WorkerObjec19", workerthread19) + + workerthread0.start(); + workerthread1.start(); + workerthread2.start(); + workerthread3.start(); + workerthread4.start(); + workerthread5.start(); + workerthread6.start(); + workerthread7.start(); + workerthread8.start(); + workerthread9.start(); + + workerthread10.start(); + workerthread11.start(); + workerthread12.start(); + workerthread13.start(); + workerthread14.start(); + workerthread15.start(); + workerthread16.start(); + workerthread17.start(); + workerthread18.start(); + workerthread19.start(); + + a.setMainWidget( &hello ); + QObject::connect(&hello, SIGNAL(clicked()), &mainobject, SLOT(buttonClicked())); + hello.show(); + a.exec(); + hello.hide(); + + qDebug( "[MainObject] Waiting for thread completion..." ); + + workerthread0.wait(); + workerthread1.wait(); + workerthread2.wait(); + workerthread3.wait(); + workerthread4.wait(); + workerthread5.wait(); + workerthread6.wait(); + workerthread7.wait(); + workerthread8.wait(); + workerthread9.wait(); + + workerthread10.wait(); + workerthread11.wait(); + workerthread12.wait(); + workerthread13.wait(); + workerthread14.wait(); + workerthread15.wait(); + workerthread16.wait(); + workerthread17.wait(); + workerthread18.wait(); + workerthread19.wait(); + + qDebug( "[MainObject] Finished!" ); +} diff --git a/tutorial/t15/main.h b/tutorial/t15/main.h new file mode 100644 index 0000000..13ac5df --- /dev/null +++ b/tutorial/t15/main.h @@ -0,0 +1,45 @@ +/**************************************************************** +** +** Qt threading tutorial +** (c) 2012 Timothy Pearson +** +** This tutorial is released into the Public Domain and +** can therefore be modified and/or used for any purpose +** +****************************************************************/ + +#ifndef _MAIN_H_ +#define _MAIN_H_ + +#include +#include +#include +#include + +class MainObject; + +class WorkerObject : public QObject +{ + Q_OBJECT + + public slots: + void run(); + void timerHandler(); + + signals: + void displayMessage(QString, QString); + + public: + QString threadFriendlyName; +}; + +class MainObject : public QObject +{ + Q_OBJECT + + public slots: + void emitMessage(QString, QString); + void buttonClicked(); +}; + +#endif // _MAIN_H_ diff --git a/tutorial/t15/t15.pro b/tutorial/t15/t15.pro new file mode 100644 index 0000000..ff0f08d --- /dev/null +++ b/tutorial/t15/t15.pro @@ -0,0 +1,5 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = main.h +SOURCES = main.cpp +TARGET = t15 diff --git a/tutorial/tutorial.pro b/tutorial/tutorial.pro index 32b31d7..90e37f7 100644 --- a/tutorial/tutorial.pro +++ b/tutorial/tutorial.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS = t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 +SUBDIRS = t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 -- cgit v1.2.3 From 9bff9eeefc262c8509b2db7c1120f6001d65e64c Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Fri, 7 Dec 2012 15:01:56 -0600 Subject: Add level method to recursive mutex Enhance thread safety when making event calls Minor cleanup of whitespace in glib event loop --- configure | 3 ++ src/kernel/qapplication.cpp | 17 +++++---- src/kernel/qeventloop_unix_glib.cpp | 18 ++++----- src/kernel/qeventloop_x11_glib.cpp | 74 ++++++++++++++++++++----------------- src/tools/qmutex.h | 3 ++ src/tools/qmutex_p.h | 1 + src/tools/qmutex_unix.cpp | 28 ++++++++++++++ 7 files changed, 94 insertions(+), 50 deletions(-) (limited to 'src/kernel/qapplication.cpp') diff --git a/configure b/configure index 076ec00..0c34ea4 100755 --- a/configure +++ b/configure @@ -734,6 +734,9 @@ while [ "$#" -gt 0 ]; do glibmainloop) if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then CFG_GLIBMAINLOOP="$VAL" + if [ "$VAL" = "yes" ]; then + echo "WARNING: glib main loop support is ***incomplete*** and will cause problems with threaded applications and/or those using non-standard event loops!" + fi else UNKNOWN_OPT=yes fi diff --git a/src/kernel/qapplication.cpp b/src/kernel/qapplication.cpp index f3b0119..5b43301 100644 --- a/src/kernel/qapplication.cpp +++ b/src/kernel/qapplication.cpp @@ -2767,18 +2767,21 @@ bool QApplication::internalNotify( QObject *receiver, QEvent * e) if (!handled) { #if defined(QT_THREAD_SUPPORT) - bool locked = false; + int locklevel = 0; + int llcount; if (QApplication::qt_mutex) { - locked = QApplication::qt_mutex->locked(); - } - if (locked) { - QApplication::qt_mutex->unlock(); + QApplication::qt_mutex->lock(); // 1 of 2 + locklevel = qt_mutex->level() - 1; + for (llcount=0; llcountunlock(); + } + QApplication::qt_mutex->unlock(); // 2 of 2 } #endif consumed = receiver->event( e ); #if defined(QT_THREAD_SUPPORT) - if (locked) { - if (QApplication::qt_mutex) { + if (QApplication::qt_mutex) { + for (llcount=0; llcountlock(); } } diff --git a/src/kernel/qeventloop_unix_glib.cpp b/src/kernel/qeventloop_unix_glib.cpp index 9d3ce27..6a890ce 100644 --- a/src/kernel/qeventloop_unix_glib.cpp +++ b/src/kernel/qeventloop_unix_glib.cpp @@ -370,9 +370,9 @@ void QEventLoop::registerSocketNotifier( QSocketNotifier *notifier ) return; } - #ifdef DEBUG_QT_GLIBMAINLOOP - printf("register socket notifier %d\n", sockfd); - #endif +#ifdef DEBUG_QT_GLIBMAINLOOP + printf("register socket notifier %d\n", sockfd); +#endif QPtrList *list = &d->sn_list; QSockNotGPollFD *sn; @@ -425,9 +425,9 @@ void QEventLoop::unregisterSocketNotifier( QSocketNotifier *notifier ) return; } - #ifdef DEBUG_QT_GLIBMAINLOOP - printf("unregister socket notifier %d\n", sockfd); - #endif +#ifdef DEBUG_QT_GLIBMAINLOOP + printf("unregister socket notifier %d\n", sockfd); +#endif QPtrList *list = &d->sn_list; QSockNotGPollFD *sn; @@ -458,9 +458,9 @@ void QEventLoop::setSocketNotifierPending( QSocketNotifier *notifier ) return; } - #ifdef DEBUG_QT_GLIBMAINLOOP - printf("set socket notifier pending %d\n", sockfd); - #endif +#ifdef DEBUG_QT_GLIBMAINLOOP + printf("set socket notifier pending %d\n", sockfd); +#endif QPtrList *list = &d->sn_list; QSockNotGPollFD *sn; diff --git a/src/kernel/qeventloop_x11_glib.cpp b/src/kernel/qeventloop_x11_glib.cpp index b3d881a..0d2275e 100644 --- a/src/kernel/qeventloop_x11_glib.cpp +++ b/src/kernel/qeventloop_x11_glib.cpp @@ -54,6 +54,8 @@ #include +// #define DEBUG_QT_GLIBMAINLOOP 1 + // Qt-GSource Structure and Callbacks typedef struct { @@ -249,7 +251,7 @@ bool QEventLoop::processEvents( ProcessEventsFlags flags ) d->pev_flags = flags; - rval = g_main_context_iteration(NULL, flags & WaitForMore ? TRUE : FALSE); + rval = g_main_context_iteration(NULL, flags & WaitForMore ? TRUE : FALSE); d->pev_flags = save_flags; @@ -288,38 +290,38 @@ bool QEventLoop::processX11Events() XNextEvent( QPaintDevice::x11AppDisplay(), &event ); if ( flags & ExcludeUserInput ) { - switch ( event.type ) { - case ButtonPress: - case ButtonRelease: - case MotionNotify: - case XKeyPress: - case XKeyRelease: - case EnterNotify: - case LeaveNotify: - continue; - - case ClientMessage: - { - // from qapplication_x11.cpp - extern Atom qt_wm_protocols; - extern Atom qt_wm_take_focus; - extern Atom qt_qt_scrolldone; - - // only keep the wm_take_focus and - // qt_qt_scrolldone protocols, discard all - // other client messages - if ( event.xclient.format != 32 ) + switch ( event.type ) { + case ButtonPress: + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + case EnterNotify: + case LeaveNotify: continue; - - if ( event.xclient.message_type == qt_wm_protocols || - (Atom) event.xclient.data.l[0] == qt_wm_take_focus ) - break; - if ( event.xclient.message_type == qt_qt_scrolldone ) - break; + + case ClientMessage: + { + // from qapplication_x11.cpp + extern Atom qt_wm_protocols; + extern Atom qt_wm_take_focus; + extern Atom qt_qt_scrolldone; + + // only keep the wm_take_focus and + // qt_qt_scrolldone protocols, discard all + // other client messages + if ( event.xclient.format != 32 ) + continue; + + if ( event.xclient.message_type == qt_wm_protocols || + (Atom) event.xclient.data.l[0] == qt_wm_take_focus ) + break; + if ( event.xclient.message_type == qt_qt_scrolldone ) + break; + } + + default: break; } - - default: break; - } } nevents++; @@ -333,7 +335,7 @@ bool QEventLoop::processX11Events() if ( d->shortcut ) { return FALSE; } - + QApplication::sendPostedEvents(); const uint exclude_all = ExcludeSocketNotifiers | 0x08; @@ -595,12 +597,16 @@ bool QEventLoop::gsourceDispatch(GSource *gs) { // color approx. optimization - only on X11 qt_reset_color_avail(); +#if defined(QT_THREAD_SUPPORT) + locker.mutex()->unlock(); +#endif processX11Events(); } - + else { #if defined(QT_THREAD_SUPPORT) - locker.mutex()->unlock(); + locker.mutex()->unlock(); #endif + } if (d->singletoolkit) { return TRUE; // Eat the event diff --git a/src/tools/qmutex.h b/src/tools/qmutex.h index 9eb1a69..1dec4d2 100644 --- a/src/tools/qmutex.h +++ b/src/tools/qmutex.h @@ -74,6 +74,9 @@ private: QMutex( const QMutex & ); QMutex &operator=( const QMutex & ); #endif + +public: + int level(); }; class Q_EXPORT QMutexLocker diff --git a/src/tools/qmutex_p.h b/src/tools/qmutex_p.h index c80c349..d06839c 100644 --- a/src/tools/qmutex_p.h +++ b/src/tools/qmutex_p.h @@ -67,6 +67,7 @@ public: virtual bool locked() = 0; virtual bool trylock() = 0; virtual int type() const = 0; + virtual int level() = 0; }; diff --git a/src/tools/qmutex_unix.cpp b/src/tools/qmutex_unix.cpp index fe60ac3..de0f909 100644 --- a/src/tools/qmutex_unix.cpp +++ b/src/tools/qmutex_unix.cpp @@ -85,6 +85,7 @@ public: bool locked(); bool trylock(); int type() const; + int level(); bool recursive; }; @@ -101,6 +102,7 @@ public: bool locked(); bool trylock(); int type() const; + int level(); int count; unsigned long owner; @@ -196,6 +198,11 @@ int QRealMutexPrivate::type() const return recursive ? Q_MUTEX_RECURSIVE : Q_MUTEX_NORMAL; } +int QRealMutexPrivate::level() +{ + return locked(); +} + #ifndef Q_RECURSIVE_MUTEX_TYPE QRecursiveMutexPrivate::QRecursiveMutexPrivate() @@ -329,6 +336,11 @@ int QRecursiveMutexPrivate::type() const return Q_MUTEX_RECURSIVE; } +int QRecursiveMutexPrivate::level() +{ + return count; +} + #endif // !Q_RECURSIVE_MUTEX_TYPE @@ -510,6 +522,22 @@ bool QMutex::tryLock() return d->trylock(); } +/*! + Returns the current lock level of the mutex. + 0 means the mutex is unlocked + This method should only be called when the mutex has already been locked + by lock(), otherwise the lock level could change before the next line + of code is executed. + + WARNING: Non-recursive mutexes will never exceed a lock level of 1! + + \sa lock(), unlock(), locked() +*/ +int QMutex::level() +{ + return d->level(); +} + /*! \class QMutexLocker qmutex.h \brief The QMutexLocker class simplifies locking and unlocking QMutexes. -- cgit v1.2.3