[Kde-cygwin-cvs] CVS: qt-3/src/kernel qthread_win.cpp,1.1.2.4.2.7,1.1.2.4.2.8
Status: Inactive
Brought to you by:
habacker
From: Christian E. <che...@us...> - 2005-08-01 17:46:35
|
Update of /cvsroot/kde-cygwin/qt-3/src/kernel In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10188/src/kernel Modified Files: Tag: QT_WIN32_3_3_BRANCH qthread_win.cpp Log Message: some pieces from qt4 Index: qthread_win.cpp =================================================================== RCS file: /cvsroot/kde-cygwin/qt-3/src/kernel/Attic/qthread_win.cpp,v retrieving revision 1.1.2.4.2.7 retrieving revision 1.1.2.4.2.8 diff -u -r1.1.2.4.2.7 -r1.1.2.4.2.8 --- qthread_win.cpp 15 Apr 2005 19:13:40 -0000 1.1.2.4.2.7 +++ qthread_win.cpp 1 Aug 2005 17:46:26 -0000 1.1.2.4.2.8 @@ -46,6 +46,7 @@ #include <process.h> +extern Qt::WindowsVersion qt_winver; static QThreadInstance main_instance = { 0, { 0, &main_instance }, 0, 0, 1, 0, /*PTHREAD_COND_INITIALIZER*/0, 0 }; @@ -53,17 +54,16 @@ static QMutexPool *qt_thread_mutexpool = 0; -static DWORD storage_key = 0; +static DWORD storage_key = TLS_OUT_OF_INDEXES; + static void create_storage_key() { - if ( storage_key ) - return ; + if ( storage_key != TLS_OUT_OF_INDEXES ) + return; - static QCriticalSection criticalSection; // hmmm we should really only run this once. - criticalSection.enter(); - if ( !storage_key ) // double check ;) - storage_key = TlsAlloc(); - criticalSection.leave(); + static QMutex mutex; + QMutexLocker locker(&mutex); + storage_key = TlsAlloc(); } QThreadInstance *QThreadInstance::current() @@ -86,6 +86,7 @@ handle = 0; thread_id = 0; + waiters = 0; // threads have not been initialized yet, do it now if ( ! qt_thread_mutexpool ) @@ -129,10 +130,12 @@ QThreadStorageData::finish( d->thread_storage ); d->thread_storage = 0; + + if (!d->waiters) { + CloseHandle(d->handle); + d->handle = 0; + } d->thread_id = 0; - //pthread_cond_broadcast(&d->thread_done); - ::CloseHandle( d->handle ); - d->handle = 0; if ( d->orphan ) { d->deinit(); @@ -148,6 +151,9 @@ void QThreadInstance::terminate() { + if ( !running ) + return; + ::TerminateThread( handle, 0 ); } @@ -286,12 +292,6 @@ _endthreadex( 0 ); } -static unsigned int __stdcall start_thread( void* that ) -{ - // Dam error C2248: 'd' : cannot access private member declared blablabla - return 0; // what should we return here?? -} - /*! Begins execution of the thread by calling run(), which should be reimplemented in a QThread subclass to contain your code. The @@ -313,17 +313,15 @@ qWarning( "Attempt to start a thread already running" ); #endif // QT_CHECK_STATE - //d->thread_done.wait( locker.mutex() ); - wait(); + return; } - d->running = TRUE; - d->finished = FALSE; + d->running = true; + d->finished = false; + // imho not needed - we can access d every time ... d->args[ 0 ] = this; d->args[ 1 ] = d; - //(HANDLE)::_beginthreadex(0, 0, &_dispatch, task, 0, &_tid) - // ulong ret = ::_beginthreadex(0, 0, &_dispatch, start_thread, (unsigned int*)&d->thread_id); d->handle = ( Qt::HANDLE ) ::_beginthreadex( 0, d->stacksize, QThreadInstance::start, d->args, CREATE_SUSPENDED, &( d->thread_id ) ); if ( !d->handle ) { @@ -331,16 +329,16 @@ qWarning( "QThread::start: thread creation error: %i", GetLastError() ); #endif // QT_CHECK_STATE - d->running = FALSE; - d->finished = FALSE; + d->running = false; + d->finished = false; d->args[ 0 ] = d->args[ 1 ] = 0; - return ; + return; } int prio; // = THREAD_PRIORITY_NORMAL; switch ( priority ) { case IdlePriority: - prio = THREAD_PRIORITY_IDLE; + prio = ( qt_winver == Qt::WV_DOS_based ) ? THREAD_PRIORITY_LOWEST : THREAD_PRIORITY_IDLE; break; case LowestPriority: prio = THREAD_PRIORITY_LOWEST; @@ -358,7 +356,7 @@ prio = THREAD_PRIORITY_HIGHEST; break; case TimeCriticalPriority: - prio = THREAD_PRIORITY_TIME_CRITICAL; + prio = ( qt_winver == Qt::WV_DOS_based ) ? THREAD_PRIORITY_HIGHEST : THREAD_PRIORITY_TIME_CRITICAL; break; //case InheritPriority: @@ -367,16 +365,16 @@ break; } - if ( !::SetThreadPriority( d->handle, prio ) ) { + if ( !SetThreadPriority( d->handle, prio ) ) { #if defined(QT_CHECK_STATE) - qWarning( "::SetThreadPriority failed!!!!" ); + qWarning( "QThread::start: Failed to set thread priority" ); #endif } // Now we let the thread run - if ( (int)::ResumeThread( d->handle ) == -1 ) { + if ( ResumeThread( d->handle ) == ( DWORD )-1 ) { #if defined(QT_CHECK_STATE) - qWarning( "::ResumeThread failed!!!!!" ); + qWarning( "QThread::start: Failed to resume new thread" ); #endif } @@ -404,10 +402,47 @@ */ bool QThread::wait( unsigned long time ) { - // QMutexLocker locker( qt_thread_mutexpool->get( d ) ); + QMutexLocker locker( d->mutex() ); + + if ( d->thread_id == GetCurrentThreadId() ) { + qWarning("Thread tried to wait on itself"); + return false; + } + if ( d->finished || ! d->running ) - return TRUE; - return ::WaitForSingleObjectEx( d->handle, time, FALSE ) != WAIT_OBJECT_0; + return true; + + ++d->waiters; + locker.mutex()->unlock(); + + bool ret = false; + switch ( WaitForSingleObject( d->handle, time ) ) { + case WAIT_OBJECT_0: + ret = true; + break; + case WAIT_FAILED: + qWarning("QThread::wait: Thread wait failure"); + break; + case WAIT_ABANDONED: + case WAIT_TIMEOUT: + default: + break; + } + + locker.mutex()->lock(); + --d->waiters; + + if ( ret && !d->finished ) { + // thread was terminated by someone else + QThreadInstance::finish( d ); + } + + if ( d->finished && !d->waiters ) { + CloseHandle( d->handle ); + d->handle = 0; + } + + return ret; } /*! |