Fix glib event loop threading

pull/2/head
Timothy Pearson 12 years ago
parent 9bff9eeefc
commit a7865cf691

@ -112,10 +112,9 @@ public:
#endif // Q_WS_X11
int thread_pipe[2];
GPollFD threadPipe_gPollFD;
QPtrList<QSockNotGPollFD> sn_list;
// pending socket notifiers list
QPtrList<QSockNotGPollFD> sn_pending_list;
@ -123,10 +122,11 @@ public:
uint pev_flags;
// My GSource
GSource * gSource;
bool singletoolkit;
// main context
GMainContext *ctx;
};
#endif // QEVENTLOOP_GLIB_P_H

@ -48,6 +48,7 @@
#if defined(QT_THREAD_SUPPORT)
# include "qmutex.h"
# include "qthread.h"
#endif // QT_THREAD_SUPPORT
#include <errno.h>
@ -99,7 +100,16 @@ static gboolean qt_gsource_dispatch ( GSource *source,
Q_UNUSED(user_data);
QtGSource * qtGSource = (QtGSource*) source;
return qtGSource->qeventLoop->gsourceDispatch(source);
QEventLoop* candidateEventLoop = qtGSource->qeventLoop;
QEventLoop* activeThreadEventLoop = QApplication::eventLoop();
if (candidateEventLoop == activeThreadEventLoop) {
return candidateEventLoop->gsourceDispatch(source);
}
else {
// Dispatch failed
return FALSE;
}
}
@ -185,11 +195,13 @@ void QEventLoop::init()
d->xfd = XConnectionNumber( QPaintDevice::x11AppDisplay() );
}
// new main context for thread
d->ctx = g_main_context_new();
g_main_context_push_thread_default(d->ctx);
// new GSource
QtGSource * qtGSource = (QtGSource*) g_source_new(&qt_gsource_funcs, sizeof(QtGSource));
g_source_set_can_recurse ((GSource*)qtGSource, TRUE);
qtGSource->qeventLoop = this;
// init main loop and attach gsource
@ -198,14 +210,11 @@ void QEventLoop::init()
printf("inside init(1)\n");
#endif
g_main_loop_new (NULL, 1);
g_source_attach( (GSource*)qtGSource, NULL );
g_main_loop_new (d->ctx, 1);
g_source_attach( (GSource*)qtGSource, d->ctx );
d->gSource = (GSource*) qtGSource;
// poll for X11 events
if ( qt_is_gui_used && QApplication::isGuiThread() ) {
d->x_gPollFD.fd = d->xfd;
d->x_gPollFD.events = G_IO_IN | G_IO_HUP;
@ -213,7 +222,6 @@ void QEventLoop::init()
}
// poll thread-pipe
d->threadPipe_gPollFD.fd = d->thread_pipe[0];
d->threadPipe_gPollFD.events = G_IO_IN | G_IO_HUP;
@ -235,6 +243,9 @@ void QEventLoop::cleanup()
// cleanup the X11 parts of the event loop
d->xfd = -1;
// unref the main context
g_main_context_unref(d->ctx);
// todo: destroy gsource
}
@ -251,7 +262,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(d->ctx, flags & WaitForMore ? TRUE : FALSE);
d->pev_flags = save_flags;
@ -412,12 +423,6 @@ bool QEventLoop::gsourcePrepare(GSource *gs, int * timeout)
(**it)();
}
// 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();
#endif
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(2.1) canwait=%d\n", canWait);
#endif
@ -425,22 +430,18 @@ bool QEventLoop::gsourcePrepare(GSource *gs, int * timeout)
// do we have to dispatch events?
if (hasPendingEvents()) {
*timeout = 0; // no time to stay in poll
#ifdef DEBUG_QT_GLIBMAINLOOP
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(3a)\n");
#endif
#endif
return FALSE;
}
// stay in poll until something happens?
if (!tm) { // fixme
*timeout = -1; // wait forever
#ifdef DEBUG_QT_GLIBMAINLOOP
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(3b) timeout=%d \n", *timeout);
#endif
#endif
return FALSE;
}
@ -529,7 +530,7 @@ bool QEventLoop::gsourceDispatch(GSource *gs) {
QMutexLocker locker( QApplication::qt_mutex );
#endif
#if defined(QT_THREAD_SUPPORT)
locker.mutex()->lock();
if (locker.mutex()) locker.mutex()->lock();
#endif
int nevents=0;
@ -598,13 +599,13 @@ bool QEventLoop::gsourceDispatch(GSource *gs) {
qt_reset_color_avail();
#if defined(QT_THREAD_SUPPORT)
locker.mutex()->unlock();
if (locker.mutex()) locker.mutex()->unlock();
#endif
processX11Events();
}
else {
#if defined(QT_THREAD_SUPPORT)
locker.mutex()->unlock();
if (locker.mutex()) locker.mutex()->unlock();
#endif
}

Loading…
Cancel
Save