diff options
Diffstat (limited to 'src/kernel/qeventloop_unix_glib.cpp')
-rw-r--r-- | src/kernel/qeventloop_unix_glib.cpp | 597 |
1 files changed, 366 insertions, 231 deletions
diff --git a/src/kernel/qeventloop_unix_glib.cpp b/src/kernel/qeventloop_unix_glib.cpp index 6a890ce..1c491cf 100644 --- a/src/kernel/qeventloop_unix_glib.cpp +++ b/src/kernel/qeventloop_unix_glib.cpp @@ -45,6 +45,11 @@ #include "qapplication.h" #include "qbitarray.h" #include "qmutex.h" + +#if defined(QT_THREAD_SUPPORT) + #include "qthread.h" +#endif + #include <stdlib.h> #include <sys/types.h> @@ -87,22 +92,25 @@ // struct TimerInfo { // internal timer info - int id; // - timer identifier - timeval interval; // - timer interval - timeval timeout; // - when to sent event - QObject *obj; // - object to receive event + int id; // - timer identifier + timeval interval; // - timer interval + timeval timeout; // - when to sent event + QObject *obj; // - object to receive event }; -typedef QPtrList<TimerInfo> TimerList; // list of TimerInfo structs +typedef QPtrList<TimerInfo> TimerList; // list of TimerInfo structs static QBitArray *timerBitVec; // timer bit vector -static TimerList *timerList = 0; // timer list +static TimerList *timerList = 0; // timer list +#if defined(QT_THREAD_SUPPORT) +static QMutex *timerListMutex = 0; // timer list mutex +#endif -static void initTimers(); +static void initTimers(); void cleanupTimers(); -static timeval watchtime; // watch if time is turned back -timeval *qt_wait_timer(); -timeval *qt_wait_timer_max = 0; +static timeval watchtime; // watch if time is turned back +timeval *qt_wait_timer(); +timeval *qt_wait_timer_max = 0; // // Internal operator functions for timevals @@ -110,45 +118,44 @@ timeval *qt_wait_timer_max = 0; static inline bool operator<( const timeval &t1, const timeval &t2 ) { - return t1.tv_sec < t2.tv_sec || - (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec); + return (t1.tv_sec < t2.tv_sec) || ((t1.tv_sec == t2.tv_sec) && (t1.tv_usec < t2.tv_usec)); } static inline bool operator==( const timeval &t1, const timeval &t2 ) { - return t1.tv_sec == t2.tv_sec && t1.tv_usec == t2.tv_usec; + return t1.tv_sec == t2.tv_sec && t1.tv_usec == t2.tv_usec; } static inline timeval &operator+=( timeval &t1, const timeval &t2 ) { - t1.tv_sec += t2.tv_sec; - if ( (t1.tv_usec += t2.tv_usec) >= 1000000 ) { - t1.tv_sec++; - t1.tv_usec -= 1000000; - } - return t1; + t1.tv_sec += t2.tv_sec; + if ( (t1.tv_usec += t2.tv_usec) >= 1000000 ) { + t1.tv_sec++; + t1.tv_usec -= 1000000; + } + return t1; } static inline timeval operator+( const timeval &t1, const timeval &t2 ) { - timeval tmp; - tmp.tv_sec = t1.tv_sec + t2.tv_sec; - if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 ) { - tmp.tv_sec++; - tmp.tv_usec -= 1000000; - } - return tmp; + timeval tmp; + tmp.tv_sec = t1.tv_sec + t2.tv_sec; + if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 ) { + tmp.tv_sec++; + tmp.tv_usec -= 1000000; + } + return tmp; } static inline timeval operator-( const timeval &t1, const timeval &t2 ) { - timeval tmp; - tmp.tv_sec = t1.tv_sec - t2.tv_sec; - if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 ) { - tmp.tv_sec--; - tmp.tv_usec += 1000000; - } - return tmp; + timeval tmp; + tmp.tv_sec = t1.tv_sec - t2.tv_sec; + if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 ) { + tmp.tv_sec--; + tmp.tv_usec += 1000000; + } + return tmp; } @@ -159,68 +166,84 @@ static inline timeval operator-( const timeval &t1, const timeval &t2 ) static int allocTimerId() // find avail timer identifier { - int i = timerBitVec->size()-1; - while ( i >= 0 && (*timerBitVec)[i] ) - i--; - if ( i < 0 ) { - i = timerBitVec->size(); - timerBitVec->resize( 4 * i ); - for( int j=timerBitVec->size()-1; j > i; j-- ) - timerBitVec->clearBit( j ); - } - timerBitVec->setBit( i ); - return i+1; + int i = timerBitVec->size()-1; + while ( i >= 0 && (*timerBitVec)[i] ) { + i--; + } + if ( i < 0 ) { + i = timerBitVec->size(); + timerBitVec->resize( 4 * i ); + for( int j=timerBitVec->size()-1; j > i; j-- ) { + timerBitVec->clearBit( j ); + } + } + timerBitVec->setBit( i ); + return i+1; } -static void insertTimer( const TimerInfo *ti ) // insert timer info into list +static void insertTimer( const TimerInfo *ti ) // insert timer info into list { - TimerInfo *t = timerList->first(); - int index = 0; +#if defined(QT_THREAD_SUPPORT) + timerListMutex->lock(); +#endif + TimerInfo *t = timerList->first(); + int index = 0; #if defined(QT_DEBUG) - int dangerCount = 0; + int dangerCount = 0; #endif - while ( t && t->timeout < ti->timeout ) { // list is sorted by timeout + while ( t && t->timeout < ti->timeout ) { // list is sorted by timeout #if defined(QT_DEBUG) - if ( t->obj == ti->obj ) - dangerCount++; + if ( t->obj == ti->obj ) { + dangerCount++; + } #endif - t = timerList->next(); - index++; - } - timerList->insert( index, ti ); // inserts sorted + t = timerList->next(); + index++; + } + timerList->insert( index, ti ); // inserts sorted #if defined(QT_DEBUG) - if ( dangerCount > 16 ) - qDebug( "QObject: %d timers now exist for object %s::%s", - dangerCount, ti->obj->className(), ti->obj->name() ); + if ( dangerCount > 16 ) { + qDebug( "QObject: %d timers now exist for object %s::%s", dangerCount, ti->obj->className(), ti->obj->name() ); + } +#endif +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); #endif } -static inline void getTime( timeval &t ) // get time of day +static inline void getTime( timeval &t ) // get time of day { - gettimeofday( &t, 0 ); - while ( t.tv_usec >= 1000000 ) { // NTP-related fix - t.tv_usec -= 1000000; - t.tv_sec++; - } - while ( t.tv_usec < 0 ) { - if ( t.tv_sec > 0 ) { - t.tv_usec += 1000000; - t.tv_sec--; - } else { - t.tv_usec = 0; - break; + gettimeofday( &t, 0 ); + while ( t.tv_usec >= 1000000 ) { // NTP-related fix + t.tv_usec -= 1000000; + t.tv_sec++; + } + while ( t.tv_usec < 0 ) { + if ( t.tv_sec > 0 ) { + t.tv_usec += 1000000; + t.tv_sec--; + } + else { + t.tv_usec = 0; + break; + } } - } } -static void repairTimer( const timeval &time ) // repair broken timer +static void repairTimer( const timeval &time ) // repair broken timer { - timeval diff = watchtime - time; - register TimerInfo *t = timerList->first(); - while ( t ) { // repair all timers - t->timeout = t->timeout - diff; - t = timerList->next(); - } +#if defined(QT_THREAD_SUPPORT) + timerListMutex->lock(); +#endif + timeval diff = watchtime - time; + register TimerInfo *t = timerList->first(); + while ( t ) { // repair all timers + t->timeout = t->timeout - diff; + t = timerList->next(); + } +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); +#endif } // @@ -236,117 +259,186 @@ static void repairTimer( const timeval &time ) // repair broken timer timeval *qt_wait_timer() { - static timeval tm; - bool first = TRUE; - timeval currentTime; - if ( timerList && timerList->count() ) { // there are waiting timers - getTime( currentTime ); - if ( first ) { - if ( currentTime < watchtime ) // clock was turned back - repairTimer( currentTime ); - first = FALSE; - watchtime = currentTime; +#if defined(QT_THREAD_SUPPORT) + if (timerListMutex) timerListMutex->lock(); +#endif + static timeval tm; + bool first = TRUE; + timeval currentTime; + if ( timerList && timerList->count() ) { // there are waiting timers + getTime( currentTime ); + if ( first ) { + if ( currentTime < watchtime ) { // clock was turned back + repairTimer( currentTime ); + } + first = FALSE; + watchtime = currentTime; + } + TimerInfo *t = timerList->first(); // first waiting timer + if ( currentTime < t->timeout ) { // time to wait + tm = t->timeout - currentTime; + } + else { + tm.tv_sec = 0; // no time to wait + tm.tv_usec = 0; + } + if ( qt_wait_timer_max && *qt_wait_timer_max < tm ) { + tm = *qt_wait_timer_max; + } +#if defined(QT_THREAD_SUPPORT) + if (timerListMutex) timerListMutex->unlock(); +#endif + return &tm; } - TimerInfo *t = timerList->first(); // first waiting timer - if ( currentTime < t->timeout ) { // time to wait - tm = t->timeout - currentTime; - } else { - tm.tv_sec = 0; // no time to wait - tm.tv_usec = 0; + if ( qt_wait_timer_max ) { + tm = *qt_wait_timer_max; +#if defined(QT_THREAD_SUPPORT) + if (timerListMutex) timerListMutex->unlock(); +#endif + return &tm; } - if ( qt_wait_timer_max && *qt_wait_timer_max < tm ) - tm = *qt_wait_timer_max; - return &tm; - } - if ( qt_wait_timer_max ) { - tm = *qt_wait_timer_max; - return &tm; - } - return 0; // no timers +#if defined(QT_THREAD_SUPPORT) + if (timerListMutex) timerListMutex->unlock(); +#endif + return 0; // no timers } // Timer initialization -static void initTimers() // initialize timers +static void initTimers() // initialize timers { - timerBitVec = new QBitArray( 128 ); - Q_CHECK_PTR( timerBitVec ); - int i = timerBitVec->size(); - while( i-- > 0 ) - timerBitVec->clearBit( i ); - timerList = new TimerList; - Q_CHECK_PTR( timerList ); - timerList->setAutoDelete( TRUE ); - gettimeofday( &watchtime, 0 ); + timerBitVec = new QBitArray( 128 ); + Q_CHECK_PTR( timerBitVec ); + int i = timerBitVec->size(); + while( i-- > 0 ) { + timerBitVec->clearBit( i ); + } + timerList = new TimerList; +#if defined(QT_THREAD_SUPPORT) + timerListMutex = new QMutex(true); +#endif + Q_CHECK_PTR( timerList ); + timerList->setAutoDelete( TRUE ); + gettimeofday( &watchtime, 0 ); } // Timer cleanup void cleanupTimers() { - delete timerList; - timerList = 0; - delete timerBitVec; - timerBitVec = 0; + delete timerList; + timerList = 0; + delete timerBitVec; + timerBitVec = 0; +#if defined(QT_THREAD_SUPPORT) + delete timerListMutex; + timerListMutex = 0; +#endif } // Main timer functions for starting and killing timers int qStartTimer( int interval, QObject *obj ) { - if ( !timerList ) // initialize timer data - initTimers(); - int id = allocTimerId(); // get free timer id - if ( id <= 0 || - id > (int)timerBitVec->size() || !obj )// cannot create timer - return 0; - timerBitVec->setBit( id-1 ); // set timer active - TimerInfo *t = new TimerInfo; // create timer - Q_CHECK_PTR( t ); - t->id = id; - t->interval.tv_sec = interval/1000; - t->interval.tv_usec = (interval%1000)*1000; - timeval currentTime; - getTime( currentTime ); - t->timeout = currentTime + t->interval; - t->obj = obj; - insertTimer( t ); // put timer in list - return id; + if ( !timerList ) { // initialize timer data + initTimers(); + } + int id = allocTimerId(); // get free timer id + if ( (id <= 0) || (id > (int)timerBitVec->size()) || (!obj) ) { // cannot create timer + return 0; + } + timerBitVec->setBit( id-1 ); // set timer active + TimerInfo *t = new TimerInfo; // create timer + Q_CHECK_PTR( t ); + t->id = id; + t->interval.tv_sec = interval/1000; + t->interval.tv_usec = (interval%1000)*1000; + timeval currentTime; + getTime( currentTime ); + t->timeout = currentTime + t->interval; + t->obj = obj; + insertTimer( t ); // put timer in list + return id; } bool qKillTimer( int id ) { - register TimerInfo *t; - if ( !timerList || id <= 0 || - id > (int)timerBitVec->size() || !timerBitVec->testBit( id-1 ) ) - return FALSE; // not init'd or invalid timer - t = timerList->first(); - while ( t && t->id != id ) // find timer info in list - t = timerList->next(); - if ( t ) { // id found - timerBitVec->clearBit( id-1 ); // set timer inactive - return timerList->remove(); - } - else // id not found - return FALSE; + register TimerInfo *t; + if ( (!timerList) || (id <= 0) || (id > (int)timerBitVec->size()) || (!timerBitVec->testBit( id-1 )) ) { + return FALSE; // not init'd or invalid timer + } +#if defined(QT_THREAD_SUPPORT) + timerListMutex->lock(); +#endif + t = timerList->first(); + while ( t && t->id != id ) { // find timer info in list + t = timerList->next(); + } + if ( t ) { // id found + bool ret; + timerBitVec->clearBit( id-1 ); // set timer inactive + ret = timerList->remove(); +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); +#endif + return ret; + } + else { // id not found +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); +#endif + return FALSE; + } } bool qKillTimer( QObject *obj ) { - register TimerInfo *t; - if ( !timerList ) // not initialized - return FALSE; - t = timerList->first(); - while ( t ) { // check all timers - if ( t->obj == obj ) { // object found - timerBitVec->clearBit( t->id-1 ); - timerList->remove(); - t = timerList->current(); - } else { - t = timerList->next(); + register TimerInfo *t; + if ( !timerList ) { // not initialized + return FALSE; } - } - return TRUE; +#if defined(QT_THREAD_SUPPORT) + timerListMutex->lock(); +#endif + t = timerList->first(); + while ( t ) { // check all timers + if ( t->obj == obj ) { // object found + timerBitVec->clearBit( t->id-1 ); + timerList->remove(); + t = timerList->current(); + } + else { + t = timerList->next(); + } + } +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); +#endif + return TRUE; } +QEventLoopPrivate::QEventLoopPrivate() { +#if defined(Q_WS_X11) + xfd = -1; + x_gPollFD.fd = -1; + x_gPollFD.events = 0; + x_gPollFD.revents = 0; +#endif // Q_WS_X11 + singletoolkit = TRUE; + ctx = 0; + ctx_is_default = false; + reset(); +} + +QEventLoopPrivate::~QEventLoopPrivate() { + // +} + +void QEventLoopPrivate::reset() { + looplevel = 0; + quitcode = 0; + quitnow = FALSE; + exitloop = FALSE; + shortcut = FALSE; +} /***************************************************************************** @@ -358,9 +450,6 @@ bool qKillTimer( QObject *obj ) void QEventLoop::registerSocketNotifier( QSocketNotifier *notifier ) { - - - int sockfd = notifier->socket(); int type = notifier->type(); if ( sockfd < 0 || type < 0 || type > 2 || notifier == 0 ) { @@ -378,13 +467,13 @@ void QEventLoop::registerSocketNotifier( QSocketNotifier *notifier ) QSockNotGPollFD *sn; /* - if ( ! list ) { - // create new list, the QSockNotType destructor will delete it for us + if ( ! list ) { + // create new list, the QSockNotType destructor will delete it for us list = new QPtrList<QSockNot>; Q_CHECK_PTR( list ); list->setAutoDelete( TRUE ); d->sn_list = list; - } + } */ gushort events=0; @@ -400,10 +489,10 @@ void QEventLoop::registerSocketNotifier( QSocketNotifier *notifier ) break; } - sn = new QSockNotGPollFD; - Q_CHECK_PTR( sn ); - sn->obj = notifier; - sn->gPollFD.fd = sockfd; + sn = new QSockNotGPollFD; + Q_CHECK_PTR( sn ); + sn->obj = notifier; + sn->gPollFD.fd = sockfd; sn->gPollFD.events = events; sn->events = events; // save events! sn->pending = FALSE; @@ -429,21 +518,24 @@ void QEventLoop::unregisterSocketNotifier( QSocketNotifier *notifier ) printf("unregister socket notifier %d\n", sockfd); #endif - QPtrList<QSockNotGPollFD> *list = &d->sn_list; - QSockNotGPollFD *sn; - if ( ! list ) - return; - sn = list->first(); - while ( sn && !(sn->obj == notifier) ) - sn = list->next(); - if ( !sn ) // not found - return; - + QPtrList<QSockNotGPollFD> *list = &d->sn_list; + QSockNotGPollFD *sn; + if ( ! list ) { + return; + } + sn = list->first(); + while ( sn && !(sn->obj == notifier) ) { + sn = list->next(); + } + if ( !sn ) { // not found + return; + } + d->sn_pending_list.removeRef( sn ); - list->remove(); // remove notifier found above + list->remove(); // remove notifier found above g_source_remove_poll(d->gSource, &sn->gPollFD); - delete sn; // we don't autodelete - lets do it manually + delete sn; // we don't autodelete - lets do it manually } @@ -514,62 +606,95 @@ void QEventLoop::wakeUp() int QEventLoop::timeToWait() const { - timeval *tm = qt_wait_timer(); - if ( ! tm ) // no active timers - return -1; - return (tm->tv_sec*1000) + (tm->tv_usec/1000); + timeval *tm = qt_wait_timer(); + if ( !tm ) { // no active timers + return -1; + } + return (tm->tv_sec*1000) + (tm->tv_usec/1000); } int QEventLoop::activateTimers() { - if ( !timerList || !timerList->count() ) // no timers - return 0; - bool first = TRUE; - timeval currentTime; - int n_act = 0, maxCount = timerList->count(); - TimerInfo *begin = 0; - register TimerInfo *t; - - for ( ;; ) { - if ( ! maxCount-- ) - break; - getTime( currentTime ); // get current time - if ( first ) { - if ( currentTime < watchtime ) // clock was turned back - repairTimer( currentTime ); - first = FALSE; - watchtime = currentTime; + if ( !timerList || !timerList->count() ) { // no timers + return 0; } - t = timerList->first(); - if ( !t || currentTime < t->timeout ) // no timer has expired - break; - if ( ! begin ) { - begin = t; - } else if ( begin == t ) { - // avoid sending the same timer multiple times - break; - } else if ( t->interval < begin->interval || t->interval == begin->interval ) { - begin = t; +#if defined(QT_THREAD_SUPPORT) + timerListMutex->lock(); +#endif + bool first = TRUE; + timeval currentTime; + int n_act = 0, maxCount = timerList->count(); + TimerInfo *begin = 0; + register TimerInfo *t; + + for ( ;; ) { + if ( ! maxCount-- ) { + break; + } + getTime( currentTime ); // get current time + if ( first ) { + if ( currentTime < watchtime ) { // clock was turned back + repairTimer( currentTime ); + } + first = FALSE; + watchtime = currentTime; + } + t = timerList->first(); + if ( !t || currentTime < t->timeout ) { // no timer has expired + break; + } + if ( ! begin ) { + begin = t; + } + else if ( begin == t ) { + // avoid sending the same timer multiple times + break; + } + else if ( t->interval < begin->interval || t->interval == begin->interval ) { + begin = t; + } + timerList->take(); // unlink from list + t->timeout += t->interval; + if ( t->timeout < currentTime ) { + t->timeout = currentTime + t->interval; + } + insertTimer( t ); // relink timer + if ( t->interval.tv_usec > 0 || t->interval.tv_sec > 0 ) { + n_act++; + } +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); +#endif + QTimerEvent e( t->id ); +#if defined(QT_THREAD_SUPPORT) + // Be careful...the current thread may not be the target object's thread! + if ((!t->obj) || (t->obj && (t->obj->contextThreadObject() == QThread::currentThreadObject()))) { + QApplication::sendEvent( t->obj, &e ); // send event + } + else { + QApplication::postEvent( t->obj, new QTimerEvent(e) ); // post event to correct thread + } +#else // defined(QT_THREAD_SUPPORT) + QApplication::sendEvent( t->obj, &e ); // send event +#endif // defined(QT_THREAD_SUPPORT) +#if defined(QT_THREAD_SUPPORT) + timerListMutex->lock(); +#endif + if ( timerList->findRef( begin ) == -1 ) { + begin = 0; + } } - timerList->take(); // unlink from list - t->timeout += t->interval; - if ( t->timeout < currentTime ) - t->timeout = currentTime + t->interval; - insertTimer( t ); // relink timer - if ( t->interval.tv_usec > 0 || t->interval.tv_sec > 0 ) - n_act++; - QTimerEvent e( t->id ); - QApplication::sendEvent( t->obj, &e ); // send event - if ( timerList->findRef( begin ) == -1 ) - begin = 0; - } - return n_act; +#if defined(QT_THREAD_SUPPORT) + timerListMutex->unlock(); +#endif + return n_act; } int QEventLoop::activateSocketNotifiers() { - if ( d->sn_pending_list.isEmpty() ) - return 0; + if ( d->sn_pending_list.isEmpty() ) { + return 0; + } // activate entries int n_act = 0; @@ -584,7 +709,17 @@ int QEventLoop::activateSocketNotifiers() printf("activate sn : send event fd=%d\n", sn->gPollFD.fd ); #endif sn->pending = FALSE; - QApplication::sendEvent( sn->obj, &event ); +#if defined(QT_THREAD_SUPPORT) + // Be careful...the current thread may not be the target object's thread! + if ((!sn->obj) || (sn->obj && (sn->obj->contextThreadObject() == QThread::currentThreadObject()))) { + QApplication::sendEvent( sn->obj, &event ); // send event + } + else { + QApplication::postEvent( sn->obj, new QEvent(event) ); // post event to correct thread + } +#else // defined(QT_THREAD_SUPPORT) + QApplication::sendEvent( sn->obj, &event ); // send event +#endif // defined(QT_THREAD_SUPPORT) n_act++; } } |