Fix glib event loop threading

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

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

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

Loading…
Cancel
Save